ããã«ã¡ã¯ã
Habr ïŒ
æè¿ãç¶æ³ã«ãã£ãŠããµã€ãºãã·ã³ãã«ã§å°ããªãªãœãŒã¹ããŒããŒãæ€çŽ¢ããããã«æ±ããããŸããã ããããç§ã®æ€çŽ¢ã¯ãã¹ãŠrequire.jsã«ã€ãªãããŸãããäœããã®çç±ã§ããã¯ç§ã«ã¯äžåãã§ããïŒããã¯å¥ã®
èšäºã®ãããã¯ã§ãïŒã
ãã®ããã
èªè»¢è»ãäœæããåæã«ç·Žç¿ããããšã決å®ãããŸããã
ãã®çµæã
uglify圢åŒã§6.28 KbãGZipã§1.3 Kbã
å ããã³ã³ããŒãã³ããå®è£
ãããŸããã
圌ã®éèŠãªãããããïŒ
- * .jsãš* .cssã®äž¡æ¹ãããŒãã§ããŸãã
- ãããã·ã¹ãã ãå®è£
ããŸããã åããã±ãŒãžã¯åå¥ã®æ§æãæã€ããšãã§ããŸãã
- ããŠã³ããŒãã¯ãããã§è¡ãããŸãã ã€ãŸããç®çã®ããã±ãŒãžã§loadïŒïŒã¡ãœãããåŒã³åºãã ãã§ååã§ããã®äžã«ãããã¹ãŠã®ãã¡ã€ã«ãããŒããããŸãã
- éåæãšé
延èªã¿èŸŒã¿ã¢ãŒãã®äž¡æ¹ã§èªã¿èŸŒãããšãã§ããŸãïŒããã±ãŒãžã®ããŠã³ããŒãã¯ããŒãžå
šäœãèªã¿èŸŒãã åŸã«ã®ã¿å®è¡ãããŸãïŒã
- åºæ¬çãªããã±ãŒãžæäœãç°¡çŽ åããçµã¿èŸŒã¿ã®ããã±ãŒãžãããŒãžã£ãŒããããŸãã ã€ãŸããã¹ãã¬ãŒãžãäœæãåé€ãããŒãã
- åå空éãå®è£
ãããŸãïŒå®éãå®è£
ã¯éåžžã«åçŽã§ãããå°èŠæš¡ãããžã§ã¯ãã®å Žåã¯ãã©ã¹ã§ãïŒã
å®éãããã«ãã®äž»ãªæ©èœã®èª¬æããããŸãã
ç«ã®äžã§ãAJLã䜿çšããçãã³ãŒã¹ãšãããã€ãã®ã³ã³ããŒãã³ãã®éçºã®èª¬æã
AJLãæ¥ç¶ããAJLãæ¥ç¶ãããšãã«æå®ããå¿
èŠãããã®ã¯ãdata-epå±æ§ã ãã§ãã 次ã«äŸã瀺ããŸãïŒããŒã¹ã¬ã€ã¢ãŠãã§AJLãæ¥ç¶ããïŒã
AJLãèªã¿èŸŒãã åŸãEntryPoint.jsãèªã¿èŸŒã¿ãŸããããã§ãããã±ãŒãžãæ§æããŸãã
data-epãåŠæ¹ããããšã¯ã§ããŸããã AJLãæ§æããã®ã«äŸ¿å©ãªå Žæãèªåã§æ±ºå®ããæš©å©ããããŸããEntryPoint.jsã¯ãåºæ¬çã«èªã¿èŸŒã¿æã«ãå®è¡ãããã¹ã¯ãªããã§ããããã±ãŒãžãèšå®ããŸãjQueryããã®ãã©ã°ã€ã³ãããã³ã¹ã¯ãªãããæ¥ç¶ããäŸãèŠãŠã¿ãŸãããã ãã®å Žåãdata-epå±æ§ã䜿çšãããŸããã
EntryPointã¯ããªããããã«èŠããŸãã ååã«ã¯ãããã±ãŒãžã®ååãæå®ããŸãã
assetã§ã¯ãã¢ã»ããURLã®é
åã
configã§ã¯ããã©ã¡ãŒã¿ãŒãæã€ãªããžã§ã¯ãã åèšã§ã6ã€ã®ãã©ã¡ãŒã¿ãŒã
configã«æž¡ãããšãã§ããŸãã
- asyncïŒboolïŒ -éåæã«ããŒããããã©ãã
- lazyïŒboolïŒ -ãŠã£ã³ããŠã®èªã¿èŸŒã¿ãåŸ
ã€
- dependïŒé
åïŒ -äŸåé¢ä¿
- scriptTypeAttrïŒæååïŒ -ãã®ãã©ã¡ãŒã¿ãŒã¯ã¹ã¯ãªããã¿ã€ã= ""ã«è¡šç€ºãããŸã
- linkCssTypeAttrïŒæååïŒ -ãªã³ã¯ã¿ã€ã= ""
- linkCssRelAttrïŒæååïŒ -ãªã³ã¯rel = ""
æ£çŽãªãšããããã©ã¡ãŒã¿ã䜿çšããŠãªããžã§ã¯ãã®ã¿ã°ã®å±æ§ãåãåºããçç±ãç解ã§ããŸããã§ãããEntryPoint.jsã§äœãèµ·ãããŸããïŒ1. jQueryãjQuery PluginsãMy Scriptsãšããååã®3ã€ã®ããã±ãŒãžãäœæãããŸãã äœæã«æåãããšãAJLã¯loadAllïŒïŒã¡ãœãããååšããPackageManagerãè¿ããŸãã ãã®ã¡ãœããã¯ããã±ãŒãžãããŒãããŸãã ãã¹ãŠã®ããã±ãŒãžã¯ãé
åãã«ãŒãããŠloadïŒïŒãåŒã³åºãããšã«ããããŒããããŸãã ããŠã³ããŒãã«åœ±é¿ããå¯èœæ§ã®ããéèŠãªèŠå ã¯ãããŠã³ããŒããããã±ãŒãžã®äœæé ã«çºçããããšã§ãã ãããã£ãŠãæåã«ãæ·±å»ãªãã©ã€ãã©ãªãæå®ãããã以å€ã¯ãã¹ãŠæå®ããæ¹ãé©åã§ãã
2.æåã«jQueryãããŒãããã®ã¯éåæã¢ãŒãã§ã¯ãããŸããã jQueryã®ããŒãåŸããã©ã°ã€ã³ãéå§ãããŸãããjQueryã®ããŒãåŸã«ã®ã¿ã§ãã ãããŠæåŸã«-ããŒãžã®ãã¹ãŠã®ãªãœãŒã¹ãããŒãããåŸïŒãã€ããŒãïŒïŒé
延ããŒãïŒã
ãããã£ãŠãããã±ãŒãžã䜿çšããŠããŸããŸãªæ§æãè¡ãããšãã§ããŸãã ã«ããŽãªãç°ãªã2ã€ã®ããŒãžããããšããŸãã 1ã€ã¯ãŠãŒã¶ãŒçšã®ã³ã³ãããŒã«ããã«ã§ããã1ã€ã¯ã¹ã¯ãªããã®ãã³ãã«ãåããã¯ãŒã«ãªãšãã£ã¿ãŒã§ãã ãããã®ããŒãžã«ããŒãããå¿
èŠãããã¹ã¯ãªãããšã¹ã¿ã€ã«ã¯ãäºãã«å®å
šã«ç°ãªããŸãã å€ãã®ã¹ã¯ãªããã å¿
èŠãªããŒããšäžèŠãªããŒãããã¹ãŠããŒãããªãã§ãã ããã 2ã€ã®ç°ãªãåºæ¬ã¬ã€ã¢ãŠããäœæããŸããïŒ ãªãã§ïŒ
AJLã䜿çšãããšããã®ç¶æ³ã«å¯Ÿãã解決çãåæ§ã®æ¹æ³ã§ææ¡ã§ããŸãã
å¿
èŠãªãã¹ãŠã®ããã±ãŒãžãäœæããEntryPointããããŸãã
AJL({ name: "jQuery", assets: ['js/vendor/jquery.min.js'], config: { async: false } }, { name: "jQuery Plugins", assets: ['js/vendor/jquery.plugin.js', 'js/vendor/jquery.plugin2.js'], config: { depend: ['jQuery'] } }, { name: "Editor Scripts And Styles", assets: ['js/editor/foo.js', 'js/editor/bar.js', 'css/editor/style.css'], config: { depend: ['jQuery Plugins'] } }, { name: "My Dashboard Scripts", assets: ['js/foo.js', 'js/bar.js'], config: { lazy: true } });
loadAllïŒïŒã«æ³šæããŠãã ããã 圌ã¯ããã«ããŸããã ããã±ãŒãžã®èªã¿èŸŒã¿ãå®å
šã«åºå¥ãããã®ã§ãloadAllïŒïŒãåŒã³åºããŸããã äœæããã ãã§ãã ãŸãããšãã£ã¿ãŒãšçµ±èšããã«ã®ãã¥ãŒãããããããããã®ãã¥ãŒã§ç®çã®ããã±ãŒãžãæåã§ããŠã³ããŒãã§ããŸãã
Editor Scripts And Stylesãšããããã±ãŒãžã®ã¿ãããŠã³ããŒãããããšã«æ³šæããŠãã ããã ããã§ã®äŸåé¢ä¿ã®è§£æ±ºã¯ååž°çã«è¡ããããããæåŸã®ãªã³ã¯ãåŒã³åºãããšãã§ããŸãã 次ã«ãjQueryãã©ã°ã€ã³ãããŒãããŠãããjQueryãããŒãããŸãã
ãããã£ãŠãããã±ãŒãžã®ãã§ãŒã³ãæ§ç¯ããäœæ¥ã«æ¬åœã«å¿
èŠãªããã±ãŒãžã®ã¿ãããŠã³ããŒãã§ããŸãã
ãããŒããã®äžã§äœãèµ·ãããŸããïŒæ¬¡ã«ãããã€ãã®AJLã¢ãžã¥ãŒã«ã®éçºæ¹æ³ã«ç§»ããŸãããã
åå空éæãèå³æ·±ãã®ã¯ã17è¡ã®ã³ãŒãã§ã®åå空éã®å®è£
ã§ãã ããã§ã¯ãã¹ãŠãåçŽã§ãããæ¬è³ªã¯åå空éãé
åã®èŠçŽ ãšãã®å埩ã«åå²ããããšã§ãã æåŸã®èŠçŽ ã«å°éãããããã®èŠçŽ ã«ã¢ãžã¥ãŒã«ãå²ãåœãŠãŸãã
åå空éãäœæãããšãã«åŒã³åºãããé¢æ°ã®ã³ãŒããæäŸããŸãã
setNamespace: function (namespace, module) { var parts = namespace.split('.'), parent = window, partsLength, curPart, i;
ãã®çµæãã¢ãžã¥ãŒã«ãéçºãããšãã«ãããªãåçŽãªèšèšã䜿çšã§ããŸãã
AJL("Module.SubModule", function() { return "Hi, I'm Module.SubModule"; });
ããã±ãŒãžãPackageConfigãããŒããŒããã±ãŒãžãšããã±ãŒãžæ§æã¯ããããã¿ã€ãïŒã€ãŸãã¯ã©ã¹ïŒãåããåãªãé¢æ°ã§ãã ããããã£ã«ä¿æããŠããã®ã¯ãããã±ãŒãžã®ååãURLã®é
åãããã±ãŒãžæ§æã®ã€ã³ã¹ã¿ã³ã¹ã ãã§ãã Packageèªäœã®loadïŒïŒã¡ãœããã¯ãcallïŒïŒã䜿çšããŠLoader.jsããéçé¢æ°loadPackageïŒïŒãåŒã³åºããŸãã
load: function () { AJL.Loader.loadPackage.call(this); }
ããã¯ãäžæ£ãªã³ãŒãã®éè€ãã身ãå®ãããã«è¡ãããŸãã ããã±ãŒãžã¯ç°ãªããæ§æã¯ç°ãªããããŒãããŒããŒã¯1ã€ã§ãªããã°ãªããŸããã Loader.jsãšloadPackageïŒïŒã¯ãDOMã«ã¿ã°ãè¿œå ã§ããå Žåãšã§ããªãå Žåã«å®éã«æ±ºå®ãè¡ããŸãã
loadPackage: function () { var helper = AJL.Helper, packageManager = AJL.PackageManager, pack = this, packageAssets = pack.getAssets(), packageConfig = pack.getConfig(), depend = packageConfig.getItem('depend');
äŸåé¢ä¿ã®èªã¿èŸŒã¿ã®å®è£
æ¹æ³ã«æ³šæãæããŸãã ãã®ããã±ãŒãžã«äŸåé¢ä¿ãããå Žåãååž°ããŒããåŒã³åºããŸãã ç§ãã¡ã¯äŸåé¢ä¿ãããŒãããçã
ããã§ãŒã³ãäžã£ãŠãããŸãã
PackageManagerãŸããAJLã®éèŠãªéšåã¯ãããã±ãŒãžã管çããPackageManagerã§ãã ãã®ãããªã³ã¬ã¯ã¿ãŒã ã²ãã¿ãŒãããããªããžã§ã¯ããããã±ãŒãžã®ã€ã³ã¹ã¿ã³ã¹ã§ãããã©ããã ãã§ãªããããã±ãŒãžã€ã³ã¹ã¿ã³ã¹ã®é
åã§èŠæ±ãããååããã§ãã¯ããã»ãã¿ãŒããããŸãã ãã®å Žåããããè¿ãããå¿
èŠãªã¢ã¯ã·ã§ã³ãå®è¡ããŸãã ããšãã°ãèæ
®ãããloadAllïŒïŒé¢æ°ã¯éåžžã®åæã«ãã£ãŠåäœããŸãã
loadAll: function () { var helper = AJL.Helper, curPack; for (var pack in packages) { if (packages.hasOwnProperty(pack)) { curPack = packages[pack]; if (helper.isInstanceOf(curPack, AJL.Package)) { curPack.load(); } } } return this; },
é
åãå埩åŠçãããããã€ã³ã¹ã¿ã³ã¹ããã±ãŒãžã§ããå Žåã¯ãloadïŒïŒãåŒã³åºããŸãã äŸåããã±ãŒãžãããŒããããšããloadByNamesïŒïŒé¢æ°ã䜿çšããŸãã
loadByNames: function (names) { var helper = AJL.Helper, curName, namesLength, i; namesLength = names.length; for (i = 0; i < namesLength; i++) { curName = names[i]; if (packages.hasOwnProperty(curName) && helper.isInstanceOf(packages[curName], AJL.Package)) { packages[curName].load(); } } return this; }
é
åå
šäœãååã§ãœãŒãããã¹ãã¬ãŒãžããã±ãŒãžã«ååããããã©ããã確èªããŸãã ãã¯ããã§ãããã€ã³ã¹ã¿ã³ã¹ããã±ãŒãžã§ããå Žåã¯ãloadïŒïŒãåŒã³åºããŸãã
AjlãããŠæåŸã«ãæãéèŠãªããšã AJLïŒïŒé¢æ°ã
AJL = function () { var packageManager = AJL.PackageManager, namespace = AJL.Namespace, helper = AJL.Helper, packageInstance = {}, packageName = '', packageAssets = [], packageConfig = {}, argLength = arguments.length, argFirst, argSecond, i;
æåã«ãé¢æ°ã«æž¡ãããåŒæ°ã®æ°ãèŠãŠãã ããã äœãéä¿¡ãããªãã£ãå Žåãããã«PackageManagerãè¿ããŸãã 1è¡æž¡ãããå Žåã¯ãããã±ãŒãžã®ååãæäŸãããŠãããšæ³å®ããŠæ€çŽ¢ããŸãã æ€çŽ¢åŸãPackageãªããžã§ã¯ããè¿ããŸãã 2ã€ã®ãã©ã¡ãŒã¿ãŒãæž¡ãããæåã®ãã©ã¡ãŒã¿ãŒãæååã®å Žåã2çªç®ã®ãã©ã¡ãŒã¿ãŒããã®åå空éã«ïŒæåã®ãã©ã¡ãŒã¿ãŒããïŒã¹ããŒããŸãã ãããŠæåŸã«ãäœãèµ·ãããªãã£ãå ŽåããããAJLã®éå§æ§æã§ãããšèãããã®äžã«ãã¹ãŠã®ããã±ãŒãžãäœæããŸãã
ãã®å ŽæãèªãåãèŠã€ãããã¹ãŠã®äººã«æè¬ããŸãã AJLãè©ŠããŠã¿ããå Žåã¯ãå¿
èŠãªãã¹ãŠã®ãªãœãŒã¹ããããŸãã
ã¡ã€ã³ããŒãžGitHubãœãŒã¹ããã¥ã¡ã³ãPSç§ã¯ãã¹ãŠã®åžæãšæ¹å€ã«æè¬ããã§ãããã éçºããããããã€ããã¯ãããŸããã ã¢ã€ãã¢ãå°œããŸãã:)