webpack å¼ç®±å³ç¨ï¼å¯ä»¥æ é使ç¨ä»»ä½é
ç½®æ件ãç¶èï¼webpack ä¼åå®é¡¹ç®çå
¥å£èµ·ç¹ä¸º src/index.js
ï¼ç¶åä¼å¨ dist/main.js
è¾åºç»æï¼å¹¶ä¸å¨ç产ç¯å¢å¼å¯å缩åä¼åã
éå¸¸ä½ ç项ç®è¿éè¦ç»§ç»æ©å±æ¤è½åï¼ä¸ºæ¤ä½ å¯ä»¥å¨é¡¹ç®æ ¹ç®å½ä¸å建ä¸ä¸ª webpack.config.js
æ件ï¼ç¶å webpack ä¼èªå¨ä½¿ç¨å®ã
ä¸é¢æå®äºææå¯ç¨çé ç½®é项ã
å¦æåºäºæäºåå ï¼éè¦æ ¹æ®ç¹å®æ
åµä½¿ç¨ä¸åçé
ç½®æ件ï¼åå¯ä»¥éè¿å¨å½ä»¤è¡ä¸ä½¿ç¨ --config
æ å¿ä¿®æ¹ã
package.json
"scripts": {
"build": "webpack --config prod.config.js"
}
Webpack æ大éçé 置项ï¼å¯è½ä¼è®©ä½ ä¸ç¥ææªï¼è¯·å©ç¨ webpack-cli ç init å½ä»¤ï¼å®å¯ä»¥æ ¹æ®ä½ ç项ç®éæ±å¿«éçæ webpack é ç½®æ件ï¼å®ä¼å¨å建é ç½®æ件ä¹å询é®ä½ å 个é®é¢ã
npx webpack init
å¦æå°æªå¨é¡¹ç®æå
¨å±å®è£
@webpack-cli/generators
ï¼npx å¯è½ä¼æç¤ºä½ å®è£
ãæ ¹æ®ä½ å¨é
ç½®çæè¿ç¨ä¸çéæ©ï¼ä½ ä¹å¯è½ä¼å®è£
é¢å¤ç package å°ä½ ç项ç®ä¸ã
$ npx webpack init
[webpack-cli] For using this command you need to install: '@webpack-cli/generators' package.
[webpack-cli] Would you like to install '@webpack-cli/generators' package? (That will run 'npm install -D @webpack-cli/generators') (Y/n)
devDependencies:
+ @webpack-cli/generators 2.5.0
? Which of the following JS solutions do you want to use? ES6
? Do you want to use webpack-dev-server? Yes
? Do you want to simplify the creation of HTML files for your bundle? Yes
? Do you want to add PWA support? No
? Which of the following CSS solutions do you want to use? CSS only
? Will you be using PostCSS in your project? Yes
? Do you want to extract CSS for every file? Only for Production
? Do you like to install prettier to format generated configuration? Yes
? Pick a package manager: pnpm
[webpack-cli] â¹ INFO Initialising project...
devDependencies:
+ @babel/core 7.19.3
+ @babel/preset-env 7.19.4
+ autoprefixer 10.4.12
+ babel-loader 8.2.5
+ css-loader 6.7.1
+ html-webpack-plugin 5.5.0
+ mini-css-extract-plugin 2.6.1
+ postcss 8.4.17
+ postcss-loader 7.0.1
+ prettier 2.7.1
+ style-loader 3.3.1
+ webpack-dev-server 4.11.1
[webpack-cli] Project has been initialised with webpack!