我已经使用webpack生成bundle.js,但是我的React App继续使用/static/bundle.js

问题描述 投票:1回答:2

我正在/ dist文件夹中生成.js和.js.gz,但实际上我需要此文件位于/ build / static / js中

[当我上载到我的react应用程序时,我上载到服务器的唯一文件是build文件夹,因此不考虑webpack生成的文件。

这是正在运行(/ static / js /)时使用我的应用程序的.js文件,而不是生成的Webpack。

enter image description here

您知道如何强制Webpack在/ build文件夹中创建.js和.js.gz文件吗?

这是我的webpack.config.js:

var path = require('path');
const CompressionPlugin = require('compression-webpack-plugin');

module.exports = {
  mode: 'production',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
    },
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
      test: /\.svg$/,
      loader: 'svg-inline-loader'
      }
    ]
  },
  plugins: [
     new CompressionPlugin({
     algorithm: 'gzip',
     test: /\.js$|\.css$|\.html$/,
     threshold: 10240,
     minRatio: 0.8
     })
    ]

}

这是package.json中的脚本部分

  "scripts": {
    "analyze": "source-map-explorer 'build/static/js/*.js'",
    "start": "react-scripts start",
    "dev": "webpack --mode development",
    "build": "webpack --mode production",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
javascript reactjs webpack bundle webpack-4
2个回答
0
投票

您的捆绑文件将转到output变量提到的路径。

将此行从以下位置更改:path:path.resolve(__ dirname,'dist')到:path:path.resolve(__ dirname,'build / static / js')


0
投票

我已经知道,如果您同时拥有该文件的两个版本:

main.jsmain.js.gz

浏览器按gzip版本询问,服务器仅发送一个gzip,但是您必须具有两个版本!

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.