import 引入 JSON 文件的问题 - rollup && eslint

2023-02-16 20:28:22 +08:00
 wdssmq

rollup 2.x 版本是可以直接这么写的:

// rollup.config.js
import pkg from "./package.json";

升级 3.15 后报错;

[!] TypeError: Module "./package.json" needs an import assertion of type "json"

修改后 eslint 又会报错:

// rollup.config.js
import pkg from "./package.json" assert { type: "json" };
// Parsing error: Unexpected token assert ← eslint

进一步解决:

cnpm i -D eslint @babel/core @babel/eslint-parser @babel/plugin-syntax-import-assertions
// .eslintrc.js
"parser": "@babel/eslint-parser",
"parserOptions": {
    "requireConfigFile": false,
    "babelOptions": {
        "plugins": [
            '@babel/plugin-syntax-import-assertions'
        ],
    },
    "ecmaVersion": "latest",
    "sourceType": "module",
},

就挺麻烦的 - -

· · · · · ·

「折腾」 ESLint 安装与使用_电脑网络_沉冰浮水:

https://www.wdssmq.com/post/20190917021.html

Support of assert {type: "json"} · Discussion #15305 · eslint/eslint:

https://github.com/eslint/eslint/discussions/15305#discussioncomment-2508948

1474 次点击
所在节点    JavaScript
5 条回复
YadongZhang
2023-02-17 13:01:19 +08:00
老哥有好用的解决办法吗
wdssmq
2023-02-17 13:53:14 +08:00
@YadongZhang 我贴的方法确实有用的样子,虽然需要额外安装三个包 - -
wdssmq
2023-02-17 13:57:35 +08:00
@YadongZhang 好吧。。发现你就是给出这个方法的人。。
learningman
2023-02-17 15:18:00 +08:00
rollup 不是有个 json 插件吗,用那个试试,就不用改 import
wdssmq
2023-02-17 17:03:22 +08:00
@learningman 我是给 rollup.config.js 文件自身引入 pkg ,插件好像管的是 input - -

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/916760

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX