V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
wdssmq

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

  •  
  •   wdssmq · Feb 16, 2023 · 2441 views
    This topic created in 1176 days ago, the information mentioned may be changed or developed.

    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

    5 replies    2023-02-17 17:03:22 +08:00
    zhyd1997
        1
    zhyd1997  
       Feb 17, 2023
    老哥有好用的解决办法吗
    wdssmq
        2
    wdssmq  
    OP
       Feb 17, 2023
    @YadongZhang 我贴的方法确实有用的样子,虽然需要额外安装三个包 - -
    wdssmq
        3
    wdssmq  
    OP
       Feb 17, 2023
    @YadongZhang 好吧。。发现你就是给出这个方法的人。。
    learningman
        4
    learningman  
       Feb 17, 2023
    rollup 不是有个 json 插件吗,用那个试试,就不用改 import
    wdssmq
        5
    wdssmq  
    OP
       Feb 17, 2023
    @learningman 我是给 rollup.config.js 文件自身引入 pkg ,插件好像管的是 input - -
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5841 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 81ms · UTC 01:51 · PVG 09:51 · LAX 18:51 · JFK 21:51
    ♥ Do have faith in what you're doing.