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

Tho*_*mas 5 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 在此处输入图片说明
  2. 如果我更正bundle.js路径,它仍然为资产设置了错误的路径: 在此处输入图片说明

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

这是我当前webpack.production.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
    }),
  ]
}
Run Code Online (Sandbox Code Playgroud)

以防万一,这就是我当前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
    })
  ]
}
Run Code Online (Sandbox Code Playgroud)

任何想法我做错了什么?

小智 0

面临类似的问题。output.publicPath: "/"webpack.dev.js 和webpack.prod.js 中的设置output.publicPath: "./"对我有用。