我有一个带有webpack的react应用。开发服务器工作正常。当我在本地webpack --mode production
上运行时,它会按预期构建并创建一个js捆绑包。当我将其推送到gitlab时,它无法生成并显示错误:
ERROR in ./src/index.js 20:16
Module parse failed: Unexpected token (20:16)
You may need an appropriate loader to handle this file type.
| firebase.initializeApp(config);
|
> ReactDOM.render(<App />, document.getElementById('app'));
|
这些是我的devDependencies:
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-transform-flow-strip-types": "^7.2.3",
"@babel/plugin-proposal-class-properties": "^7.2.3",
"@babel/plugin-proposal-decorators": "^7.2.3",
"@babel/plugin-proposal-object-rest-spread": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"babel-eslint": "8.2.6",
"babel-loader": "^8.0.4",
"copy-webpack-plugin": "4.6.0",
"css-loader": "0.28.11",
"eslint": "4.19.1",
"eslint-loader": "2.1.1",
"eslint-plugin-react": "7.11.1",
"html-webpack-plugin": "3.2.0",
"style-loader": "0.21.0",
"webpack": "^4.28.2",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.14"
}
这是我的webpack配置:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'build'),
filename: 'index.bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: [/node_modules/, /build/],
use: ['babel-loader', 'eslint-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
devServer: {
historyApiFallback: true
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
filename: 'index.html'
}),
new CopyWebpackPlugin([
{ from: path.join(__dirname, 'public/'), to: path.join(__dirname, 'build') }
])
]
};
这以前是可行的,但我更新的库已经过时了。更新后,gitlab ci失败。
这是构建配置:
image: node:latest
cache:
paths:
- node_modules/
stages:
- setup
- build
setup:
stage: setup
script:
- echo "Setup started"
- yarn install
artifacts:
paths:
- node_modules/
build:
stage: build
script:
- yarn build
artifacts:
paths:
- build/
编辑
我已经遍历了index.js,所以输入的内容可能不好,但是随后在组件内部出现了很多错误,无法解析jsx标签。我怀疑babel-loader
在gitlab-ci上不起作用。
编辑29.3.2019 我还没有找到解决方案。截至您可以在github上获得免费的私有存储库,然后将项目迁移到github和setup circleci。 Circleci完美地为我工作。
答案 0 :(得分:0)
添加
"react": "^16.4.1",
"react-dom": "^16.4.1"
到您的devDependencies
,它应该可以工作。
由于您没有在react
中提供react-dom
和package.json
模块,因此出现错误。
让我知道它是否对您有用,我们将期待这个问题
更新:
image: node:latest
cache:
paths:
- node_modules/
stages:
- setup
- build
setup:
stage: setup
script:
- echo "Setup started"
- yarn install
artifacts:
paths:
- node_modules/
build:
stage: build
script:
- yarn install react
- yarn install react-dom
- yarn build
artifacts:
paths:
- build/
直接将安装命令添加到脚本应该可以解决此问题。
您的webpack
不知何故找不到react
和react-dom
的安装目录,因此传递安装命令将在构建时显式提供该模块。
答案 1 :(得分:0)
我在Gitlab CI上遇到了同样的问题,发现排除是问题所在:
exclude: [/node_modules/, /build/]
尝试将其替换为以下内容:
exclude: '/node_modules/',
include: [
path.join(__dirname, 'src')
]
现在它可以在本地运行,也可以在Gitlab CI上运行。以前,它仅在本地工作。
还有另一个问题向我指出了正确的方向:Webpack build fails with Gitlab-CI