简体   繁体   English

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

[英]I have generated the bundle.js with webpack, but my React App continue using /static/bundle.js

I am generating the .js and .js.gz in the /dist folder, but really I need that this files will be in the /build/static/js我正在 /dist 文件夹中生成 .js 和 .js.gz,但我真的需要这些文件将在 /build/static/js 中

When I upload to production my react app, the only files that I upload to the server is the build folder, so the webpack generated files aren't taken in consideration.当我将 React 应用程序上传到生产环境时,我上传到服务器的唯一文件是 build 文件夹,因此不会考虑 webpack 生成的文件。

This is the .js file that use my app when is running (/static/js/) and not the webpack generated.这是运行时使用我的应用程序的 .js 文件 (/static/js/) 而不是生成的 webpack。

在此处输入图片说明

Do you know how could I force to webpack to create the .js and .js.gz files inside the /build folder?你知道我怎么能强制 webpack 在 /build 文件夹中创建 .js 和 .js.gz 文件吗?

This is my webpack.config.js:这是我的 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
     })
    ]

}

And this is the script section in package.json这是 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"
  },

Your bundled files will be going to the path mentioned to the output variable.您的捆绑文件将转到输出变量提到的路径。

Change this line from: path: path.resolve(__dirname, 'dist') to: path: path.resolve(__dirname, 'build/static/js')将此行从: path: path.resolve(__dirname, 'dist') 更改为: path: path.resolve(__dirname, 'build/static/js')

I have seen that, If you have both versions of the file:我已经看到了,如果你有两个版本的文件:

main.js main.js.gz main.js main.js.gz

The browser ask by the gzip version and the server send only the gzip one, But you have to have the two versions!浏览器要求 gzip 版本,服务器只发送 gzip 一个,但你必须有两个版本!

thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM