webpack - 如何使用 Webpack 3 创建一个完全独立的块
问题描述
我在我的项目中引入了一个服务工作者,它将用于推送通知。我想在服务人员和我网站的其他部分之间分享一些通用代码。
使用 Webpack 3 来构建我的项目,我最初的方法是为 service worker 创建一个单独的条目,并将生成的文件放在我的输出的根目录中,不使用哈希来为其提供一致的网址:
entry: {
'app': './src/main.js',
'service-worker': './src/service-worker.js'
},
output: {
path: config.build.assetsRoot,
filename: chunkData => {
return chunkData.chunk.name === 'service-worker'
? '[name].js'
: utils.assetsPath('js/[name].[chunkhash].js')
},
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
},
我还排除了 service worker 被注入到我的 HTML 中:
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true,
excludeChunks: ['service-worker'],
}),
CommonsChunkPlugin
当我第一次创建项目时,Vue CLI 工具自动设置的还有几种用法:
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
}),
我遇到的问题是生成的service-worker.js
文件是这样开始的:
webpackJsonp([17],{"2xmR":function(e,n,_){"use strict";
var t,c,r,a,o,i;t="/path/to/src/service-worker.js",...
当浏览器尝试运行 service worker 时,该webpackJsonp
功能不可用。
我想做的是让 Webpack 3 生成service-worker.js
它的所有依赖项(它们很少,我可以接受重复的代码)并且没有任何 Webpack 实用程序。
我的搜索不足。似乎我想做与CommonsChunkPlugin
预期相反的事情——但我确实想要网站的那些常见块,而不是服务人员。
如何构建我的入口点之一,其所有依赖项都包含在文件中并且没有 Webpack 实用程序?
解决方案
我偶然发现了一个关于 GitHub 问题的评论,导致我找到了这个解决方案。非常感谢 Eric Wood ( SO , GitHub ) 的评论。
我学到的是CommonsChunkPlugin
接受一个chunks
选项来限制哪些块被用作公共块的来源。通过提供一个排除我的service-worker
块的块列表,Webpack 不会提取通用代码service-worker
,因此不会引入webpackJsonp
代码来动态加载通用代码。
在其他一切都相同的情况下,这是解决我的问题的新 Webpack 插件配置:
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
chunks: ['app'],
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
)
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
chunks: ['app'],
minChunks: Infinity
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'app',
async: 'vendor-async',
children: true,
minChunks: 3
}),
请注意,我同时添加chunks
了 thevendor
和manifest
common 块。最后一次使用CommonsChunkPlugin
已经在使用该children
选项,Webpack 很快告诉我chunks
不需要。
这会导致一个service-worker.js
文件没有webpackJsonp
,而是像这样开始:
!function(e){var t={};function n(r){ ...
推荐阅读
- python - 使用python循环遍历复杂的xml数据以获取特定值
- sql - SQL中没有固定列数的列的连接
- mongodb - 尝试使用 Mongo 查询加入(查找)两个集合,但它给我一个错误
- javascript - Browserify Express App 错误:解析文件意外令牌
- python - 在遇到换行符或逗号时拆分/剥离一组行
- python - 在 tf.while_loop 中使用 tf.concat 会导致 `InvalidArgumentError: ConcatOp : 所有输入张量的排名应该匹配`。但是 Ops 可以单独工作
- java - 如何在 java netbeans 上使用输入 joption 创建行
- vb.net - 使用 Westhold 的 Race Manager 软件读取 TCP/IP 数据流
- java - Tomcat8 Springboot不读取application.properties
- javascript - 如何使用 JS 重复 Type Writer 效果?