CommonJS导入const webpack = require('webpack');
工作正常,但ES6 import webpack from 'webpack';
没有。
webpack的第2版本身支持ES6模块语法。
但它对我来说并不开箱即用。
我也尝试过:
babel-loader
用于JS文件; 但它一切都不起作用。
的package.json
"scripts": {
"build:dev": "webpack --config webpack.config.dev.babel"
},
"devDependencies": {
"babel-loader": "^7.1.4",
"webpack": "^4.8.3",
"webpack-cli": "^2.1.3",
"webpack-merge": "^4.1.2"
}
webpack.config.common.babel.js
export const /* in this implied like default */ module = {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: '/node_modules/'
}
],
};
webpack.config.dev.babel.js
import webpack from 'webpack';
import merge from 'webpack-merge';
import commonConfig from './webpack.config.common.babel';
export default merge(commonConfig, {
mode: 'development',
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development')
})
]
});
.babelrc
{
"presets": ["es2015"]
}
当我输入npm run build:dev
时,它会抛出:
import webpack from 'webpack';
^^^^^^
SyntaxError: Unexpected token import
问题:
babel-loader
包裹?