4

I'm trying to create a package with directory structure as follow:

src/
    folder1/
        index.ts
        ...
    folder2/
        index.ts
        ...
    folder3/
        index.ts
        ...
.
.
.

And I would like to import it as import { Class1 } from '@package/folder1', import { Class2 } from '@package/folder2', etc.

At this moment, I'm able to import my classes like import { Class1 } from '@package/lib/folder1'. Is it possible to achieve my expected behavior?

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./lib",
    "rootDir": "./src",
    "rootDirs": ["./src/folder1"],
    "lib": [
      "es2017",
      "esnext.asynciterable"
    ],
    "typeRoots": [
      "./node_modules/@types"
    ],
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "pretty": true,
    "noEmit": false,
    "esModuleInterop": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "lib"]
}

David Lilue
  • 597
  • 2
  • 14
  • Is this what you are looking for: https://www.npmjs.com/package/module-alias? – Rvy Pandey Feb 10 '20 at 16:17
  • Not exactly. It is useful indeed but my problem is just the `lib` directory. I want the root of my package to be `lib`. So I don't need to specify it in the import. – David Lilue Feb 10 '20 at 16:45

2 Answers2

0

It seems this feature is not out-of-the-box in the version of Typescript I'm using (3.7.4). The package module-alias could be a workaround but it looks like Typescript 3.8 will allow adding aliases to the exports.

David Lilue
  • 597
  • 2
  • 14
-1

Setting the output directory as the package directory in the following manner should solve your problem:

"outDir": "."

This will help you make imports without the "./lib" that currently your "outDir" refers to in the tsconfig.json.

Suryasish Paul
  • 399
  • 2
  • 12