初始化,生成package.json

1
npm init -y

初始化git

1
git init

Install Node Dependency

1
cnpm install --save-dev webpack webpack-dev-server webpack-merge cross-env time-stamp
1
cnpm install --save-dev @babel/core @babel/preset-react babel-preset-env babel-loader
1
cnpm install --save-dev style-loader css-loader sass-loader postcss-loader autoprefixer cssnano postcss-preset-env node-sass file-loader url-loader

package.json

1
2
3
4
5
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --config webpack.config.dev -d --history-api-fallback --inline --colors",
"build": "npm run clean && cross-env NODE_ENV=production webpack --config webpack.config.prod --progress --colors",
"clean": "rm -rf dist"
}

.babelrc

1
2
3
{
"presets": ["@babel/preset-env","@babel/preset-react"]
}

.gitignore

git版本控制配置表

1
2
/node_modules/
/dist/

.npmignore

npm包发布配置表

1
2
/node_modules/
/src/

webpack.config.js

config可以通过webpack-merge拆分成base,dev,prod,moduleN
webpack configs

Plugins

html-webpack-plugin
mini-css-extract-plugin
zip-webpack-plugin
new webpack.ProvidePlugin
new webpack.HotModuleReplacementPlugin
new webpack.EnvironmentPlugin

postcss.config.js

1
2
3
4
5
6
7
8
9
10
module.exports = {
parser: 'sugarss',
plugins: [
require('autoprefixer'),
require('postcss-preset-env'),
require('cssnano')({
preset: 'default',
})
]
}

webpack.config 示例

base

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const path = require('path')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const devMode = process.env.NODE_ENV !== 'production'
const miniExtractSass = new MiniCssExtractPlugin({
filename: devMode ? 'dev.css' : 'prod.css'
})
module.exports = {
module: {
rules: [{
test: /\.(jsx|js)$/,
loader: 'babel-loader',
exclude: /node_modules/
}, {
test: /\.scss|\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
minimize: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]

}, {
test: /\.(png|jpe?g|gif|svg|ttf|eot|woff)(\?.*)?$/,
use: [{
loader: 'file-loader',
options: {
limit: 10000,
name: '[name]_[hash:6].[ext]'
}
}]
}]
},
resolve: {
extensions: ['.js', '.jsx', '.json', '.css', '.scss'],
alias: {
componentsPackage: path.resolve(__dirname, './src/components'),
}
},
plugins: [
miniExtractSass,
new webpack.EnvironmentPlugin(['NODE_ENV'])
]
}

dev

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
const path = require('path')
const merge = require('webpack-merge')
const webpack = require('webpack')
const baseConfig = require('./webpack.config.base')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const createWithHtml = new HtmlWebpackPlugin({
template: './example/index.html'
})
module.exports = merge(baseConfig, {
entry: './example/index.jsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'dev.js'
},
plugins: [
createWithHtml, new webpack.HotModuleReplacementPlugin()
],
devServer: {
contentBase: path.join(__dirname, './example'),
hot: true,
compress: true,
port: 8088,
progress: true,
disableHostCheck: true
},
devtool: 'source-map'
})

prod

1
2
3
4
5
6
7
8
9
10
11
12
13
const path = require('path')
const merge = require('webpack-merge')
const baseConfig = require('./webpack.config.base')
module.exports = merge(baseConfig, {
context: path.join(__dirname, './src'),
entry: './app/index.jsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
publicPath: './'
},
externals: {}
})

原创内容,欢迎交流转载请注明出处