工具css
步骤html
代码前端
代码不贴了,能够到 create-repo-cli
参考vue
注意:全部构建类相关的都应该安装到 devDependencies 下,即安装时加上 --save-devwebpack
这里就不一一列出全部功能的配置了,只写一些优化类的配置。想知道全部配置的,能够直接移步到 webpack-project-template
git
将开发环境、生产环境、ssr的配置分开文件写,易于维护。将共同的部分抽离出来,再经过 webpack-merge
来merge抽离出来的部分。例:github
const merge = require('webpack-merge')
const base = require('./webpack.base.config')
const dev = {...}
module.exports = merge(base, dev)
复制代码
将单个文件或整个目录复制到构建目录。适合用于拷贝一些静态资源,如图片等web
const CopyPlugin = require('copy-webpack-plugin')
new CopyPlugin([
{ from: path.resolve(__dirname, 'public'), to: '../dist/public' }
]),
复制代码
将CSS提取为独立的文件的插件,对每一个包含css的js文件都会建立一个CSS文件,支持按需加载css和sourceMap.vue-cli
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
new MiniCssExtractPlugin({
filename: 'chunk.[name].[contenthash:8].css',
}),
复制代码
Webpack 进行默认编译时会有不少无用的信息,须要进行清理,只显示少许信息,并便于排错。使用这个插件,能够帮助清理 console 的信息。也能够自定义错误处理回调等npm
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
new FriendlyErrorsWebpackPlugin(),
复制代码
进行多线程构建,提升构建速度
const HappyPack = require('happypack')
const os = require('os')
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length })
new HappyPack({
id: 'babel',
loaders: ['cache-loader', 'babel-loader?cacheDirectory'],
threadPool: happyThreadPool,
}),
复制代码
构建各路程的测速,如loader、plugin消耗的时间,能够经过看消耗的时间来针对性地进行一些优化...
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const smp = new SpeedMeasurePlugin()
module.exports = smp.wrap(merge(base, dev))
复制代码
代码规范是必须的。这里用 eslint 也支持了检查 ts 类型,毕竟 tslint 已经中止维护了,且官方也推荐用 eslint 进行约束。业界有好几个挺标准的规范,如airbnb等。这里,我是继承了 eslint-config-airbnb 再进行一些小修改。如过直接使用,这样就OK了:
//.eslintrc.js
module.exports = {
extends: ['eslint-config-airbnb'],
}
复制代码
统一规范 commit 格式,让 commit 信息整整齐齐的展现 安装 commitlint 、@commitlint/cli、@commitlint/config-conventional
//package.json
"pre-commit": [
"lint"
],
"husky": {
"hooks": {
"commit-msg": "commitlint -e .git/COMMIT_EDITMSG"
}
}
//commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'scope-empty': [0],
},
}
复制代码
rollup 就是适用于作工具库。其用到的配置,在功能上,其实与 webpack 差很少。具体配置就不详细说了。想了解的能够看 rollup-project-template
A cli to create project quickly...
sudo npm install create-repo-cli -g
yarn && yarn start
yarn
yarn start