一.创建项目并初始化
初始化:npm init -y (默认配置)
二.安装依赖
安装webpack依赖,bable依赖
npm install --save-dev babel-core babel-loader babel-plugin-import babel-preset-latest babel-preset-react babel-preset-stage-0 clean-webpack-plugin css-loader file-loader html-webpack-plugin node-sass sass-loader style-loader url-loader webpack webpack-cli webpack-dev-server
安装react相关依赖
npm install --save react react-dom react-router-dom
三.创建src,config文件以及indexl文件夹
config文件夹下各文件的配置
webpack.base.conf.js
'use strict'
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
// 入口起点
entry: {
app: './src/main.js',
},
// 输出
output: {
path: path.resolve(__dirname, '../dist'),
filename: "[name].js",
},
// 解析
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json']
},
// loader
module: {
rules: [
{
test: /\.js|jsx$/,
exclude: /(node_modules|bower_components)/,// 屏蔽不需要处理的文件(文件夹)(可选)
loader: 'babel-loader'
}
]
},
// 插件
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: 'body'
})
]
};
webpack.dev.conf.js
先安装webpack-merge、uglifyjs-webpack-plugin依赖。webpack-merge是用于合并文件,UglifyJS Webpack Plugin插件用来缩小(压缩优化)js文件,至少需要Node v6.9.0和Webpack v4.0.0版本。
uglifyjs-webpack-plugin使用参考:https://www.jianshu.com/p/b597ea88b165
'use strict'
const {merge} = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
const path = require('path');
const webpack = require('webpack');
module.exports = merge(baseWebpackConfig, {
// 模式
mode: "development",
// 调试工具
devtool: 'inline-source-map',
// 开发服务器
devServer: {
contentBase: false,// 默认webpack-dev-server会为根文件夹提供本地服务器,如果想为另外一个目录下的文件提供本地服务器,应该在这里设置其所在目录
historyApiFallback: true,// 在开发单页应用时非常有用,它依赖于HTML5 history API,如果设置为true,所有的跳转将指向index.html
compress: true,// 启用gzip压缩
inline: true,// 设置为true,当源文件改变时会自动刷新页面
hot: true,// 模块热更新,取决于HotModuleReplacementPlugin
host: '127.0.0.1',// 设置默认监听域名,如果省略,默认为“localhost”
port: 8703// 设置默认监听端口,如果省略,默认为“8080”
},
// 插件
plugins: [
// 热更新相关
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
],
optimization: {
nodeEnv: 'development',
}
});
webpack.prod.conf.js
'use strict'
const {merge} = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
const path = require('path');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
module.exports = merge(baseWebpackConfig, {
// 模式
mode: "production",
// 调试工具
devtool: '#source-map',
// 输出
output: {
path: path.resolve(__dirname, '../dist'),
filename: "js/[name].[chunkhash].js",
},
// 插件
plugins: [
new CleanWebpackPlugin(),
new webpack.HashedModuleIdsPlugin(),
],
// 代码分离相关
optimization: {
nodeEnv: 'production',
minimizer: [new UglifyJSPlugin()],
runtimeChunk: {
name: 'manifest'
},
splitChunks: {
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
name: false,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'initial',
}
}
}
}
});
配置完成之后执行npm run dev 命令。出现如下图片说明搭建成功
参考:https://blog.csdn.net/qq_26789867/article/details/90238015