Home > Software design >  Remove dist from the import path
Remove dist from the import path

Time:10-02

I wrote a small library

On lib/index.ts I have this code

export default function hello() {
    return 'world'
}

package.json

{
  "name": "@org/library",
  "author": "Org name",
  "version": "1.0.8",
  "main": "dist/index.js",
  "license": "MIT",
  "devDependencies": {
    "typescript": "^4.4.3"
    ...
  },
  "scripts": {
    "build": "tsc",
    "clear": "rimraf dist/"
  }
}

tsconfig.json

{
  "exclude": ["lib/**/*.spec.tsx", "lib/**/*.stories.tsx", "lib/**/*.styles.tsx", "dist/**/*"],
  "compilerOptions": {
    "skipLibCheck": true,
    "target": "es6",
    "module": "es6",
    "lib": ["es6", "dom"],
    "jsx": "react",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "dist",
    "rootDirs": ["lib"],
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  }
}

But when I go to another project to use, I had to import using dist on the path

Why I had to import like this

import hello from '@org/library/dist'

And not

import hello from '@org/library/dist'

What I need to do to remove dist from the import path?

CodePudding user response:

I ended with a custom Makefile

.PHONY: build clean lint publish test

build:
    npm run build

clean:
    npm run clean

lint:
    npm run lint

test:
    npm run test

publish: clean lint test build
    cp package.json dist
    npm publish dist
  • Related