这篇文章将为大家详细讲解有关webpack怎么打包单页面引用的js,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
目录结构如下图所示:
webpack打包代码如下:
var webpack = require('webpack');
var fs = require('fs');
var path = require('path');
var fse = require('fs-extra');
const debug = process.env.NODE_ENV !== 'production';
function entries(jsPath) {
var dirnames = fs.readdirSync(jsPath);
var entries = {}, entry;
for (var i = 0; i < dirnames.length; i++) {
var dirname, basename;
var jsList = [];
dirname = dirnames[i];
var files = fs.readdirSync(jsPath + '/' + dirname);
for (var j = 0; j < files.length; j++) {
entry = files[j];
basename = path.basename(entry, '.js');
jsList.push(path.join(jsPath, dirname, entry));
}
entries[path.join(dirname, 'app')] = jsList;
}
return entries;
}
var option = {
entry: entries(__dirname + '/js/src'),
output: {
path: __dirname + '/dist/',
filename: '[name].js'
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
]
}
var compiler = webpack(option);
compiler.run(function() {
fse.copy( __dirname + '/page/', __dirname + '/dist/');
console.log('success');
});
关于“webpack怎么打包单页面引用的js”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。