1

After ci deployment, I try to navigate to '/records/:id' or other routes with 2 or more path elements and will get this error in the console. This error never comes out when running in local.

Uncaught SyntaxError: Unexpected token '<'

I had used webpack for configuration and connected-react-router as router in my project.

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',
    })
  ]
})

index.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')
);

package.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"
  },
...

May I know this error is caused by the configuration in webpack or router?

chingcong
  • 11
  • 1

0 Answers0