Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The import file path of the ".d.ts" file is not converted to a relative path. #1501

Closed
stegano opened this issue Sep 6, 2022 · 1 comment

Comments

@stegano
Copy link

stegano commented Sep 6, 2022

Hi

I am using the absolute path in the source code through the path alias setting in tsconfig.json. And I enabled the declaration option in tsconfig.json.

When built using ts-loader and webpack, it builds normally and creates a .js file, but the path of the import statement in the .d.ts file is still displayed as an absolute path.

I wonder why the generated .d.ts path is not converted.

Expected Behaviour

The file path of the import statement in the .d.ts file must also be converted into a relative path.

Actual Behaviour

The import statements in the d.ts file are displayed the same as the source code (in absolute path).

Steps to Reproduce the Problem

// webpack.js
{
  mode: "production",
  entry: {
    ...
  },
  module: {
    rules: [
    {
      test: /\.ts$/,
      use: "ts-loader",
      exclude: /node_modules/,
    },
    ],
  },
  resolve: {
    alias: {
      src: path.resolve(__dirname, "src"),
    },
    extensions: [".ts"],
  },
}
// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true,
    "declarationDir": "dist/@types",
    "noEmit": false,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "baseUrl": ".",
    "paths": {
      "src/*": ["src/*"]
    },
    "outDir": "dist"
  },
  "include": ["global.d.ts", "src/**/*"],
  "exclude": ["node_modules"]
}

Location of a Minimal Repository that Demonstrates the Issue.

@stegano
Copy link
Author

stegano commented Sep 7, 2022

I have confirmed that the handling of path conversion is done through webpack.resolve, so I will close this issue.

I'm share my findings below in the hopes that it will help someone.

ts-loader seems to deliver the JS converted using the TS compiler to the webpack loader context (I could check the path of the .d.ts file generated by the TS compiler through the webpack assets) .

The TS compiler does not handle path conversion and seems to be designed to only convert the code written in Typescript into Javascript code. (Please check the discussion link below.)

Therefore, it seems that the path conversion process of files created through other library modules such as webpack.resolve(bundling processing only), ts-alias, ttypescript, etc. should be done.

Please let me know if I'm wrong.

�Thanks.

@stegano stegano closed this as completed Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant