Svelte (rollup) - Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)

Svelte (rollup) - Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)

什么都没做来触发这个错误。该应用程序前一秒还可以,下一秒就不行了。

为什么会这样?这不是由于缺少 @rollup/plugin-json 插件,因为它以前没有它也能正常工作。

错误

https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
path (imported by  path?commonjs-external)
http (imported by  http?commonjs-external)
net (imported by  net?commonjs-external)
url (imported by  url?commonjs-external)
[!] Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
node_modules/mime-db/db.json (2:40)
1: {
2:   "application/1d-interleaved-parityfec": {
                                           ^
3:     "source": "iana"
4:   },
Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
    at error (/mnt/c/ivan/dev/lab/atlas-biotek/node_modules/rollup/dist/shared/rollup.js:5265:30)
    at Module.error (/mnt/c/ivan/dev/lab/atlas-biotek/node_modules/rollup/dist/shared/rollup.js:9835:16)
    at tryParse (/mnt/c/ivan/dev/lab/atlas-biotek/node_modules/rollup/dist/shared/rollup.js:9716:23)
    at Module.setSource (/mnt/c/ivan/dev/lab/atlas-biotek/node_modules/rollup/dist/shared/rollup.js:10142:19)
    at ModuleLoader.addModuleSource (/mnt/c/ivan/dev/lab/atlas-biotek/node_modules/rollup/dist/shared/rollup.js:18312:20)

添加插件

npm i @rollup/plugin-json --save-dev

rollup.js.config

import json from "@rollup/plugin-json";

export default {
    plugins: [
        commonjs(),
        json(),      // <---- put after commonjs
    ]
}

客户端错误

Uncaught ReferenceError: require$[=13=] is not defined
    at main.js:5
(anonymous) @ main.js:5

问题是由于自动完成,我在客户端导入了一个节点模块。

import { is } from "express/lib/request";

如果你使用的是 typescript,我在 typescript 之后添加了 json() 插件:

文件:rollup.config.js

import typescript from "rollup-plugin-typescript2";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import replace from "@rollup/plugin-replace";
import json from "@rollup/plugin-json";


const plugins = [
  typescript({
    tsconfig: "./tsconfig-build.json",
  }),
  json(), <<------------- HERE
  resolve(),
  commonjs(),
  replace({
    ...
    preventAssignment: true,
  }),
];