使用动态导入时出现意外令牌 System.import
Unexpected token when using dynamic import instead System.import
重写我对 import
的所有动态 System.import
调用后,webpack 编译失败:
Module build failed: SyntaxError: (...)/dashboard.js: Unexpected token (7:4)
5 |
6 | export default Promise.all([
> 7 | import('charts')
| ^
我的 webpack.config.js
:
module:
{
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
(...)
}
.babelrc
:
{
"presets": ["es2015"],
"plugins": ["syntax-dynamic-import", "transform-runtime"]
}
package.json
:
"devDependencies": {
"autoprefixer": "~6.5.3",
"babel": "^6.5.2",
"babel-cli": "~6.18.0",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.18.0",
"node-sass": "^4.5.3",
"noty": "~2.4.0",
"path": "~0.12.7",
"promise-polyfill": "~6.0.2",
"resolve-url-loader": "~1.6.0",
"sass-loader": "~4.0.2",
"style-loader": "~0.13.1",
"stylelint": "~7.6.0",
"tag-it": "~2.0.0",
"url-loader": "~0.5.7",
"webpack": "^3.0.0",
"webpack-dev-server": "~2.2.1",
"webpack-uglify-js-plugin": "~1.1.9",
"whatwg-fetch": "~2.0.2"
},
你也必须像dynamic-import-webpack
一样导入变压器。这些插件已经包含所需的语法作为依赖项,因此您的配置可以很简单:
{
"presets": ["es2015"],
"plugins": ["dynamic-import-webpack", "transform-runtime"]
}
原来我的 .babelrc
被忽略了,因为这个文件(和 webpack)在我项目的根目录之外。我通过将 webpack.config.js
中的配置文件路径添加到 babel-loader
:
来修复它
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader?extends='+path.resolve('./.babelrc')
}
安装这个 babel 插件 https://github.com/airbnb/babel-plugin-dynamic-import-node
in .babelrc
{
"plugins": ["dynamic-import-node"]
}`
重写我对 import
的所有动态 System.import
调用后,webpack 编译失败:
Module build failed: SyntaxError: (...)/dashboard.js: Unexpected token (7:4)
5 |
6 | export default Promise.all([
> 7 | import('charts')
| ^
我的
webpack.config.js
:
module:
{
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
(...)
}
.babelrc
:
{
"presets": ["es2015"],
"plugins": ["syntax-dynamic-import", "transform-runtime"]
}
package.json
:
"devDependencies": {
"autoprefixer": "~6.5.3",
"babel": "^6.5.2",
"babel-cli": "~6.18.0",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.18.0",
"node-sass": "^4.5.3",
"noty": "~2.4.0",
"path": "~0.12.7",
"promise-polyfill": "~6.0.2",
"resolve-url-loader": "~1.6.0",
"sass-loader": "~4.0.2",
"style-loader": "~0.13.1",
"stylelint": "~7.6.0",
"tag-it": "~2.0.0",
"url-loader": "~0.5.7",
"webpack": "^3.0.0",
"webpack-dev-server": "~2.2.1",
"webpack-uglify-js-plugin": "~1.1.9",
"whatwg-fetch": "~2.0.2"
},
你也必须像dynamic-import-webpack
一样导入变压器。这些插件已经包含所需的语法作为依赖项,因此您的配置可以很简单:
{
"presets": ["es2015"],
"plugins": ["dynamic-import-webpack", "transform-runtime"]
}
原来我的 .babelrc
被忽略了,因为这个文件(和 webpack)在我项目的根目录之外。我通过将 webpack.config.js
中的配置文件路径添加到 babel-loader
:
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader?extends='+path.resolve('./.babelrc')
}
安装这个 babel 插件 https://github.com/airbnb/babel-plugin-dynamic-import-node
in .babelrc
{
"plugins": ["dynamic-import-node"]
}`