å ¥å£å¯¹è±¡æ¯ç¨äº webpack æ¥æ¾å¼å§æ建 bundle çå°æ¹ãä¸ä¸ææ¯å ¥å£æ件æå¤çç®å½çç»å¯¹è·¯å¾çå符串ã
string
åºç¡ç®å½ï¼ç»å¯¹è·¯å¾ï¼ç¨äºä»é ç½®ä¸è§£æå ¥å£ç¹(entry point)å å è½½å¨(loader)ã
const path = require('path');
module.exports = {
//...
context: path.resolve(__dirname, 'app'),
};
é»è®¤ä½¿ç¨ Node.js è¿ç¨çå½åå·¥ä½ç®å½ï¼ä½æ¯æ¨èå¨é ç½®ä¸ä¼ å ¥ä¸ä¸ªå¼ãè¿ä½¿å¾ä½ çé ç½®ç¬ç«äº CWD(current working directory, å½åå·¥ä½ç®å½)ã
string
[string]
object = { <key> string | [string] | object = { import string | [string], dependOn string | [string], filename string, layer string }}
(function() => string | [string] | object = { <key> string | [string] } | object = { import string | [string], dependOn string | [string], filename string })
å¼å§åºç¨ç¨åºæå è¿ç¨çä¸ä¸ªæå¤ä¸ªèµ·ç¹ãå¦æä¼ å ¥æ°ç»ï¼åä¼å¤çæææ¡ç®ã
å¨æå è½½ç模å ä¸æ¯ å ¥å£èµ·ç¹ã
ä¸ä¸ªéè¦èèçè§åï¼æ¯ä¸ª HTML 页é¢é½æä¸ä¸ªå ¥å£èµ·ç¹ãå页åºç¨(SPA)ï¼ä¸ä¸ªå ¥å£èµ·ç¹ï¼å¤é¡µåºç¨(MPA)ï¼å¤ä¸ªå ¥å£èµ·ç¹ã
module.exports = {
//...
entry: {
home: './home.js',
about: './about.js',
contact: './contact.js',
},
};
å¦æä¼ å
¥ä¸ä¸ªå符串æå符串æ°ç»ï¼chunk ä¼è¢«å½å为 main
ãå¦æä¼ å
¥ä¸ä¸ªå¯¹è±¡ï¼åæ¯ä¸ªå±æ§çé®(key)ä¼æ¯ chunk çå称ï¼è¯¥å±æ§çå¼æè¿°äº chunk çå
¥å£ç¹ã
å¦æä¼ å ¥ä¸ä¸ªå¯¹è±¡ï¼å¯¹è±¡çå±æ§çå¼å¯ä»¥æ¯ä¸ä¸ªå符串ãå符串æ°ç»æè ä¸ä¸ªæ述符(descriptor):
module.exports = {
//...
entry: {
home: './home.js',
shared: ['react', 'react-dom', 'redux', 'react-redux'],
catalog: {
import: './catalog.js',
filename: 'pages/catalog.js',
dependOn: 'shared',
chunkLoading: false, // Disable chunks that are loaded on demand and put everything in the main chunk.
},
personal: {
import: './personal.js',
filename: 'pages/personal.js',
dependOn: 'shared',
chunkLoading: 'jsonp',
asyncChunks: true, // Create async chunks that are loaded on demand.
layer: 'name of layer', // set the layer for an entry point
},
},
};
æ述符è¯æ³å¯ä»¥ç¨æ¥ä¼ å ¥é¢å¤çé项ç»å ¥å£ã
é»è®¤æ
åµä¸ï¼å
¥å£ chunk çè¾åºæ件åæ¯ä» output.filename
ä¸æååºæ¥çï¼ä½ä½ å¯ä»¥ä¸ºç¹å®çå
¥å£æå®ä¸ä¸ªèªå®ä¹çè¾åºæ件åã
module.exports = {
//...
entry: {
app: './app.js',
home: { import: './contact.js', filename: 'pages/[name].js' },
about: { import: './about.js', filename: 'pages/[name].js' },
},
};
æ述符è¯æ³å¨è¿é被ç¨æ¥å° filename
âéé¡¹ä¼ éç»æå®çå
¥å£ç¹ã
é»è®¤æ
åµä¸ï¼æ¯ä¸ªå
¥å£ chunk ä¿åäºå
¨é¨å
¶ç¨ç模å(modules)ãä½¿ç¨ dependOn
éé¡¹ä½ å¯ä»¥ä¸å¦ä¸ä¸ªå
¥å£ chunk å
±äº«æ¨¡å:
module.exports = {
//...
entry: {
app: { import: './app.js', dependOn: 'react-vendors' },
'react-vendors': ['react', 'react-dom', 'prop-types'],
},
};
app
è¿ä¸ª chunk å°±ä¸ä¼å
å« react-vendors
æ¥æç模åäº.
dependOn
é项çä¹å¯ä»¥ä¸ºå符串æ°ç»ï¼
module.exports = {
//...
entry: {
moment: { import: 'moment-mini', runtime: 'runtime' },
reactvendors: { import: ['react', 'react-dom'], runtime: 'runtime' },
testapp: {
import: './wwwroot/component/TestApp.tsx',
dependOn: ['reactvendors', 'moment'],
},
},
};
æ¤å¤ï¼ä½ è¿å¯ä»¥ä½¿ç¨æ°ç»ä¸ºæ¯ä¸ªå ¥å£æå®å¤ä¸ªæ件ï¼
module.exports = {
//...
entry: {
app: { import: ['./app.js', './app2.js'], dependOn: 'react-vendors' },
'react-vendors': ['react', 'react-dom', 'prop-types'],
},
};
å¦æä¼ å ¥ä¸ä¸ªå½æ°ï¼é£ä¹å®å°ä¼å¨æ¯æ¬¡ make äºä»¶ä¸è¢«è°ç¨ã
è¦æ³¨æçæ¯ï¼
make
äºä»¶å¨ webpack å¯å¨åæ¯å½ çå¬æ件åå æ¶é½ä¼è§¦åã
module.exports = {
//...
entry: () => './demo',
};
æè
module.exports = {
//...
entry: () => new Promise((resolve) => resolve(['./demo', './demo2'])),
};
ä¾å¦ï¼ä½ å¯ä»¥ä½¿ç¨å¨æå ¥å£æ¥ä»å¤é¨æ¥æºï¼è¿ç¨æå¡å¨ï¼æ件系ç»å 容æè æ°æ®åºï¼è·åçæ£çå ¥å£ï¼
webpack.config.js
module.exports = {
entry() {
return fetchPathsFromSomeExternalSource(); // è¿åä¸ä¸ªä¼è¢«ç¨å ['src/main-layout.js', 'src/admin-layout.js'] çä¸è¥¿ resolve ç promise
},
};
å½å output.library
é项ç»åï¼å¦æä¼ å
¥çæ¯ä¸ä¸ªæ°ç»ï¼åªææ°ç»çæåä¸ä¸ªæ¡ç®ä¼è¢«å¯¼åºã