具体代码
const path = require('path');
module.exports = {
mode: 'production',
entry: './src/index.js',
// 忽略 lodash ,不打包
// externals: ['lodash'],
externals: {
// 当用户引用 lodash 这个库文件的时候,必须使用 lodash 这个名字
lodash: {
commonjs: 'lodash'
}
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'library.js',
library: 'library',
// 支持 script 标签引入
libraryTarget: 'umd'
// import const require 无论什么方法引入,都支持
// libraryTarget: 'this'
// library 挂载到 this
}
}
package.json
"main": "./dist/library.js",
"scripts": {
"build": "webpack"
},
为了模拟环境,安装npm i http-server -D
npm install workbox-webpack-plugin --save-dev
具体代码
package.json
"scripts": {
"start": "http-server dist",
"dev": "webpack-dev-server --config ./build/webpack.common.js",
"build": "webpack --env.production --config ./build/webpack.common.js"
},
webpack.prod.js
const WorkboxPlugin = require('workbox-webpack-plugin');
// ....
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[name].chunk.css'
}),
new WorkboxPlugin.GenerateSW({
clientsClaim: true,
skipWaiting: true
})
],
npm install --save-dev typescript ts-loader
npm install --save-dev @types/lodash
webpack.config.js
const path = require('path')
module.exports = {
mode: 'production',
entry: './src/index.tsx',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
}
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist",
"module": "es6",
"target": "es5",
"allowJs": true
}
}
typescript
npm i axios -D
以 react 为例
class App extends Component {
componentDidMount() {
axios.get('/react/api/header.json').then ((res) => {
console.log(res)
})
}
render () {
return <div>hello react</div>
}
}
devServer: {
contentBase: './dist',
open: true,
hot: true,
hotOnly: true,
proxy: {
'/react/api': {
target: 'http://www.dell-lee.com',
//对https协议的网址的请求的转发
secure: false,
// bypass: function (req, res, proxyOptions) {
// // 请求是 html 的情况,直接返回这个 html ,不会走代理
// if (req.headers.accept.indexOf('html') !== -1) {
// console.log('Skipping proxy for browser request.');
// return '/index.html';
// }
// },
pathRewrite: {
'header.json': 'demo.json'
},
//解决网站对接口的限制
changeOrigin: true,
//变更请求头
headers: {
host: 'www.dell-lee.com',
cookie: 'asvafhgs'
}
}
}
},
npm i react-router-dom --save
proxy: {
'/react/api': {
target: 'http://www.dell-lee.com',
secure: false,
// historyApiFallback: {
// rewrites: [//访问任何路径都展示index.html页面
// { from: /\.*/, to: '/index.html' },
// ]
// },
// 一般配置这个就可以了
historyApiFallback: true,
// bypass: function (req, res, proxyOptions) {
// // 请求是 html 的情况,直接返回这个 html ,不会走代理
// if (req.headers.accept.indexOf('html') !== -1) {
// console.log('Skipping proxy for browser request.');
// return '/index.html';
// }
// },
pathRewrite: {
'header.json': 'demo.json'
},
changeOrigin: true,
headers: {
host: 'www.dell-lee.com',
cookie: 'asvafhgs'
}
}
}
npm install eslint -D
npm install babel-eslint -D
npm install eslint-loader -D
//快速生成eslint配置
npx eslint --init
.eslintrc.js
module.exports = {
"env": {
"browser": true,
"es6": true
},
"extends": [
"plugin:react/recommended",
"airbnb"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
}
};
webpack.config.js
devServer: {
// eslint 报错显示在浏览器
overlay: true,
contentBase: './dist',
},
{
test: /\.js$/,
exclude: /node_modules/,
// use: [
// 'babel-loader',
// {
// loader: 'eslint-loader',
// options: {
// fix: true
// }
// }
// ]
// use: [
// {
// loader: 'eslint-loader',
// options: {
// fix: true
// },
// // 强制执行
// force: 'pre'
// },
// 'babel-loader'
// ]
loader: 'babel-loader'
},
1.使用高版本的 Webpack 和 Node.js
2.多进程/多实例构建:thread-loader, parallel-webpack,happypack
3.压缩代码
4.图片压缩
5.缩小打包作用域
6.提取页面公共资源
DLL:
充分利用缓存提升二次构建速度:
Tree shaking
Scope hoisting
动态Polyfill
mkdir make-loader
cd make-loader
npm init -y
npm i webpack webpack-cli -D
npm i loader-utils -D
webpack.config.js
const path = require('path')
module.exports = {
mode: 'development',
entry: {
main: './src/index.js'
},
resolveLoader: {
modules: ['node_modules', './loader']
},
module: {
rules: [
{
test: /\.js/,
use: [
// path.resolve(__dirname, './loader/replaceLoader.js')
// {
// loader: path.resolve(__dirname, './loader/replaceLoader.js'),
// options: {
// name: 'webpack'
// }
// }
path.resolve(__dirname, './loader/replaceLoader.js'),
{
loader: path.resolve(__dirname, './loader/replaceLoaderAsync.js'),
options: {
name: 'webpack'
}
}
]
}
]
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
}
}
const path = require('path')
const CopyrightWebpackPlugin = require('./plugins/copyright-webpack-plugin')
module.exports = {
mode: 'development',
entry: {
main: './src/index.js'
},
plugins:[
new CopyrightWebpackPlugin()
],
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
}
}