GitHub 我們一同進修webpack地點:https://github.com/liangfengbo/learning-webpack
怎樣運用webpack打包JS
我們在敕令行中輸入:webpack -h
看看webpack的敕令行大全
Usage: webpack-cli [options]
webpack-cli [options] --entry <entry> --output <output>
webpack-cli [options] <entries...> --output <output>
webpack通知我們,用webpack舉行打包有三種要領:
1.新建為webpack.config.js定名的文件,能夠直接webpack敕令打包
2.webpack 進口文件<app.js> -o 輸出文件<bundle.js>
3.webpack --config 你自定義的文件名字<webpack-default.js>
新建一個目次,新建一個sum.js文件,這個文件主假如用es6 module暴露一個加法函數要領:
// sum.js
export default function sum(a, b) {
return a + b;
}
然後新建一個app.js進口文件(webpack要打包這個文件)
// app.js
// es6 module範例
import sum from './sum'
// 挪用
console.log('sum(23,24) = ', sum(23, 24));
下面舉行打包:
webpack app.js -o bundle.js
假如打包勝利顯現:
Hash: 7e137d767d2665688484
Version: webpack 4.5.0
Time: 588ms
Built at: 2018-6-20 20:30:06
Asset Size Chunks Chunk Names
0.bundle.js 188 bytes 0 [emitted]
bundle.js 1.84 KiB 1 [emitted] main
Entrypoint main = bundle.js
[0] ./app.js + 1 modules 466 bytes {1} [built]
| ./app.js 411 bytes [built]
| ./sum.js 55 bytes [built]
[1] ./minus.js 54 bytes {1} [built]
[2] ./muti.js 114 bytes {0} [built]