´úÂëÖеÄд·¨
react
<Route path="movieDetail/:id"
getComponent={
(nextState, callback)=> {
require.ensure([], (require)=> {
callback(null, require("../containers/MovieDetailContainer.js").default)
}, "movieDetail")
}
}
/>
vue
const MovieContainer= r => require.ensure([], () => r(require('../containers/MovieContainer.vue')),'movie')
5.4. ×é¼þ¿â°´Ðè¼ÓÔØ´¦Àí
5.4.1. reactÖеÄantd°´Ðè¼ÓÔØÅäÖÃ
npm install babel-plugin-import -save-dev
// ÐÞ¸ÄbabelÅäÖÃÎļþ
{
"presets": ["es2015", "react","stage-0","stage-1","stage-2","stage-3"],
"plugins": [
["import", { "libraryName": "antd","style": "css" }]
]
}
5.4.2. vueÖеÄelement°´Ðè¼ÓÔØÅäÖÃ
npm install babel-plugin-component-save-dev
// ÐÞ¸ÄbabelÅäÖÃÎļþ
{
"presets": [
"es2015",
"react",
"stage-0",
"stage-1",
"stage-2",
"stage-3"
],
"plugins": [
"transform-runtime",
[
"component",
[
{
"libraryName": "element-ui",
"styleLibraryName": "theme-default"
}
]
]
]
}
6. ÆäËû
6.1. ÆôÓÃsource-map
ÏÖÔڵĴúÂëÊǺϲ¢ÒÔºóµÄ´úÂ룬²»ÀûÓÚÅÅ´íºÍ¶¨Î»£¬Ö»ÐèÒªÔÚconfigÖÐÌí¼Ó
devtool: 'source-map',
output: {
sourceMapFilename: '[name].map'
},
ÕâÑù³ö´íÒÔºó¾Í»á²ÉÓÃsource-mapµÄÐÎʽֱ½ÓÏÔʾÄã³ö´í´úÂëµÄλÖá£
6.2. ¼ÓÔØjQuery plugin»òÕßÆäËûlegacyµÚÈý·½¿â
ÄãµÄÏîÄ¿ÓÐʱºò»áÒª¼ÓÔظ÷ÖÖ¸÷ÑùµÄµÚÈý·½¿â£¬±ÈÈçһЩjQueryµÄ²å¼þ£¬ËüÃǶ¼ÒÀÀµjQuery£¬µ«Èç¹ûÖ±½ÓÒýÓþͻᱨ´íjQuery is not undefinedÕâÀàµÄ´íÎó£¬ÒòΪһЩÀϵĿⲻ֧³ÖAMD»òÕßCommonJSµÈһЩÏȽøµÄ¸ñʽ£¬ËùÒÔÓм¸ÖÖ·½·¨½â¾ö
// plugin.js
(function ($) {
const shade = "#556b2f";
$.fn.greenify = function() {
this.css( "color", shade );
return this;
};
}(jQuery));
6.2.1. ·½·¨Ò»£ºwebpack.ProvidePlugin
×÷Ó㺰ÑÒ»¸öÈ«¾Ö±äÁ¿²åÈëµ½ËùÓеĴúÂëÖÐ
ÐÞ¸ÄÅäÖÃÎļþ
plugins: [
//provide $, jQuery and window.jQuery to every script
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
})
]
//Õâ¸öÒ²²»ÐèÒªÁË ÒòΪ$, jQuery, window.jQueryÒѾ±»×¢È뵽ÿһ¸öjsÖУ¬¶¼¿ÉÒÔÖ±½ÓʹÓÃÁË
//import $ from 'jquery';
import './plugin.js';
...
myPromise.then((number) => {
$('body').append('<p>promise result is ' + number + ' now is ' + moment().format() + '</p>');
//call our jquery plugin!
$('p').greenify();
});
6.2.2. ·½·¨¶þ£ºimport-loader
npm install imports-loader --save-dev
ÐÞ¸ÄÅäÖÃÎļþ
//×¢ÒâÕâÖÖд·¨ ÎÒÃÇ°ÑjQueryÕâ¸ö±äÁ¿Ö±½Ó²åÈëµ½plugin.jsÀïÃæÁË
//Ï൱ÓÚÔÚÕâ¸öÎļþµÄ¿ªÊ¼Ìí¼ÓÁË var jQuery = require('jquery');
import 'imports?jQuery=jquery!./plugin.js';
//ºóÃæµÄ´úÂëÒ»Ñù
myPromise.then((number) => {
$('body').append('<p>promise result is ' + number + ' now is ' + moment().format() + '</p>');
//call our jquery plugin!
$('p').greenify();
});
6.3. exports-loader
´ÓÄ£¿éÖе¼³ö±äÁ¿µÄ¹¦ÄÜ
// Ö±½Ó°ó¶¨ÔÚÈ«¾Ö±äÁ¿windowÉÏÃæ
// Hello.js
window.Hello = function(){
console.log('say hello.');
}
//È»ºóÎÒÃÇÔÚindex.jsÎļþÀïÈ¥ÒýÓÃÕâ¸öHello.js:var hello = require('./Hello.js');¡£ÕâÑùÒýÓõĽá¹ûÊDZäÁ¿helloÊÇ
//undefined¡£ÒòΪÎÒÃÇÔÚHello.jsÎļþÀïûÓÐдmodule.exports=window.Hello£¬ËùÒÔindex.jsÀïÎÒÃÇrequireµÄ½á¹û¾ÍÊÇ
//undefined¡£Õâ¸öʱºò£¬exports-loader¾ÍÅÉÉÏÓó¡ÁË¡£ÎÒÃÇÖ»ÓðÑindex.jsµÄ´úÂëÉÔ΢¸Ä¶¯Ò»Ï£ºvar hello = require
//('exports?window.Hello!./Hello.js');£¬Õâ¸öʱºò£¬´úÂë¾ÍÄÜÕýÈ·µÄÔËÐÐÁË¡£±äÁ¿hello¾ÍÊÇÎÒÃǶ¨ÒåµÄwindow.helloÀ²¡£
//var hello = require('exports?window.Hello!./Hello.js');ÕâÐдúÂ룬µÈÓÚÔÚHello.jsÀï¼ÓÉÏÒ»¾ämodule
//.exports=window.Hello£¬ËùÒÔÎÒÃDzÅÄÜÕýÈ·µÄµ¼Èë¡£
6.4. expose-loader
°ÑÒ»¸öÄ£¿éµ¼³ö²¢¸¶¸øÒ»¸öÈ«¾Ö±äÁ¿
require("expose?libraryName!./file.js");
// Exposes the exports for file.js to the global context on property "libraryName".
// In web browsers, window.libraryName is then available
//Àý×ÓÖеÄ×¢ÊÍÊÇ˵°Ñfile.jsÖÐexports³öÀ´µÄ±äÁ¿¸¶¸øÈ«¾Ö±äÁ¿"libraryName"¡£¼ÙÈçfile.jsÖÐÓдúÂëmodule.exports=1£¬ÄÇô
//require("expose?libraryName!./file.js")ºówindow.libraryNameµÄÖµ¾ÍΪ1£¨ÎÒÃÇÕâÀïÖ»ÌÖÂÛä¯ÀÀÆ÷»·¾³£©
6.5. resolveÊôÐÔ
resolve: {
//×¢ÒâÒ»ÏÂ, extensions webpack2µÚÒ»¸ö²»ÊÇ¿Õ×Ö·û´®! ¶ÔÓ¦²»ÐèÒªºó׺µÄÇé¿ö.
extensions: ['.js', '.json', '.sass', '.scss', '.less', 'jsx', '.vue'],
//Ä£¿é±ðÃû¶¨Ò壬·½±ãºóÐøÖ±½ÓÒýÓñðÃû£¬ÎÞÐë¶àд³¤³¤µÄµØÖ·
alias: {
'assets': path.resolve(__dirname, './src/assets'),
'components': path.resolve(__dirname, './src/components')
}
},
6.6. externalÊôÐÔ
// ÅäÖÃÁËÕâ¸öÊôÐÔÖ®ºó vue ºÍ vue-routerÕâЩµÚÈý·½µÄ°ü¶¼²»»á±»¹¹½¨½ø js ÖУ¬ÄÇôÎÒÃǾÍÐèҪͨ¹ý cdn ½øÐÐÎļþµÄÒýÓÃÁË
//externals¶ÔÏóµÄkeyÊǸørequireʱÓõģ¬±ÈÈçrequire('vue'),£¬¶ÔÏóµÄvalue±íʾµÄÊÇÈçºÎÔÚglobal£¨¼´window£©ÖзÃÎʵ½¸Ã¶ÔÏó£¬ÕâÀïÊÇwindow.Vue
externals: {
'vue': 'Vue',
'vue-router': 'VueRouter'
},
ÃâÔð˵Ã÷
1¡¢±¾²©¿ÍÖеÄÎÄÕÂÕª×ÔÍøÉϵÄÖڶ಩¿Í£¬½ö×÷Ϊ×Ô¼ºÖªÊ¶µÄ²¹³äºÍÕûÀí£¬²¢·ÖÏí¸øÆäËûÐèÒªµÄcoder£¬²»»áÓÃÓÚÉÌÓÃ
2¡¢ÒòΪºÜ¶à²©¿ÍµÄµØÖ·ÒѾ¼Ç²»Çå³þÁË£¬ËùÒÔ²»»áÔÚÕâÀï±êÃ÷³ö´¦
¡¡