//引入路径插件 const path = require('path'); //引入导出html插件 const HtmlWebpackPlugin = require('html-webpack-plugin'); //引入清除插件 const CleanWebpackPlugin = require('clean-webpack-plugin'); //引入导出css插件 const ExtractTextPlugin = require('extract-text-webpack-plugin'); //引入webpack const webpack = require('webpack'); //引入压缩css的插件 const optimizeCss = require('optimize-css-assets-webpack-plugin'); //引入cssnano插件 const cssnano = require('cssnano'); //引入js压缩插件 const uglifyjs = require('uglifyjs-webpack-plugin'); //webpack配置内容 const config = { //入口 entry:{ home:'./pages/lawSearchHomepage.js', list:'./pages/lawSearchList.js' }, //便于调试 devtool:'inline-source-map', //服务 devServer:{ contentBase:'./build/lawSearchHomepage.html' }, //loader模块 module:{ rules: [ {//ES6 test:/\.js$/, loader:'babel-loader', // exclude:__dirname+'node_modules',//不对这个进行babel转换,这里边已经打包好,这样能减少打包时间 // include:__dirname+'src'这里的src是你要编译的js文件的目录, exclude:path.resolve(__dirname,'node_modules'), include:path.resolve(__dirname,'pages'), query:{//若在package.json中定义了这个presets,则这边可以删掉 presets:['es2015'] } }, {//Jquery test: require.resolve('jquery'), use: [{ loader: 'expose-loader', options: 'jQuery' },{ loader: 'expose-loader', options: '$' }] }, {//CSS test:/\.css/, use:ExtractTextPlugin.extract({ use:['css-loader'] }) }, {//Sass test:/\.scss/, use:ExtractTextPlugin.extract({ fallback:"style-loader", use:['css-loader','sass-loader'] },) }, {//处理模块html test: /\.html$/, use: 'html-loader' }, {//图片 test: /\.(jpg|png|gif)$/, use: { loader: 'file-loader', options: { outputPath: 'images' } } }, //字体图标 { test: /\.(eot|svg|ttf|woff|woff2)\w*/, loader: 'file-loader' } ] }, //插件 plugins: [ //清空build文件下的多余文件 new CleanWebpackPlugin(['build']), //将css单独打包插件 new ExtractTextPlugin({ filename:"[name].css",//制定编译后的文件名称 allChunks:true,//把分割的块分别打包 }), //html单独导出插件——首页 new HtmlWebpackPlugin({ filename:'lawSearchHomepage.html',//输出后的文件名称 template:'./pages/lawSearchHomepage.html',//模版页面路径 favicon:'./pages/images/favicon.ico',//页签图标 chunks:['home'],//需要引入的js文件名称 inject: true, hash:true,//哈希值 minify: {//压缩 removeComments: true, collapseWhitespace: true } }), //html单独导出插件——列表页 new HtmlWebpackPlugin({ filename:'lawSearchList.html', template:'./pages/lawSearchList.html', favicon:'./pages/images/favicon.ico', chunks:['list'], inject: true, hash:true, minify: { removeComments: true, collapseWhitespace: true } }), //压缩css new optimizeCss({ assetNameRegExp: /\.style\.css$/g, cssProcessor: cssnano, cssProcessorOptions: { discardComments: { removeAll: true } }, canPrint: true }), //压缩js new uglifyjs() ], //压缩优化css optimization: { // minimize: true, minimizer: [new optimizeCss({})] }, //出口 output:{ filename: '[name].bundle.js', path: path.resolve(__dirname,'build') } } module.exports = config;
如果在打包过程中报模块没有定义错误,那就再安装一次,实在不行,直接删了node_modules文件,重新npm install一下。