Skip to content

Commit

Permalink
add data-packages-strategy param support and set defaultStrategy to '…
Browse files Browse the repository at this point in the history
…flat'
  • Loading branch information
hthetiot authored and cdebost committed Feb 11, 2019
1 parent c2c27da commit 561710c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
56 changes: 34 additions & 22 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
}
},
"promise": {
"exports": Promise,
global: "Promise",
export: "Promise",
location: "node_modules/bluebird/js/browser/bluebird.min.js",
Expand All @@ -206,6 +207,10 @@
}
};

function moduleHasExport(module) {
return module.exports !== null && module.exports !== void 0;
}

function bootModule(id) {
//console.log('bootModule', id, factory);

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>Mr demo</title>
<style type="text/css"> body { font-family: sans-serif; } </style>

<script src="../node_modules/mr/bootstrap.js" data-module="index"></script>
<script src="../bootstrap.js" data-module="index"></script>
</head>
<body>
<h1>Hello Mr</h1>
Expand Down

0 comments on commit 561710c

Please sign in to comment.