(function (exports, require, module, __filename, __dirname) { import path from 'path'
^^^^^^
SyntaxError: Unexpected token import当我使用webpack-dev-server --hot时会发生此错误。
这似乎是因为它看不懂import或者webpack不支持import。我试着使用babel-register,但它不起作用。有办法解决这个问题吗?请参阅下面的代码。
webpack.config.babel.js
import path from 'path'
import webpack from 'webpack'
import HtmlPlugin from 'html-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
const vueLoaders = {
html: 'pug-loader',
css: ExtractTextPlugin.extract({
use: 'css-loader',
fallback: 'vue-style-loader'
}),
scss: 'vue-style-loader!css-loader!sass-loader',
sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}
export default {
entry: './client/main.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new ExtractTextPlugin('bundle.css'),
new HtmlPlugin({
title: 'sex',
template: 'client/assets/index.pug'
})
],
module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: 'css-loader', fallback: 'style-loader'
})
}, {
test: /\.s[a|c]ss$/,
use: ExtractTextPlugin.extract({
use: ['css-loader', 'sass-loader'], fallback: 'style-loader'
})
}, {
test: /\.pug$/,
loader: 'pug-loader'
}, {
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
}, {
test: /\.vue$/,
loader: 'vue-loader',
options: { loaders: vueLoaders }
}, {
test: /\.(png|jpe?g|gif|svg|ttf|woff2?|eot)$/,
loader: 'file-loader',
options: { name: '[name].[ext]?[hash]' }
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.common.js'
}
},
devServer: {
host: '0.0.0.0',
historyApiFallback: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': { NODE_ENV: '"production"' }
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: { warnings: false }
}),
new webpack.LoaderOptionsPlugin({ minimize: true })
])
}发布于 2017-04-02 04:50:17
节点目前不支持ES6 import语法。同时使用CommonJS require语法。
const path = require('path')
const webpack = require('webpack')
const HtmlPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')发布于 2017-04-02 06:16:27
babel-register只使用调用require("babel-register");的babel来转换所需的模块,而不是模块本身。
您可以使用中间步骤使这个工作与Webpack的配置。
webpack.config.js
require('babel-register');
module.exports = require('./webpack.config.babel.js');webpack.config.babel.js
import path from 'path'
import webpack from 'webpack'
import HtmlPlugin from 'html-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
const vueLoaders = {
html: 'pug-loader',
css: ExtractTextPlugin.extract({
...发布于 2017-08-12 12:45:27
您已经创建了一个webpack.config.js,并且在绑定以执行webpack时,您将得到高于错误的结果。因为您的webpack配置文件必须是babelified才能使用,
1)将webpack.config.js重命名为webpack.config.babel.js。
2)现在创建一个新文件webpack.config.js,只需使用下面的2行
require('babel-register');
module.exports = require('./webpack.config.babel.js');3)创建一个与您的.babelrc文件并行的webpack.config.js文件,内容如下。我们需要明确地告诉babel要使用什么预置。
{
"presets": ["latest",'react', 'es2015','stage-2']
}4)向依赖项中添加以下节点模块(.babelrc中使用的预置所需的)
npm install babel-preset-env babel-preset-es2015 babel-preset-stage-2 babel-preset-latest babel-preset-react --save-dev5)你完蛋了。现在,如果您执行webpack --progress --colors --watch,它应该可以工作。
https://stackoverflow.com/questions/43164716
复制相似问题