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

fix(typescript)!: correctly resolve filenames of declaration files for output.file #1728

Merged
merged 4 commits into from
Sep 22, 2024

Conversation

lameuler
Copy link
Contributor

@lameuler lameuler commented Jun 3, 2024

Rollup Plugin Name: @rollup/plugin-typescript

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers:

Description

Currently, when output.file is specified instead of output.dir, the filename for emitting declaration files with this.emitFile is resolved incorrectly. This means that given a declarationDir that is outside of the bundle output directory (which is output.dir || dirname(output.file!)), the declarations will still be generated inside the bundle, albeit in a location not actually matching declarationDir that does not respect the absolute file path generated by typescript.

Whereas when using output.dir, declarationDir is actually validated and filenames for declarations are resolved correctly.

For example, with output.file = 'some-path/index.js' and declarationDir = 'types', the declaration files will be generated at some-path/types/index.d.ts, instead of types/index.d.ts which is the correct location (but invalid as it would be output the bundle directory). When output.dir = 'some-path' is used instead, validation fails as expected.

This can be fixed by just using dirname(output.file) in place of output.dir when output.file is specified, and including a validation check that declarationDir is valid when using output.file.

@lameuler lameuler requested a review from shellscape as a code owner June 3, 2024 03:15
Copy link
Contributor Author

@lameuler lameuler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baseDir, which is used to resolve the pathname for this.emitFile using path.relative(baseDir, id), should just be the bundle output directory since id is the absolute file path we want to emit.

@shellscape
Copy link
Collaborator

Thanks for the PR. Since this will change some expectations around outputs, we'll have to mark this as a breaking change.

@shellscape shellscape changed the title fix(typescript): correctly resolve filenames of declaration files for output.file fix(typescript)!: correctly resolve filenames of declaration files for output.file Sep 22, 2024
@shellscape shellscape merged commit a85b649 into rollup:master Sep 22, 2024
20 checks passed
michaelfaith added a commit to michaelfaith/plugins that referenced this pull request Oct 10, 2024
This change corrects a regression introduced by rollup#1728, preventing dual bundle configs from writing output files to directories nested within the directory defined in the tsconfig's outDir property.

Example:
```js
export default {
  input: 'src/index.ts', // Replace with the path to your entry TypeScript file
  output: [
    {
      dir: 'dist/cjs',
      format: 'cjs',
      sourcemap: true,
      preserveModules: true
    },
    {
      dir: 'dist/esm',
      format: 'esm',
      sourcemap: true,
      preserveModules: true
    }
  ],
  plugins: [
    json(),
    resolve(),
    commonjs(),
    typescript({
      tsconfig: 'tsconfig.json',
      compilerOptions: {
        /* Basic Options */
        target: 'ES5',
        module: 'ES2020',
        lib: ['ES2019', 'ES2020'],
        allowJs: true,
        checkJs: false,
        /* Strict Type-Checking Options */
        strict: true,
        /* Module Resolution Options */
        moduleResolution: 'node',
        esModuleInterop: true,
        /* Advanced Options */
        forceConsistentCasingInFileNames: true,
        skipDefaultLibCheck: true,
        skipLibCheck: true,
        outDir: path.join(__dirname, 'dist')
      }
    })
  ],
  external: deps // Add any external dependencies you don't want to bundle
};
```
michaelfaith added a commit to michaelfaith/plugins that referenced this pull request Oct 10, 2024
This change corrects a regression introduced by rollup#1728, preventing dual bundle configs from writing output files to directories nested within the directory defined in the tsconfig's outDir property.

Example:
```js
export default {
  input: 'src/index.ts', // Replace with the path to your entry TypeScript file
  output: [
    {
      dir: 'dist/cjs',
      format: 'cjs',
      sourcemap: true,
      preserveModules: true
    },
    {
      dir: 'dist/esm',
      format: 'esm',
      sourcemap: true,
      preserveModules: true
    }
  ],
  plugins: [
    json(),
    resolve(),
    commonjs(),
    typescript({
      tsconfig: 'tsconfig.json',
      compilerOptions: {
        /* Basic Options */
        target: 'ES5',
        module: 'ES2020',
        lib: ['ES2019', 'ES2020'],
        allowJs: true,
        checkJs: false,
        /* Strict Type-Checking Options */
        strict: true,
        /* Module Resolution Options */
        moduleResolution: 'node',
        esModuleInterop: true,
        /* Advanced Options */
        forceConsistentCasingInFileNames: true,
        skipDefaultLibCheck: true,
        skipLibCheck: true,
        outDir: path.join(__dirname, 'dist')
      }
    })
  ],
  external: deps // Add any external dependencies you don't want to bundle
};
```
shellscape pushed a commit that referenced this pull request Oct 16, 2024
#1783)

fix(typescript): allow for files to be nested within outDir

This change corrects a regression introduced by #1728, preventing dual bundle configs from writing output files to directories nested within the directory defined in the tsconfig's outDir property.

Example:
```js
export default {
  input: 'src/index.ts', // Replace with the path to your entry TypeScript file
  output: [
    {
      dir: 'dist/cjs',
      format: 'cjs',
      sourcemap: true,
      preserveModules: true
    },
    {
      dir: 'dist/esm',
      format: 'esm',
      sourcemap: true,
      preserveModules: true
    }
  ],
  plugins: [
    json(),
    resolve(),
    commonjs(),
    typescript({
      tsconfig: 'tsconfig.json',
      compilerOptions: {
        /* Basic Options */
        target: 'ES5',
        module: 'ES2020',
        lib: ['ES2019', 'ES2020'],
        allowJs: true,
        checkJs: false,
        /* Strict Type-Checking Options */
        strict: true,
        /* Module Resolution Options */
        moduleResolution: 'node',
        esModuleInterop: true,
        /* Advanced Options */
        forceConsistentCasingInFileNames: true,
        skipDefaultLibCheck: true,
        skipLibCheck: true,
        outDir: path.join(__dirname, 'dist')
      }
    })
  ],
  external: deps // Add any external dependencies you don't want to bundle
};
```
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

Successfully merging this pull request may close these issues.

2 participants