我有点卡住了
我有一个脚本,可以在加载时以编程方式将清单,供应商和应用程序注入页面。 我需要在构建过程中将此脚本注入到html中,但在开发过程中不需要这样做。
这是我的prod配置(默认情况下,有一些非常小的变化) -
'use strict'
const path = require('path')
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const env = process.env.NODE_ENV === 'testing'
? require('../config/test.env')
: require('../config/prod.env')
const webpackConfig = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({
sourceMap: config.build.productionSourceMap,
extract: true,
usePostCSS: true
})
},
devtool: config.build.productionSourceMap ? config.build.devtool : false,
output: {
path: config.build.assetsRoot,
filename: utils.assetsPath('js/[name].js'),
chunkFilename: utils.assetsPath('js/[id].js')
},
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
'process.env': env
}),
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false
}
},
sourceMap: config.build.productionSourceMap,
parallel: true
}),
// extract css into its own file
new ExtractTextPlugin({
filename: utils.assetsPath('css/[name].css'),
// set the following option to `true` if you want to extract CSS from
// codesplit chunks into this main css file as well.
// This will result in *all* of your app's CSS being loaded upfront.
allChunks: false,
}),
// Compress extracted CSS. We are using this plugin so that possible
// duplicated CSS from different components can be deduped.
new OptimizeCSSPlugin({
cssProcessorOptions: config.build.productionSourceMap
? { safe: true, map: { inline: false } }
: { safe: true }
}),
// copy custom static assets
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
}
]),
// 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: false,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
}),
new AddAssetHtmlPlugin({
filepath: require.resolve('../static/js/init.js'),
publicPath: '/static/js/init.js',
includeSourcemap: false
}),
// keep module.id stable when vender modules does not change
new webpack.HashedModuleIdsPlugin(),
// enable scope hoisting
new webpack.optimize.ModuleConcatenationPlugin(),
// split vendor js into its own file
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks (module) {
// 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',
minChunks: Infinity
}),
// This instance extracts shared chunks from code splitted chunks and bundles them
// in a separate chunk, similar to the vendor chunk
// see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
new webpack.optimize.CommonsChunkPlugin({
name: 'app',
async: 'vendor-async',
children: true,
minChunks: 3
})
]
})
if (config.build.productionGzip) {
const 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) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}
module.exports = webpackConfig
我需要在html中添加static/js/init.js
,但如果文件是静态的,并且不是块,我不知道该怎么做。有什么建议吗?谢谢!
我尝试使用add-asset-html-webpack-plugin但无法正常工作
答案 0 :(得分:1)
您可以在使用html-webpack-plugin和copy-webpack-plugin生成的static/js/init.js
文件中插入index.html
。这还需要一个支持添加脚本资源的html模板,例如html-webpack-template中提供的资源。
以下是这些插件的必要配置:
plugins: [
new HtmlWebpackPlugin({
template: 'path-to/node_modules/html-webpack-template/index.ejs',
inject: false,
scripts: ['/static/js/init.js']
}),
new CopyWebpackPlugin([
{
from: './path/to/static/js/init.js',
to: path.join(config.build.assetsRoot, 'static/js/init.js')
}
])
]
此处CopyWebpackPlugin
用于将静态资产(本例中为脚本)复制到输出构建文件夹。
然后,输出路径将添加到scripts
的{{1}}选项中,以便添加引用该静态资产的HtmlWebpackPlugin
标记。
您可以使用此配置和其他有用的配置检查工作Webpack Demo。
答案 1 :(得分:0)
如果我找到了您并且您不想在构建过程中处理该文件,则可以将静态js
添加到index.html
。
像这样:
<script src="static/js/init.js"></script>
答案 2 :(得分:0)
我最终解决了这个问题。
我的inject: false
中最初有HtmlWebpackPlugin
,这阻止html-webpack-include-assets-plugin
工作。
为了让它正常工作,我使用html-webpack-include-assets-plugin
来注入任何静态资源,然后在我的new HtmlWebpackPlugin
中将chunks
设置为空数组([]
)。这意味着我的块仍然被编译但没有被转储到最终的构建HTML中。