Webpack 4静态资产未使用HtmlWebpackPlugin重新加载

时间:2018-07-24 08:28:41

标签: webpack html-webpack-plugin webpack-4

我正在使用npm和HtmlWebpackPlugin配置一个简单的webpack 4设置来生成index.html,并且一切正常,除了它仅在运行生产版本时生成文件,并且模板更改不会自动反映在浏览器。

这是我的webpack.config.js文件

$contents

在package.json中,我有以下两个脚本:

const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

// Static data
const castingteamData = require('./castingteam.json');

module.exports = {
  entry: './src/app/app.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
  ..................................omitted loaders
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      data: castingteamData,
      template: 'src/app/index.html'
    })
  ],

  devServer: {
    contentBase: './dist',
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
};

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new CopyWebpackPlugin([
      {
        from: 'src/assets/img/',
        to: 'assets/img/',
        toType: 'dir'
      }
    ]),
    new CopyWebpackPlugin([
      {
        from: 'src/assets/fonts',
        to: 'assets/fonts',
        toType: 'dir'
      }
    ]),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

就像我说过的那样,在运行npm run build时正确生成了index.html,但是对于npm run dev却不满意。看来这应该很容易解决,但我找不到!

0 个答案:

没有答案