survivejs教程链接
代码:survivejs-webpack
一、功能性
1. css处理
1 2 3 4 5
| options: { importLoaders: 1 }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| exports.extractCSS = ({ include, exclude, use = [] }) => { const plugin = new MiniCssExtractPlugin({ filename: '[name].[contenthash:4].css'
});
return { module: { rules: [ { test: /\.css$/, include, exclude,
use: [ MiniCssExtractPlugin.loader ].concat(use) } ] }, plugins: [plugin] }; };
|
1 2 3 4 5 6
| exports.purifyCSS = ({paths}) => ({ plugins: [new PurifyCSSPlugin({ paths })] });
|
1 2 3 4 5 6
| exports.autoprefix = () => ({ loader: 'postcss-loader', options: { plugins: () => [require('autoprefixer')()] } });
|
自动补齐添加.browserslistrc
文件
1 2 3 4 5 6 7 8 9 10
| parts.extractCSS({ use: ['css-loader', parts.autoprefix()]
}), parts.purifyCSS({ paths: glob.sync(`${PATHS.app}/**/*.js`, { nodir: true }) }),
|
2. 图片处理
- url-loader:可以设置limit,和name;
3. js处理
加.env文件;
如果有按需加载的import语法,需要
"plugins": ["syntax-dynamic-import"]
4. 单个html
1 2 3 4 5 6 7
| { plugins:[ new HtmlWebpackPlugin({ title: 'Webpack demo' }) ] }
|
5. 多页处理
需要多个entry;需要output.publicPath;页面可通过chunks复用公共资源
定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| exports.page = ({ path = '', template = require.resolve( 'html-webpack-plugin/default_index.ejs' ), title, entry, chunks = [] } = {}) => ({ entry,
plugins: [ new HtmlWebpackPlugin({ chunks, filename: `${path && path + '/'}index.html`, template, title }) ] });
|
使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| { output: { publicPath: '/' } }, const pages = [ parts.page({ title: 'Webpack demo', entry: { app: PATHS.app }, chunks: ['app', 'manifest', 'vendor']
}), parts.page({ title: 'Another demo', path: 'another', entry: { another: path.join(PATHS.app, 'another.js') }, chunks: ['another','vendor','manifest']
}) ];
return merge([commonConfig, config, { mode }].concat(pages));
|
6. 清理功能
1 2 3
| exports.clean = path => ({ plugins: [new CleanWebpackPlugin([path])] });
|
7. 添加版本信息
1 2 3 4 5 6 7
| exports.attachRevision = () => ({ plugins: [ new webpack.BannerPlugin({ banner: new GitRevisionPlugin().version() }) ] });
|
二、代码分割+懒加载+tree-shaking
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { optimization: { splitChunks: { cacheGroups: { commons: { test: /[\\/]node_modules[\\/]/, name: 'vendor', chunks: 'initial' } } } } },
|
三、其他优秀
compose的模式
将定义与配置分离;
webpack.config.js
webpack.parts.js
构建分析
产出,然后有许多可视化工具分析
1
| "build:stats": "webpack --env production --profile --json > stats.json"
|