javascript - 使用 html-webpack-plugin 配置 webpack

标签 javascript webpack

我正在使用带有build设置的 HTML-Webpack-plugin:

build: {
  env: require('./prod.env'),
  index: path.resolve(__dirname, '../app/templates/dist/index.html'),
  assetsRoot: path.resolve(__dirname, '../app/static'),
  assetsSubDirectory: 'dist'

应用结构:

app-
    static-
        dist-
            css- minified css files
            js- minified js files
    templates-
        dist-
            index.html

当使用 npm run build 构建输出时,输出 index.html 文件如下所示:

<!DOCTYPE html><html><head><meta charset=utf-8><title>app</title><link href=/dist/css/app.22f236.css

但是,对于 index.html 中的所有路径,我希望 href 路径在/dist 之前包含/static,即

/static/dist/css...  

我的 webpack.prod.conf.js:

var path = require('path')
var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')

var env = process.env.NODE_ENV === 'testing'
  ? require('../config/test.env')
  : config.build.env

var webpackConfig = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({
      sourceMap: config.build.productionSourceMap,
      extract: true
    })
  },
  devtool: config.build.productionSourceMap ? '#source-map' : false,
  output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].[chunkhash].js'),
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  },
  plugins: [
    // http://vuejs.github.io/vue-loader/en/workflow/production.html
    new webpack.DefinePlugin({
      'process.env': env
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      },
      sourceMap: true
    }),
    // extract css into its own file
    new ExtractTextPlugin({
      filename: utils.assetsPath('css/[name].[contenthash].css')
    }),
    // Compress extracted CSS. We are using this plugin so that possible
    // duplicated CSS from different components can be deduped.
    new OptimizeCSSPlugin({
      cssProcessorOptions: {
        safe: true
      }
    }),
    // generate dist index.html with correct asset hash for caching.
    // you can customize output by editing /index.html
    // see https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: process.env.NODE_ENV === 'testing'
        ? 'index.html'
        : config.build.index,
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
    // keep module.id stable when vender modules does not change
    new webpack.HashedModuleIdsPlugin(),
    // split vendor js into its own file
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks: function (module, count) {
        // any required modules inside node_modules are extracted to vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(
            path.join(__dirname, '../node_modules')
          ) === 0
        )
      }
    }),
    // extract webpack runtime and module manifest to its own file in order to
    // prevent vendor hash from being updated whenever app bundle is updated
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',
      chunks: ['vendor']
    }),
    // copy custom static assets
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static'),
        to: config.build.assetsSubDirectory,
        ignore: ['.*']
      }
    ])
  ]
})

if (config.build.productionGzip) {
  var CompressionWebpackPlugin = require('compression-webpack-plugin')

  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(' +
        config.build.productionGzipExtensions.join('|') +
        ')$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  )
}

if (config.build.bundleAnalyzerReport) {
  var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}

module.exports = webpackConfig

我有什么办法可以更改 index.html 中的注入(inject)路径以在每个路径前面包含一个父文件夹? (除了编写 bash 脚本来执行此操作)。

最佳答案

编译 Assets 的输出路径不依赖于您可以使用 html-webpack-plugin 提供的设置(除了 HTML 文件本身)。您将必须配置编译器输出路径和模块规则,以确保 Assets 在所需的公共(public)路径上发出。

关于javascript - 使用 html-webpack-plugin 配置 webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46618376/

相关文章:

Javascript 没有将图像对象从数组加载到 Canvas 上

javascript - 如何获取表单中输入的所有值

javascript - 我的新 HTML 元素正在以指数速度创建 WTF Javascript 和 jQuery

css - 防止字体嵌入到 webpack 生成的 css 中

angular - 将非核心 npm 库导入 Angular 2 App

javascript - PHP文件中的iframe恶意代码?

javascript - React/Javascript If 语句在 map 内

javascript - 仅从 ES6 模块导入静态变量

Webpack - SASS/SCSS '@import' 语句中的字符串替换

javascript - Vue - 删除生产模式的特定 JavaScript 文件