webpack.config.js

来源:互联网 发布:趣分期走淘宝套现 编辑:程序博客网 时间:2024/06/11 14:55
var path = require('path');var glob = require('glob');var webpack = require('webpack');/* * extract css * (提取css文件) * */var ExtractTextPlugin = require("extract-text-webpack-plugin");/* * create html * (创建html文件) * */var HtmlWebpackPlugin = require('html-webpack-plugin');/* * clean publishing directory * (清空发布目录) * */var CleanPlugin = require('clean-webpack-plugin');var CopyWebpackPlugin = require('copy-webpack-plugin');//webpack --config 文件名.js -w        /*执行另一个配置文件*/var config = {entry: {index: './js/main.js'},output: {path: './build/',filename: 'js/[name].[chunkhash].js',chunkFilename: '[id].[chunkhash].js'},devServer: {        historyApiFallback: true,        hot: false,        inline: true,        grogress: true    },module: {loaders: [// { test: /\.css$/, loader: 'style-loader!css-loader' },{test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},{test: /\.less$/, loader: 'style-loader!css-loader!less-loader'},{test: /\.js$/, loader:"babel",query:{presets:["es2015","stage-0","react"]}},{test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'},]},plugins: [new CleanPlugin(['./build/*']),new CopyWebpackPlugin([{from: './images', to: 'images'},{from: './less', to: 'less'},]),// 公共CSS名字和路径new ExtractTextPlugin("css/[name].[chunkhash].css"),// 把公共文件提取出来new webpack.optimize.CommonsChunkPlugin({name: 'vendor',minChunks: function (module, count) {// any required modules inside node_modules are extracted to vendorreturn (module.resource &&/\.js$/.test(module.resource) &&module.resource.indexOf(path.join(__dirname, './node_modules')) === 0)}}),//压缩代码// new webpack.optimize.UglifyJsPlugin({// compress: {// warnings: false// }// }),new webpack.ProvidePlugin({// 全局依赖jQuery,不需要import了$: "jquery",jQuery: "jquery","window.jQuery": "jquery"})]};module.exports = config;var pages = Object.keys(getEntry('./*.html'));//生成HTML模板pages.forEach(function (pathname) {var conf = {filename: pathname + '.html', //生成的html存放路径,相对于pathtemplate: './' + pathname + '.html', //html模板路径inject: true, //允许插件修改哪些内容,包括head与bodyhash: false, //是否添加hash值minify: { //压缩HTML文件removeComments: true,//移除HTML中的注释collapseWhitespace: true //删除空白符与换行符},chunksSortMode: 'dependency',chunks: [pathname,"vendor"]};config.plugins.push(new HtmlWebpackPlugin(conf));});//按文件名来获取入口文件(即需要生成的模板文件数量)function getEntry(globPath) {var files = glob.sync(globPath);var entries = {},entry, dirname, basename, pathname, extname;for (var i = 0; i < files.length; i++) {entry = files[i];dirname = path.dirname(entry);extname = path.extname(entry);basename = path.basename(entry, extname);pathname = path.join(dirname, basename);entries[pathname] = entry;}return entries;}