我有一个带有 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 并设置 circleci。Circleci 完美地为我工作。