代码拆分的动态导入原因:ESLint解析错误'导入'

时间:2017-12-14 14:30:35

标签: vue.js vuejs2 babel eslint vue-router

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

示例路线:

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

示例错误:

  

[eslint]解析错误:意外的令牌导入

我已经按照Webpack动态导入部分提供的步骤,以及Anthony Gore关于如何在路由器级别使用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时,我的代码运行并按预期工作,但此解析错误会阻止文件的其余部分被删除,我无法抑制它。

感谢任何/所有帮助!

2 个答案:

答案 0 :(得分:15)

.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.json



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




答案 1 :(得分:2)

这个StackOverflow的问题解答确实帮助我解决了这个问题,但是目前在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"]
            }
        }
    }
}

如果您使用的是Webpack别名,则只需要settings键即可。

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

如果您在不使用Laravel Mix的情况下“正常”使用Webpack,则此处的语法会有所不同,但是,如果您使用Webpack别名,则会发现此有用:

// 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