未测试,只是记录下来而已
const {resolve} = require('path')const MiniCssExtractPlugin = require('mini-css-extract-plugin')const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')/* 关于js 正常来讲,一个文件只能被一个loader处理 当一个文件要被多个loader处理,那么一定要指定loader执行的先后顺序 先执行eslint 在 执行 babel*///定义nodejs环境变量:决定使用browserslist的哪个环境process.env.NODE_ENV = 'production'const commonCssLoader = [ MiniCssExtractPlugin.loader, 'css-loader', { //还需要在package.js中定义browserslist loader:'post-loader' } ] module.exports ={ entry:'./src/js/index.js', output:{ filename:'js/built.js', path:resolve(__dirname,'build') }, module:{ rules:[ { test:/\.css$/, //use 从下到上执行 use:[ ...commonCssLoader ] }, { test:/\.less$/, use:[ ...commonCssLoader, 'less-loader' ] }, { /* 在package.json中eslintConfig -->airbnb*/ test:/\.js$/, exclude:/node_modules/, // 优先执行 enforce:'pre', loader:'eslint-loader', options:{ fix:true } }, { test:/\.js$/, exclude:/node_modules/, loader:'babel-loader', options:{ presets:[ '@babel/preset-env', { userBuiltIns:'usage', corejs:{version:3}, targets:{ chrome:'60', firefox:'50' } } ] } }, { test:/\.(jpg|png|gif)/$, loader:'url-loader', options:{ limit:8*1024, name:'[hash:10].[ext]', outputPath:'imgs', esModule:false } }, { test:/\.html$/, loader:'html-loader' }, { exclude:/.(js|css|html|jpg|png|gif)/, loader:'file-loader', options:{ outputPath } } ] }, plugins:[ new HtmlWebpackPlugin({ template:'./src/index.html', minify:{ collapseWhitespace:true, removeComments:true } }), new MiniCssExtractPlugin({ filename:'css/built.css' }), new OptimizeCssAssetsWebpackPlugin() ], mode:'development'}
{ "name": "12webpackproduction", "version": "1.0.0", "description": "", "main": "postcss.config.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "css-loader": "^5.2.4", "file-loader": "^6.2.0", "html-webpack-plugin": "^5.3.1", "mini-css-extract-plugin": "^1.5.0", "postcss-loader": "^5.2.0", "postcss-preset-env": "^6.7.0", "style-loader": "^2.0.0", "url-loader": "^4.1.1" }, "browserslist": { "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ], "production": [ ">0.2%", "not dead", "not op_mini all" ], "eslintConfig": { "extends": "airbnb-base" } } }