Closed phun-ky closed 1 year ago
Perhaps this is relevant: https://github.com/rollup/plugins/issues/1480
So declaration files are not supposed to be directly imported in TS. TS picks them up without imports. See #273 for some more details.
@agilgur5 well, that kind of goes without saying :) but I am not including the declaration files directly, nor is helmet
.
@phun-ky try explicitly including d.cts and d.mts:
include: [ "*.ts+(|x)", "**/*.ts+(|x)", '**/*.d.cts', '**/*.d.mts' ]
This will run them through typescript and I guess produce empty js.
Somehow import 'helmet'
resolves to .d.cts:
rpt2: dependency '/home/projects/rpt2-repro-vnppxj/node_modules/helmet/index.d.cts'
rpt2: imported by '/home/projects/rpt2-repro-vnppxj/src/index.ts'
rpt2: resolving 'helmet' imported by '/home/projects/rpt2-repro-vnppxj/src/index.ts'
rpt2: to '/home/projects/rpt2-repro-vnppxj/node_modules/helmet/index.d.cts'
Ah, ignore that -- this fixes the problem:
include: [ "*.ts+(|x)", "**/*.ts+(|x)", '**/*.cts', '**/*.mts' ]
@phun-ky could you try building your whole repo with master branch of the plugin?
@ezolenko still got issues, sorry for the late reply!
/home/alexander/Workspace/knowit/internal/devplatform-server-api/packages/middlewares/csp/src/index.ts → dist...
[!] (plugin rpt2) RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
../../../node_modules/helmet/index.d.cts (3:5)
1: import {IncomingMessage, ServerResponse} from "http"
2:
3: type ContentSecurityPolicyDirectiveValueFunction = (req: IncomingMessage, res: ServerResponse) => string
^
4: type ContentSecurityPolicyDirectiveValue = string | ContentSecurityPolicyDirectiveValueFunction
5: interface ContentSecurityPolicyOptions {
at error (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:279:30)
at Module.error (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:13933:16)
at Module.tryParse (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:14644:25)
at Module.setSource (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:14246:39)
at ModuleLoader.addModuleSource (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:24211:20)
@phun-ky could you post verbose output when building with master branch and your rollup config if different from what you already posted?
Here is the verbose output, tsconfig.json
should be unchanged imho:
npm run build
So this run succeeded? Try doing a clean build
Crap, no, parts of the log is missing
@ezolenko updated the comment with the output now, sorry
What's weird is that this works like a charm:
$ ts-node --esm --project tsconfig.json src/index.ts
Any progress on this issue? :)
@phun-ky the progress is slow :)
Could you try mts
branch (set this in your package.json: "rollup-plugin-typescript2": "git@github.com:ezolenko/rollup-plugin-typescript2.git#mts",
)
Your example project compiles for me on that branch.
@ezolenko yep! that worked like a charm!
Ok, I'll make a release with the last few fixes
@ezolenko thanks for the great work! I really appreciate it!
@agilgur5 well, that kind of goes without saying :) but I am not including the declaration files directly, nor is
helmet
.
Ah, sorry for not checking this in more depth earlier! Was on my phone and just responded real quick to the title. We've had a lot of folks trying to import .d.ts
files in the past so thought that a quick response with a link to a similar issue might be helpful.
Glad that ezolenko had some time to spare and could help in more depth that same day!
Somehow
import 'helmet'
resolves to .d.cts:
So I did root cause this as I was curious how the heck that happened as that's definitely not correct behavior. Took me a bit to find Helmet's package.json
as it is dynamically generated. Its current version's internal only have .cjs
, .mjs
, .cts
extensions, which rpt2 did not previously support.
Specifically, rpt2 resolved one of the references to the index.d.cts
file, which is a possible correct resolution, but rpt2 is supposed to ignore declarations. Those checks detected declarations as files with .d.ts
, so ezolenko had to change this line and our default exclude
filter to now also ignore .d.cts
and .d.mts
as those are now possible extensions in newer TS versions.
That is to say, the Unexpected token
error is actually correct, as rpt2 shouldn't be reading declarations, but it shouldn't be loading declarations into Rollup at all to begin with. It just didn't know the new file extensions in order to ignore them. Will rename the issue to make that clearer.
In a bout of extreme irony, when I looked at my local code, I actually already had a similar line there 😵💫🤔 It turns out while I was attempting to fix #426 back in October (never quite finished that, partly because I was having trouble with Rollup ESM config), I made this commit on one of my branches containing the exact same line 😅
Troubleshooting
Does
tsc
have the same output? If so, please explain why this is incorrect behaviorN/A
Does your Rollup plugin order match this plugin's compatibility? If not, please elaborate
yes
Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction
https://stackblitz.com/edit/rpt2-repro-3j5vny?file=src%2Findex.ts,package.json,src%2Flib%2Findex.ts,README.md,tsconfig.json
What happens and why it is incorrect
Error is thrown, should have support for
*.cts
/*.mts
filesEnvironment
Ubuntu 22.04
Versions
```js /* global process */ import { nodeResolve } from '@rollup/plugin-node-resolve'; import ts from 'rollup-plugin-typescript2'; import dts from 'rollup-plugin-dts'; import json from '@rollup/plugin-json'; import typescript from 'typescript'; import terser from '@rollup/plugin-terser'; import commonjs from '@rollup/plugin-commonjs'; import externals from 'rollup-plugin-node-externals'; const PACKAGE_NAME = process.cwd(); export default [ { input: `${PACKAGE_NAME}/src/index.ts`, output: { dir: `${PACKAGE_NAME}/dist`, format: 'es' }, plugins: [ externals(), json(), nodeResolve({ preferBuiltins: false }), commonjs(), ts({ useTsconfigDeclarationDir: false, sourceMap: false, typescript, tsconfig: `${PACKAGE_NAME}/tsconfig.json` }), terser() ] }, { input: `${PACKAGE_NAME}/dist/index.d.ts`, output: [{ file: `${PACKAGE_NAME}/dist/index.d.ts`, format: 'es' }], plugins: [dts()] } ]; ```
:rollup.config.js
```json5 { "compilerOptions": { "module": "ES2020", "target": "es2022", "lib": ["ES2021", "dom", "dom.iterable"], "sourceMap": true, "declaration": true, // "declarationDir": "./dts", "moduleResolution": "node", "rootDirs": ["middlewares", "utils", "routes"], "noImplicitReturns": true, "noImplicitThis": true, "noImplicitAny": false, "strictNullChecks": true, "esModuleInterop": true, "typeRoots": ["./@types", "./node_modules/@types"], "resolveJsonModule": true, "types": ["node"] }, "exclude": ["__tests__", "node_modules", "dist", "*.d.ts", "*.d.cts"] } ```
:tsconfig.json
```json { "name": "@kilabs/devplatform-server-api", "type": "module", "private": true, "version": "0.0.1", "description": "The API monorepo for the devplatform", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "commit": "npx git-cz" }, "author": "Alexander Vassbotn Røyne-Helgesen
:package.json