From 561710c24d23dbd80300fc2a3ffb7939204b3a6a Mon Sep 17 00:00:00 2001 From: Harold Thetiot Date: Wed, 28 Feb 2018 14:11:29 -0800 Subject: [PATCH] add data-packages-strategy param support and set defaultStrategy to 'flat' --- bootstrap.js | 56 ++++++++++++++++++++++++++++++------------------- demo/index.html | 2 +- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/bootstrap.js b/bootstrap.js index 1f8c9486..01c955e7 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -195,6 +195,7 @@ } }, "promise": { + "exports": Promise, global: "Promise", export: "Promise", location: "node_modules/bluebird/js/browser/bluebird.min.js", @@ -206,6 +207,10 @@ } }; + function moduleHasExport(module) { + return module.exports !== null && module.exports !== void 0; + } + function bootModule(id) { //console.log('bootModule', id, factory); @@ -217,7 +222,7 @@ if ( module && - typeof module.exports === "undefined" && + moduleHasExport(module) === false && typeof module.factory === "function" ) { module.exports = module.factory(bootModule, (module.exports = {})) || module.exports; @@ -268,26 +273,35 @@ callback(mrRequire, mrPromise, miniURL); } - function bootstrapModuleScript(module, err, script) { - if (err) { - // Fallback on flat strategy for missing nested module - if (module.strategy === 'nested') { - module.strategy = 'flat'; - module.script = resolveUrl(resolveUrl(location, '../../'), module.location); - loadScript(module.script, bootstrapModuleScript.bind(null, module)); - } else { - throw err; - } - } else if (module.export || module.global) { - - bootstrapModule(module.id, function (bootRequire, exports) { - if (module.export) { - exports[module.export] = global[module.global]; + // This define if the script should be loaded has "nested" of "flat" dependencies in packagesLocation. + // Change to "nested" for npm 2 support or add data-packages-strategy="nested" on montage.js script tag. + var defaultStrategy = params.packagesStrategy || 'nested'; + + function bootstrapModuleScript(module, strategy) { + module.strategy = strategy || defaultStrategy; + var locationRoot = strategy === "flat" ? params.packagesLocation : params.location; + module.script = resolveUrl(locationRoot, module.location); + loadScript(module.script, function (err, script) { + if (err) { + if (module.strategy === defaultStrategy) { + var nextStrategy = module.strategy === 'flat' ? 'nested' : 'flat'; + bootstrapModuleScript(module, nextStrategy); } else { - return global[module.global]; + throw err; } - }); - } + } else if (module.export || module.global) { + defaultStrategy = module.strategy; + bootstrapModule(module.id, function (bootRequire, exports) { + if (module.export) { + exports[module.export] = global[module.global]; + } else { + return global[module.global]; + } + }); + } else if (!module.factory && !module.exports) { + throw new Error('Unable to load module ' + module.id); + } + }); } // Expose bootstrap @@ -319,9 +333,7 @@ } else if (typeof module.shim !== "undefined") { bootstrapModule(module.id, module.shim); } else { - module.strategy = "nested"; - module.script = resolveUrl(location, module.location); - loadScript(module.script, bootstrapModuleScript.bind(null, module)); + bootstrapModuleScript(module); } } } diff --git a/demo/index.html b/demo/index.html index 4cff6ba0..80c942fe 100644 --- a/demo/index.html +++ b/demo/index.html @@ -4,7 +4,7 @@ Mr demo - +

Hello Mr