推荐关注
› 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 · 2917 views
    This topic created in 1356 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
    zhyd1997
    Feb 17, 2023
    1
    老哥有好用的解决办法吗
    wdssmq
    wdssmq
    OP
    Feb 17, 2023
    2
    @YadongZhang 我贴的方法确实有用的样子,虽然需要额外安装三个包 - -
    wdssmq
    wdssmq
    OP
    Feb 17, 2023
    3
    @YadongZhang 好吧。。发现你就是给出这个方法的人。。
    learningman
    learningman
    Feb 17, 2023
    4
    rollup 不是有个 json 插件吗,用那个试试,就不用改 import
    wdssmq
    wdssmq
    OP
    Feb 17, 2023
    5
    @learningman 我是给 rollup.config.js 文件自身引入 pkg ,插件好像管的是 input - -
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4965 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 03:50 · PVG 11:50 · LAX 19:50 · JFK 22:50
    ♥ Do have faith in what you're doing.