webpack 是一个现代 JavaScript 应用程序的静态模块打包器,前端模块化的基础。做为一个前端工程师(切图仔),很是有必要学习。前端
webpack官网的文档很是的棒,中文文档也很是给力,能够媲美vue的文档。建议先看概念篇章,再看指南,而后看API和配置总览。看完指南教程后,须要自主动手练习才能更加有影响,这里记录下我搭建react开发环境的过程vue
```
yarn add webpack webpack-cli -D
```
复制代码
```
config
webpack.common.js
webpack.dev.js
webpack.prod.js
scripts
build.js // 构建模式脚本
start.js // 开发模式脚本
src
index.js
package.json
```
复制代码
package.jsonnode
```
...
"license": "MIT",
+ "scripts": {
+ "start": "node ./scripts/start.js",
+ "build": "node ./scripts/build.js"
+ },
"devDependencies": {
"webpack": "^4.35.2",
"webpack-cli": "^3.3.5"
}
...
```
复制代码
src/index.js
做为主入口,以 build
为打包后的目录config/webpack.common.jsreact
output path字段这里配置的绝对路径webpack
const path = require('path');
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "../build"),
filename: "bundle.js"
}
}
复制代码
scripts/build.jsgit
const webpack = require('webpack');
const webpackConfig = require('../config/webpack.common.js');
webpack(webpackConfig, (err, stats) => {
if(err || stats.hasErrors()){
console.log("编译失败");
}
});
复制代码
webpack node接口文档: www.webpackjs.com/api/node/github
src/index.jsweb
console.log('hello')
复制代码
yarn build
命令,生成打包目录运行生成的 `bundle.js`
复制代码
yarn add webpack-dev-server webpack-merge -D
复制代码
config/webpack.common.jsjson
const path = require('path');
function webpackCommonConfigCreator(options){
/**
* options:
* mode // 开发模式
*/
return {
mode: options.mode,
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "../build"),
}
}
}
module.exports = webpackCommonConfigCreator;
复制代码
config/webpack.prod.jsapi
const webpackConfigCreator = require('./webpack.common');
const merge = require('webpack-merge');
const config = {
}
const options = {
mode: "production"
}
module.exports = merge(webpackConfigCreator(options), config);
复制代码
config/webpack.dev.js
const webpackConfigCreator = require('./webpack.common');
const merge = require('webpack-merge');
const config = {
}
const options = {
mode: "development"
}
module.exports = merge(webpackConfigCreator(options), config);
复制代码
script/build.js
const webpack = require('webpack');
const webpackConfig = require('../config/webpack.prod.js');
webpack(webpackConfig, (err, stats) => {
if(err || stats.hasErrors()){
console.log("编译失败");
}
});
复制代码
yarn build
打包,输出正常
config/webpack.dev.js
这里的contentBase
选项是server模式下的output,开启server后,webpack会实时编译代码到内存
...
+ const path = require('path');
const config = {
+ devServer: {
+ contentBase: path.join(__dirname, "../dist")
+ }
}
...
复制代码
scripts/start.js
const webpack = require('webpack');
const webpackDevServer = require('webpack-dev-server');
const webpackConfig = require('../config/webpack.dev.js');
const compiler = webpack(webpackConfig);
const options = Object.assign({}, webpackConfig.devServer, {
open: true
})
const server = new webpackDevServer(compiler, options);
server.listen(3000, '127.0.0.1', () => {
console.log('Starting server on http://localhost:8080');
})
复制代码
yarn start
, 浏览器自动弹出窗口,访问 localhost:3000/bundle.js
相关连接
源码github仓库: github.com/tanf1995/We…