如何为不同的条目输出指定不同的文件名
?
例如:
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: {
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);
});
});
});
}
由于浏览器缓存的原因,让文件不带 chunkhashes 被认为是不好的做法。
关于javascript - Webpack 条目的不同名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43182092/