¶©ÔÄÒ³ÃæºÍ×ÊÁÏÒ³Ãæ¹²ÓÃÐí¶à´úÂ루ÏñReactÒÔ¼°ÆäËûµÄÑùʽºÍ×é¼þµÈ£©¡£webpack¿ÉÒÔ³é³öËûÃǹ²ÓõĴúÂ룬Ȼºó´òÒ»¸öͨÓðüÔÚ¸÷¸öÒ³Ã滺´æ¡£
// webpack.config.js var webpack = require('webpack'); var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js'); module.exports = { entry: { Profile: './profile.js', Feed: './feed.js' }, output: { path: 'build', filename: '[name].js' // Template based on keys in entry above }, plugins: [commonsPlugin] };ÔÚÉÏÒ»²½µÄscript±êǩ֮ǰ¼ÓÉÏ <script src="build/common.js"></script> £¬È»ºó¾¡ÇéÏíÊÜÃâ·ÑµÄ¸ßËÙ»º´æ°É¡£
9¡¢Òì²½¼ÓÔØCommonJSÊÇͬ²½µÄ£¬µ«ÊÇWebpackÌṩһ¸ö·½·¨À´Òì²½µÄÖ¸¶¨ÒÀÀµ¡£ÕâÔÚ¿Í»§¶Ë·ÓÉÖкÜÓÐÓã¬Ã¿¸öÒ³Ã涼ÐèҪ·ÓÉ£¬Äã²»ÏëÖ±µ½ÄãÕæÕýÐèÒªËûÃǵÄʱºò²ÅÏÂÔØÏà¹Ø¹¦ÄÜ¡£
Ö¸¶¨ÄãÏëÒì²½¼ÓÔØµÄ ·Ö¸îµã £¬±ÈÈ磺
if (window.location.pathname === '/feed') { showLoadingState(); require.ensure([], function() { // this syntax is weird but it works hideLoadingState(); require('./feed').show(); // when this function is called, the module is guaranteed to be synchronously available. }); } else if (window.location.pathname === '/profile') { showLoadingState(); require.ensure([], function() { hideLoadingState(); require('./profile').show(); }); }webpack»á×ö½ÓÏÂÀ´µÄ¹¤×÷£¬²¢ÇÒ²úÉú¶îÍâµÄ¿éÎļþ£¬È»ºó¼ÓÔØËûÃÇ¡£
µ±Äã¼ÓÔØÕâЩÎļþµ½htmlµÄscript±êÇ©£¬webpack¼Ù¶¨ËûÃÇÔÚ¸ùĿ¼£¬Äã¿ÉÒÔͨ¹ý output.publicPath À´¶¨Òå:
// webpack.config.js output: { path: "/home/proj/public/assets", //path to where webpack will build your stuff publicPath: "/assets/" //path that will be considered when requiring your files } ÆäËû×ÊÔ´¿´Ò»ÏÂÕæʵµÄ°¸Àý£ºÒ»¸ö³É¹¦µÄÍŶÓÊÇÈçºÎʹÓÃwebpackµÄ¡£ÕâÊÇPete HuntÔÚÈ«Çò¿ªÔ´´ó»áÉϹØÓÚIntagramʹÓõÄwebpackµÄÌÖÂÛ£º ¡£
¡¡