reactjs - Webpack 生产构建文件路径已关闭

标签 reactjs webpack webpack-dev-server webpack-2

我正在运行此命令来尝试生成生产 webpack 版本:

rimraf ./build/* && webpack -p --progress --config webpack.production.js

但是,当我打开 build/index.html 时,它无法加载大量文件,因为位置已关闭。

  1. 它无法为 bundle.js 文件放置正确的位置。它像这样加载:/bundle.js。然而,bundle.js 文件实际上与构建文件夹中的 index.html 文件位于同一目录中,因此它应该像 ./bundle 一样加载它。 js enter image description here
  2. 如果我更正 bundle.js 路径,它仍然会为 Assets 放置错误的路由: enter image description here

有趣的是,当我运行:webpack-dev-server --inline --progress --config webpack.dev.js时,我的应用程序当前可以与 webpack 开发服务器配合使用。

这是我当前的 webpack.development.js 文件:

var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {  
  devtool: 'source-map',
  devServer: {
    historyApiFallback: true, // This will make the server understand "/some-link" routs instead of "/#/some-link"
  },
  entry: [
    './src/scripts' // This is where Webpack will be looking for the entry index.js file
  ],
  output: {
    path: path.join(__dirname, 'build'), // This is used to specify folder for producion bundle
    filename: 'bundle.js', // Filename for production bundle
    publicPath: '/'
  },
  resolve: {
    modules: [
      'node_modules', 
      'src',
      path.resolve(__dirname, 'src/scripts'),
      path.resolve(__dirname, 'node_modules')
    ], // Folders where Webpack is going to look for files to bundle together
    extensions: ['.jsx', '.js'] // Extensions that Webpack is going to expect
  },
  module: {
    // Loaders allow you to preprocess files as you require() or “load” them. 
    // Loaders are kind of like “tasks” in other build tools, and provide a powerful way to handle frontend build steps.
    loaders: [
      {
        test: /\.jsx?$/, // Here we're going to use JS for react components but including JSX in case this extension is preferable
        include: [
          path.resolve(__dirname, "src"),
        ],
        loader: ['react-hot-loader']
      },
      {
        loader: "babel-loader",

        // Skip any files outside of your project's `src` directory
        include: [
          path.resolve(__dirname, "src"),
        ],

        // Only run `.js` and `.jsx` files through Babel
        test: /\.jsx?$/,

        // Options to configure babel with
        query: {
          plugins: ['transform-runtime'],
          presets: ['es2015', 'stage-0', 'react'],
        }
      },
      {
          test: /\.scss$/,
          loaders: ['style-loader', 'css-loader', 'sass-loader']
      }
    ]
  },
  plugins: [
    new webpack.NoEmitOnErrorsPlugin(), // Webpack will let you know if there are any errors

    // Declare global variables
    new webpack.ProvidePlugin({
      React: 'react',
      ReactDOM: 'react-dom',
      _: 'lodash'
    }),

    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './src/index.html',
      hash: true
    }),

    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      },
      sourceMap: true
    }),
  ]
}

为了以防万一,我当前的 webpack.dev.js 文件如下所示:

var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {  
  devtool: 'cheap-module-source-map',
  devServer: {
    historyApiFallback: true, // This will make the server understand "/some-link" routs instead of "/#/some-link"
  },
  entry: [
    'babel-polyfill',
    'webpack-dev-server/client?http://127.0.0.1:8080/', // Specify the local server port
    'webpack/hot/only-dev-server', // Enable hot reloading
    './src/scripts' // This is where Webpack will be looking for the entry index.js file
  ],
  output: {
    path: path.join(__dirname, 'build'), // This is used to specify folder for producion bundle
    filename: 'bundle.js', // Filename for production bundle
    publicPath: '/'
  },
  resolve: {
    modules: [
      'node_modules', 
      'src',
      path.resolve(__dirname, 'src/scripts'),
      path.resolve(__dirname, 'node_modules')
    ], // Folders where Webpack is going to look for files to bundle together
    extensions: ['.jsx', '.js'] // Extensions that Webpack is going to expect
  },
  module: {
    // Loaders allow you to preprocess files as you require() or “load” them. 
    // Loaders are kind of like “tasks” in other build tools, and provide a powerful way to handle frontend build steps.
    loaders: [
      {
        test: /\.jsx?$/, // Here we're going to use JS for react components but including JSX in case this extension is preferable
        include: [
          path.resolve(__dirname, "src"),
        ],
        loader: ['react-hot-loader']
      },
      {
        loader: "babel-loader",

        // Skip any files outside of your project's `src` directory
        include: [
          path.resolve(__dirname, "src"),
        ],

        // Only run `.js` and `.jsx` files through Babel
        test: /\.jsx?$/,

        // Options to configure babel with
        query: {
          plugins: ['transform-runtime', 'transform-decorators-legacy'],
          presets: ['es2015', 'stage-0', 'react'],
        }
      },
      {
          test: /\.scss$/,
          loaders: ['style-loader', 'css-loader', 'sass-loader']
      }
    ]
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(), // Hot reloading
    new webpack.NoEmitOnErrorsPlugin(), // Webpack will let you know if there are any errors

    // Declare global variables
    new webpack.ProvidePlugin({
      React: 'react',
      ReactDOM: 'react-dom',
      _: 'lodash'
    }),

    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './src/index.html',
      hash: false
    })
  ]
}

知道我做错了什么吗?

最佳答案

面临类似的问题。在 webpack.dev.js 中设置 output.publicPath: "/" 和在 webpack.prod.js 中设置 output.publicPath: "./" ,对我来说是成功的。

关于reactjs - Webpack 生产构建文件路径已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424571/

相关文章:

Webpack4 不会对我的页面条目执行主模块

javascript - 条件渲染标记 (JSX) 与 CSS `display: none` - 哪个更好?

javascript - 重构三元运算符

reactjs - 不是来自 webpack 的内容由/foo 提供

javascript - 仅捆绑 React native 组件

vue.js - Vue cli 3 项目,图像路径中的动态 src 不起作用

javascript - Webpack 2 配置错误

javascript - react : Can not use HTML tags in Lerna

javascript - 使 Browserify bundle 与 React DevTools 兼容

webpack - 包含/排除不起作用