Webpack 条目的不同名称
Webpack different names for entries
如何为不同的条目输出指定不同的 filename
?
例如:
module.exports = {
context: path.resolve(__dirname, 'assets'),
entry: {
vendor: ['react', 'react-dom', 'lodash', 'redux'],
app: './src/app.js'
}
output: {
path: path.resolve(__dirname, (isDevelopment) ? 'demo' : 'build'),
filename: (isDevelopment) ? '[name].js' : '[name][chunkhash:12].js'
}
}
接收这样的输出
build
-- index.html
-- app.2394035ufas0ue34.js
-- vendor.js
因此浏览器将 vendor.js
与所有库一起缓存。因为我不打算很快和经常迁移到任何主要的新版本。
并且仍然能够在每次需要更新时中断 app.js
的缓存。
是否有某种选项可以将 output
设置为
output: {
app: {
...
},
vendor: {
...
},
}
这是工作代码:
entry: {
'./build/app': './src/app.js',
'./build/vendor': VENDOR_LIBS // or path to your vendor.js
},
output: {
path: __dirname,
filename: '[name].[chunkhash].js'
},
将此代码作为数组的最后一个元素添加到您的 webpack plugins
数组中。
plugins: [
... // place our new plugin here
]
function() {
this.plugin("done", function(stats) {
const buildDir = __dirname + '/build/';
const fs = require('fs');
var vendorTempFileName = '';
new Promise(function(resolve, reject) {
fs.readdir(buildDir, (err, files) => {
files.forEach(file => {
if (file.substr(0,6) === 'vendor') {
resolve(file);
}
});
});
}).then(function(file) {
fs.rename( buildDir + file, buildDir + 'vendor.js', function(err) {
if ( err ) console.log('ERROR: ' + err);
});
});
});
}
输出应该如下:
由于浏览器缓存的缘故,让文件不包含块哈希被认为是不好的做法。
对于 Webpack 4,我添加了一个快捷的 done
钩子来重命名我的 service worker 脚本:
// Plugin to rename sw-[chunkhash].js back to sw.js
class SwNamePlugin {
apply(compiler) {
compiler.hooks.done.tap("SW Name Plugin", (stats) => {
const swChunk = stats.compilation.chunks.find((c) => c.name === "sw");
fs.rename(path.resolve(outDir, swChunk.files[0]), `${outDir}/sw.js`);
});
}
}
plugins.push(new SwNamePlugin());
这避免了警告 DeprecationWarning: Tapable.plugin is deprecated. Use new API on .hooks instead
您会在 loelsonk 的回答后看到。
如何为不同的条目输出指定不同的 filename
?
例如:
module.exports = {
context: path.resolve(__dirname, 'assets'),
entry: {
vendor: ['react', 'react-dom', 'lodash', 'redux'],
app: './src/app.js'
}
output: {
path: path.resolve(__dirname, (isDevelopment) ? 'demo' : 'build'),
filename: (isDevelopment) ? '[name].js' : '[name][chunkhash:12].js'
}
}
接收这样的输出
build
-- index.html
-- app.2394035ufas0ue34.js
-- vendor.js
因此浏览器将 vendor.js
与所有库一起缓存。因为我不打算很快和经常迁移到任何主要的新版本。
并且仍然能够在每次需要更新时中断 app.js
的缓存。
是否有某种选项可以将 output
设置为
output: {
app: {
...
},
vendor: {
...
},
}
这是工作代码:
entry: {
'./build/app': './src/app.js',
'./build/vendor': VENDOR_LIBS // or path to your vendor.js
},
output: {
path: __dirname,
filename: '[name].[chunkhash].js'
},
将此代码作为数组的最后一个元素添加到您的 webpack plugins
数组中。
plugins: [
... // place our new plugin here
]
function() {
this.plugin("done", function(stats) {
const buildDir = __dirname + '/build/';
const fs = require('fs');
var vendorTempFileName = '';
new Promise(function(resolve, reject) {
fs.readdir(buildDir, (err, files) => {
files.forEach(file => {
if (file.substr(0,6) === 'vendor') {
resolve(file);
}
});
});
}).then(function(file) {
fs.rename( buildDir + file, buildDir + 'vendor.js', function(err) {
if ( err ) console.log('ERROR: ' + err);
});
});
});
}
输出应该如下:
由于浏览器缓存的缘故,让文件不包含块哈希被认为是不好的做法。
对于 Webpack 4,我添加了一个快捷的 done
钩子来重命名我的 service worker 脚本:
// Plugin to rename sw-[chunkhash].js back to sw.js
class SwNamePlugin {
apply(compiler) {
compiler.hooks.done.tap("SW Name Plugin", (stats) => {
const swChunk = stats.compilation.chunks.find((c) => c.name === "sw");
fs.rename(path.resolve(outDir, swChunk.files[0]), `${outDir}/sw.js`);
});
}
}
plugins.push(new SwNamePlugin());
这避免了警告 DeprecationWarning: Tapable.plugin is deprecated. Use new API on .hooks instead
您会在 loelsonk 的回答后看到。