從0開(kāi)始
1.0甜癞、 新建package管理文件 package.json
,
cnpm init y
1.1、目錄下新建兩個(gè)文件夾:dist
鹤竭,src()
dist: 存放入口html文件及webpack生成的js文件
src: 代碼源文件
1.2丈屹、安裝package
//安裝webPack
cnpm install -D webpack
//安裝webPack-cli
npm i -D webpack-cli
//安裝loadsh:
//lodash是一套工具庫(kù)乱豆,內(nèi)部封裝了很多字符串、數(shù)組钩述、對(duì)象等常見(jiàn)數(shù)據(jù)類型的處理函數(shù)寨躁。
cnpm install loadsh
1.3、簡(jiǎn)單的第一個(gè)webpack代碼
- package.json: 查看你安裝的三方插件的版本
{
"name": "webpackDemo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0"
},
"dependencies": {
"loadsh": "^0.0.4"
}
}
- index.html: 網(wǎng)頁(yè)加載入口牙勘,將webpack編譯出來(lái)的main.js嵌入到html中顯示出來(lái)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>標(biāo)題</title>
</head>
<body>
<div>以下顯示webpack打包的js內(nèi)容:</div>
<script src="./main.js"></script>
</body>
</html>
- webpack.config.js: webpack設(shè)置文件职恳,定義入口及輸出路徑等...
const path = require('path');
module.exports = {
// entry: './src/index.js',
entry: './src/index.js',
mode: 'development',
output: {
filename: 'main.js',
path: path.resolve(__dirname,'dist')
}
}
- index.js: 我們編寫的一個(gè)個(gè)js文件所禀,后通過(guò)webpack打包成main.js作用到html中
import loda from 'loadsh'
function createDomElement() {
let dom = document.createElement('div');
dom.innerHTML = loda.join(['aicoder.com','hao',],'');
return dom;
}
let divDom = createDomElement();
document.body.appendChild(divDom);
- 打包命令
npx webpack
其他插件
- postCss
cnpm i -D postcss-loader
cnpm install autoprefixer --save-dev