迁移到Webpack 2后,使用gulp运行webpack时使用webpack-stream似乎会导致babel或webpack抛出错误(无法确定错误的来源)。使用下面的配置和结构运行webpack
可以成功构建,但通过gulp和webpack-stream进行管道处理会导致此错误:
Message:
./app/app.jsx
Module parse failed: /Users/schne482/Code/tralgo/app/app.jsx Unexpected token (11:1)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (11:1)
at Parser.pp$4.raise (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp.unexpected (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:603:10)
at Parser.pp$3.parseExprAtom (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:1822:12)
at Parser.pp$3.parseExprSubscripts (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:1715:21)
at Parser.pp$3.parseMaybeUnary (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:1692:19)
at Parser.pp$3.parseExprOps (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:1637:21)
at Parser.pp$3.parseMaybeConditional (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:1597:21)
at Parser.pp$3.parseParenAndDistinguishExpression (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:1861:32)
at Parser.pp$3.parseExprAtom (/Users/schne482/Code/tralgo/node_modules/webpack-stream/node_modules/acorn/dist/acorn.js:1796:19)
Details:
domain: [object Object]
domainThrown: true
我已经完成的工作:
- 更改了
webpack.config.js
的结构以遵守Webpack 2规范。 - 将babel配置移到单独的
.babelrc
文件中。没有看到任何效果。 - 确保
.babelrc
文件的结构正确。 - 确保Webpack指向正确的入口文件(
./app/app.jsx
)并由加载器处理该入口文件。 - 使用相同的
.babelrc
和依赖项运行babel (babel ./app/app.jsx
),不使用Webpack进行操作。输出是正确的,没有错误。 - 在没有gulp的情况下运行Webpack,输出是正确的。
- 更新相关软件包(babel-core、babel-loader、babel预设、webpack-stream、gulp等)。
- 检查是否有任何简单的语法错误。
以下是相关文件:
gulpfile.js
:
const gulp = require('gulp');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const webpack_config = require('./webpack.config');
function webpack_build_dev() {
webpack_config.devtool = 'eval-source-map';
return gulp.src('app/app.jsx')
.pipe(webpackStream(webpack_config), webpack)
.pipe(gulp.dest('./'));
}
gulp.task('webpack:build:dev', gulp.series('clean', webpack_build_dev));
./app/app.jsx
:
import React from 'react';
import { render } from 'react-dom';
const app = (
<h1>Hello</h1>
);
render(
app,
document.getElementById('app')
);
./webpack.config.js
:
const webpack = require('webpack');
const path = require('path');
module.exports = {
context: path.resolve(__dirname, './app'),
entry: './app.jsx',
output: {
path: path.resolve(__dirname, './public'),
filename: 'bundle.js',
},
resolve: {
modules: [
path.resolve(__dirname, './app'),
"node_modules",
],
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules)/,
use: 'babel-loader',
},
],
},
};
./.babelrc
:
{
"presets": [
"es2015",
"react"
]
}
./package.json
(相关的 devDependencies):
{
"devDependencies": {
"babel-core": "^6.22.1",
"babel-loader": "^6.3.2",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"webpack": "^2.2.1",
}
}