Dynamic Imports for Code Splitting 原因:ESLint 解析错误 'import'

Dynamic Imports for Code Splitting cause: ESLint Parsing Error 'import'

我正在使用此处找到的 VueJS Webpack 模板:https://github.com/vuejs-templates/webpack

示例路线:

const AuthRoute = () => import(/* webpackChunkName: "authroute" */ './AuthRoute.vue')

示例错误:

[eslint] Parsing error: Unexpected token import

我已经按照 Webpack 的动态导入部分中提供的步骤以及 Anthony Gore 的博客 post 了解如何在路由器级别使用 VueJS 完成代码拆分。更具体地说,这是我的设置:

Package.json

...
"babel-core": "^6.22.1",
"babel-eslint": "^8.0.3",
"babel-jest": "^21.2.0",
"babel-loader": "^7.1.1",
"babel-plugin-dynamic-import-webpack": "^1.0.2",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.24.1",
"eslint": "^4.13.1"
...

.babelrc

{
  "presets": [
    ["env", {
      "modules": false
    }],
    "stage-2"
  ],
  "plugins": [
    "dynamic-import-webpack",
    "syntax-dynamic-import",
    "transform-runtime"
  ],
  "env": {
    "test": {
      "presets": ["env", "stage-2"]    }
  }
}

.eslintrc.js

parserOptions: {
  parser: 'babel-eslint',
  sourceType: 'module',
  allowImportExportEverywhere: true
}

我有点不知所措,为什么我仍然看到这个错误。使用 npm run devnpm run build 时,我的代码按预期运行和工作,但此解析错误阻止文件的其余部分被检查,我无法抑制它。

Any/all 感谢帮助!

.eslintrc.js

parserOptions: {
  parser: 'babel-eslint',
  sourceType: 'module',
  allowImportExportEverywhere: true
}

应该是:

parser: 'babel-eslint',
parserOptions: {
  sourceType: 'module',
  allowImportExportEverywhere: true
}

来源:https://eslint.org/docs/user-guide/configuring#specifying-parser

与 (@vue/cli) .eslintrc.js上

{
  "parser": "vue-eslint-parser",
  "parserOptions": {
    "parser": "babel-eslint",
    "ecmaVersion": 8,
    "sourceType": "module"
  }
}

这个 Whosebug 问题和答案确实帮助我解决了这个问题,但目前在 2020 年 4 月,parser 密钥似乎需要在 parserOptions 中,或者至少对于我的设置组合。

我将展示我使用 Laravel 7/Laravel Mix 和 Vue 2.6~ 的当前配置。

.eslintrc.json

{
    "env": {
        "browser": true,
        "es6": true,
        "node": true
    },
    "extends": [
        "eslint:recommended",
        "airbnb-base",
        "plugin:vue/essential"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "parser": "babel-eslint",
        "ecmaVersion": 2019,
        "sourceType": "module"
    },
    "plugins": [
        "vue"
    ],
    "rules": {
        // dat ruleset
    },
    "settings": {
        "import/resolver": {
            "alias": {
                "map": [
                    ["@", "./resources"]
                ],
                "extensions": [".vue"]
            }
        }
    }
}

You'll only need that settings key if you are using Webpack aliases.

package.json

   "devDependencies": {
        "@babel/plugin-syntax-dynamic-import": "^7.8.3",
        "babel-eslint": "^10.1.0",
        "eslint": "^6.8.0",
        "eslint-config-airbnb-base": "^14.1.0",
        "eslint-import-resolver-alias": "^1.1.2",
        "eslint-plugin-import": "^2.20.2",
        "eslint-plugin-vue": "^6.2.2",
        "laravel-mix": "^5.0.1",
    }

.babelrc

{
    "plugins": ["@babel/plugin-syntax-dynamic-import"]
}

webpack.config.js

If you are using Webpack "normally" without Laravel Mix, you will find your syntax is a little different here, but if you are using Webpack aliases, you will find this useful:

// use named JS bundles
mix.config.webpackConfig.output = {
    chunkFilename: 'js/[name].bundle.js',
    publicPath: '/',
};

// alias the ~/resources folder
mix.webpackConfig({
    resolve: {
        extensions: ['.js', '.vue'],
        alias: {
            '@': `${__dirname}/resources`,
        },
    },
});

最后说明:我总是推荐使用 airbnb-base 配置并依靠它来形成 lint 规则的基础,因为它是为多开发人员环境创建的 运行 顽固的函数式编程不可变、单向数据流的技术——因此,函数式响应式编程,同时避免 JavaScript 的陷阱......并专注于声明性代码,同时避免相当糟糕的命令式代码。

我在关于在 Vue 中为 Airbnb 设置 ES Lint 的文章中写了更多关于此的内容:https://medium.com/@agm1984/how-to-set-up-es-lint-for-airbnb-vue-js-and-vs-code-a5ef5ac671e8

将以下代码放入.eslintrc。它需要解析器选项;默认为 2013:

"parserOptions": {
    "ecmaVersion":"latest"
  },