重點是瞭解四個核心概念:輸入,輸出,加載程序和插件css
npm install webpack webpack-cli webpack-dev-server –ghtml
npm install webpack webpack-cli webpack-dev-server --save-devwebpack
這裏須要知道的是:(es5和es6寫法上的不一樣)
eg:
a.Js 暴露模塊 module.exports=obj; || export default obj;
b.Js 接收模塊,設置html須要功能 require(‘./xx.js) || import obj from ‘./xx.js’es6
webpack 輸入文件.js -o 輸出文件.js --mode development (開發環境build)web
webpack main.js -o output.js --mode production(生產環境build)npm
webpack.config.jsjson
var path = require('path');
module.exports = {
mode: 'development', //開發模式
entry: './main.js', //入口文件
output: { //出口文件
path: path.resolve(__dirname, 'dist'), //路徑
filename: 'output.js' //名字
}};
複製代碼
配置好後終端輸入webpack運行瀏覽器
再安裝一下熱更新省事 熱更新:npm install webpack-dev-server –gbash
var path = require('path');
module.exports = {
//...
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
}};
複製代碼
運行終端命令webpack-dev-server --hot --line完成自動刷新webpack-dev-server
若是默認端口被佔用,改端口webpack-dev-server --port 3000 (將默認端口改爲3000) 運行命令更改:在package.json中scripts添加:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"start": "npm run dev",
"dev":"webpack-dev-server"
},
複製代碼
則npm start運行
剩餘css、img、js、es六、ts的loader自尋查找webpack官網
www.webpackjs.com