ezolenko / rollup-plugin-typescript2

Rollup plugin for typescript with compiler errors.
MIT License
819 stars 71 forks source link

Extra declarations (`.d.ts` files) in build output #429

Closed d8corp closed 1 year ago

d8corp commented 1 year ago

Troubleshooting

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior Yes I guess, tsc have the same output, but it strange, because of useless js files removed and .d.ts not.

  2. Does your Rollup plugin order match this plugin's compatibility? If not, please elaborate Yes, it's alone.

  3. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction Examples: online and offline

What happens and why it is incorrect

There are created .d.ts files for that .js excluded by rollup. So I have .d.ts files without .js.

from

src
├ index.ts
├ a.ts
└ b.ts

index includes only a.ts, no one includes b.ts

to

lib
├ index.js
├ index.d.ts
├ a.js
├ a.d.ts
└ b.d.ts <- this file useless

Environment

Reproduces everywhere.

versions

:
```text System: OS: macOS 13.0.1 CPU: (8) arm64 Apple M1 Memory: 45.69 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.16.0 - /usr/local/bin/node Yarn: 1.22.11 - /usr/local/bin/yarn npm: 8.11.0 - /usr/local/bin/npm npmPackages: rollup: ^3.3.0 => 3.3.0 rollup-plugin-typescript2: ^0.34.1 => 0.34.1 typescript: ^4.8.4 => 4.8.4 ```

rollup.config.mjs

:
```js import typescript from 'rollup-plugin-typescript2' export default { input: './src/index.ts', output: { dir: 'result', entryFileNames: '[name].js', format: 'cjs', exports: 'named', preserveModules: true, }, plugins: [ typescript({ verbosity: 3, clean: true, }), ] } ```

tsconfig.json

:
```json5 { "compilerOptions": { "target": "ES6", "module": "esnext", "moduleResolution": "node", "declaration": true }, "include": [ "src" ] } ```

package.json

:
```json { "private": true, "scripts": { "build": "rollup -c" }, "devDependencies": { "rollup": "^3.3.0", "rollup-plugin-typescript2": "^0.34.1", "typescript": "^4.8.4" } } ```

plugin output with verbosity 3

:
```text rpt2: built-in options overrides: { "noEmitHelpers": false, "importHelpers": true, "noResolve": false, "noEmit": false, "noEmitOnError": false, "inlineSourceMap": false, "outDir": "/Users/deight/cantinc/rollup-plugin-typescript2-bug/node_modules/.cache/rollup-plugin-typescript2/placeholder", "moduleResolution": 2, "allowNonTsExtensions": true } rpt2: parsed tsconfig: { "options": { "target": 2, "module": 99, "moduleResolution": 2, "declaration": true, "configFilePath": "/Users/deight/cantinc/rollup-plugin-typescript2-bug/tsconfig.json", "noEmitHelpers": false, "importHelpers": true, "noResolve": false, "noEmit": false, "noEmitOnError": false, "inlineSourceMap": false, "outDir": "/Users/deight/cantinc/rollup-plugin-typescript2-bug/node_modules/.cache/rollup-plugin-typescript2/placeholder", "allowNonTsExtensions": true }, "fileNames": [ "/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/a.ts", "/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/b.ts", "/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/index.ts" ], "typeAcquisition": { "enable": false, "include": [], "exclude": [] }, "raw": { "compilerOptions": { "target": "ES6", "module": "esnext", "moduleResolution": "node", "declaration": true }, "include": [ "src" ], "compileOnSave": false }, "errors": [], "wildcardDirectories": { "/users/deight/cantinc/rollup-plugin-typescript2-bug/src": 1 }, "compileOnSave": false } rpt2: typescript version: 4.8.4 rpt2: tslib version: 2.4.1 rpt2: rollup version: 3.3.0 rpt2: rollup-plugin-typescript2 version: 0.34.1 rpt2: plugin options: { "check": true, "verbosity": 3, "clean": true, "cacheRoot": "/Users/deight/cantinc/rollup-plugin-typescript2-bug/node_modules/.cache/rollup-plugin-typescript2", "include": [ "*.ts+(|x)", "**/*.ts+(|x)" ], "exclude": [ "*.d.ts", "**/*.d.ts" ], "abortOnError": true, "rollupCommonJSResolveHack": false, "useTsconfigDeclarationDir": false, "tsconfigOverride": {}, "transformers": [], "tsconfigDefaults": {}, "objectHashIgnoreUnknownHack": false, "cwd": "/Users/deight/cantinc/rollup-plugin-typescript2-bug", "typescript": "version 4.8.4" } rpt2: rollup config: { "external": [], "input": "./src/index.ts", "plugins": [ { "name": "rpt2" }, { "name": "stdin" } ], "output": [ { "dir": "result", "entryFileNames": "[name].js", "exports": "named", "format": "cjs", "plugins": [], "preserveModules": true } ] } rpt2: tsconfig path: /Users/deight/cantinc/rollup-plugin-typescript2-bug/tsconfig.json rpt2: included: [ "*.ts+(|x)", "**/*.ts+(|x)" ] rpt2: excluded: [ "*.d.ts", "**/*.d.ts" ] rpt2: transpiling '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/index.ts' rpt2: generated declarations for '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/index.ts' rpt2: transpiling '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/a.ts' rpt2: generated declarations for '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/a.ts' rpt2: dependency '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/a.ts' rpt2: imported by '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/index.ts' rpt2: resolving './a' imported by '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/index.ts' rpt2: to '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/a.ts' rpt2: type-checking missed '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/b.ts' rpt2: generating target 1 rpt2: generating missed declarations for '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/b.ts' rpt2: generated declarations for '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/b.ts' rpt2: emitting declarations for '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/index.ts' to 'index.d.ts' rpt2: emitting declarations for '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/a.ts' to 'a.d.ts' rpt2: emitting declarations for '/Users/deight/cantinc/rollup-plugin-typescript2-bug/src/b.ts' to 'b.d.ts' created result in 288ms ```
agilgur5 commented 1 year ago

Thanks for the detailed issue and repros @d8corp!

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior Yes I guess, tsc have the same output, but it strange, because of useless js files removed and .d.ts not.

So this is pretty key -- the reason we ask this is because, like most TS integrations (including Webpack's ts-loader), we try to standardize on tsc's behavior for a consistent experience for developers. Usually, any difference between tsc and a TS integration is more confusing to developers than the opposite.

In your case, this is relatively easy to solve:

  1. tsc figures out what files to perform operations on based on your tsconfig
  2. Your tsconfig has "include": ["src"] in it, which includes b.ts, even if you don't use it in your build.
  3. You can reconfigure this in a few different ways, for example:
    1. "include": ["src/index.ts"]
    2. replace include with files: "files": ["src/index.ts"]

One key caveat here is that tsc and your IDE won't type-check b.ts if you do this. So this is why you may want to have a separate tsconfig.build.json that only differs in include/files and can otherwise extends your tsconfig.json (or vice-versa).

As such this isn't a bug, but tsc behavior. Hopefully the above should fix your issue 🙂

NoirVoider commented 1 year ago

I met the same confusion, I tried

  1. "include": ["src/index.ts"]
  2. replace include with files: "files": ["src/index.ts"] and have no effect
agilgur5 commented 1 year ago

I met the same confusion, I tried

this is working in many repos, so you'd have to provide a reproduction or failing test with this configuration not working. otherwise, similar to the above, it is likely a misconfiguration.