Node.JS Typescript Rollup 错误:意外的标记(请注意,您需要插件来导入非 JavaScript 文件

Kok*_*Teh 7 plugins node.js node-modules typescript rollupjs

NodeJS:v18.7.0

\n

NPM:8.15.0

\n

rollup.config.js

\n
import typescript from '@rollup/plugin-typescript';\nimport dts from 'rollup-plugin-dts';\n\nimport tsConfig from './tsconfig.json';\n\nconst config = [\n    {\n        input: "index.ts",\n        output: [{ file: "build/index.js", sourcemap: true }],\n        plugins: [\n            typescript(\n                {\n                    sourceMap: tsConfig.compilerOptions.sourceMap\n                }\n            )\n        ]\n    },\n    {\n        input: 'build/index.d.ts',\n        output: [{ file: "build/index.d.ts", "format": "es" }],\n        plugins: [\n            dts(\n                {\n                    compilerOptions: {\n                        baseUrl: tsConfig.compilerOptions.baseUrl\n                    }\n                }\n            )\n        ]\n    },\n]\n\nexport default config;\n
Run Code Online (Sandbox Code Playgroud)\n

tsconfig.json

\n
{\n   "compilerOptions": {\n        "plugins": [\n            {\n                "name": "@rollup/plugin-typescript"\n            }\n        ]\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

package.json

\n
{\n  "devDependencies": {\n    "@rollup/plugin-typescript": "^8.3.4",\n    "rollup": "^2.77.2",\n    "rollup-plugin-dts": "^4.2.2",\n    "ts-node": "^10.9.1",\n    "tslib": "^2.4.0",\n    "typescript": "^4.7.4"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
$ n run build\n\n> webapi@1.0.0 build\n> tsc --project tsconfig.build.json && npm run rollup\n\n\n> webapi@1.0.0 rollup\n> rollup -c\n\n\nindex.ts \xe2\x86\x92 build/index.js...\n(!) Unresolved dependencies\nhttps://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency\nurl (imported by index.ts)\npath (imported by index.ts)\nhttp-errors (imported by index.ts)\nexpress (imported by index.ts, routes/index.ts, routes/healthchecks.ts, routes/api.ts)\ncompression (imported by index.ts)\nhelmet (imported by index.ts)\ncors (imported by index.ts)\ncookie-parser (imported by index.ts)\nmorgan (imported by index.ts)\n[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)\n../infrastructure/db.ts (5:9)\n3: import config from 'config'\n4: export class Database {\n5:   private static _pool: Pool;\n             ^\n6:   static init() {\n7:     try {\nError: Unexpected token (Note that you need plugins to import files that are not JavaScript)\n    at error (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:198:30)\n    at Module.error (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:12543:16)\n    at Module.tryParse (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:12920:25)\n    at Module.setSource (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:12825:24)\n    at ModuleLoader.addModuleSource (/usr/src/Node.JSRestAPI/node_modules/rollup/dist/shared/rollup.js:22267:20)\n\nnpm ERR! Lifecycle script `rollup` failed with error: \nnpm ERR! Error: command failed \nnpm ERR!   in workspace: webapi@1.0.0 \nnpm ERR!   at location: /usr/src/Node.JSRestAPI/src/webapi \nnpm ERR! Lifecycle script `build` failed with error: \nnpm ERR! Error: command failed \nnpm ERR!   in workspace: webapi@1.0.0 \nnpm ERR!   at location: /usr/src/Node.JSRestAPI/src/webapi \n
Run Code Online (Sandbox Code Playgroud)\n

Mor*_*ler 2

遇到同样的问题,显然@rollup/plugin-typescript已经坏了。

切换到rollup-plugin-typescript为我解决问题。


更新:我切换回@rollup/plugin-typescript不久前,问题不再出现(使用v10.0.1)。不管是什么,他们似乎已经修复了它。

  • `rollup-plugin-typescript` 已被[弃用](https://www.npmjs.com/package/rollup-plugin-typescript),转而使用 `@rollup/plugin-typescript`。 (2认同)