1

在 ci 部署之后,我尝试导航到 '/records/:id' 或具有 2 个或更多路径元素的其他路由,并将在控制台中收到此错误。在本地运行时,此错误永远不会出现。

Uncaught SyntaxError: Unexpected token '<'

在我的项目中,我使用webpack进行配置,并使用connected-react-router作为路由器。

webpack.config.js


const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

const config = {
  mode: 'development',
  entry: './src/index.tsx',
  output: {
    path: path.join(__dirname, 'public'),
    filename: '[name].[hash].js',
    publicPath: '/',
  },
  devtool: 'source-map',
  resolve: {
    extensions: ['.css', '.ts', '.tsx', '.js'],
  },
  devServer: {
    historyApiFallback: true,
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: [/node_modules/, /\.stories\.tsx$/],
        use: [
          {
            loader: 'babel-loader',
          },
        ],
      },
      {
        test: /\.css$/,
        use: [
          {loader: 'css-loader'}
        ]
      }
    ],
  },
    
  plugins: [
    new HtmlWebpackPlugin({
      template: 'static/index.template.html',
      filename: 'index.html',
    }),

    new CleanWebpackPlugin({cleanOnceBeforeBuildPatterns: ['**/*']}),
  ]
};

module.exports = config;

webpack.config.prod.js

const path = require('path');
const { merge } = require('webpack-merge');
const common = require('./webpack.config');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = merge(common, {
  mode: 'production',
  output: {
    filename: '[name].[hash].js',
    publicPath: './',
    path: path.join(__dirname, 'public'),
  },
  devtool: false,
  performance: {
    hints: false,
    maxEntrypointSize: 512000,
    maxAssetSize: 512000
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'static/index.template.html',
      filename: 'index.html',
    })
  ]
})

索引.tsx

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <Switch>
        <Route exact path="/" component={Dashboard} />
        <Route exact path="/records" component={Records} />
        <Route path="/records/:id" component={Record} />
        <Route component={NotFound} />
      </Switch>
    </ConnectedRouter>
  </Provider>,
  document.getElementById('root')
);

包.json

...
  "scripts": {
    "type-check:watch": "npm run type-check -- --watch",
    "build": "webpack --progress --config webpack.config.prod.js",
    "start": "webpack-dev-server --port 4001 --progress --colors --open --mode=development",
    "watch:css": "postcss src/styles/tailwind.css -o src/styles/main.css"
  },
...

我可以知道这个错误是由 webpack 或路由器中的配置引起的吗?

4

0 回答 0