Skip to content

Commit

Permalink
Read package-lock.json during bootstrap instead
Browse files Browse the repository at this point in the history
  • Loading branch information
cdebost committed Jan 26, 2019
1 parent c59cf63 commit 122984a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
72 changes: 41 additions & 31 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
function finallyHandler() {
// remove clutter
if (script.parentNode) {
script.parentNode.removeChild(script);
script.parentNode.removeChild(script);
}
}

Expand All @@ -64,11 +64,11 @@
}
} else {
throw new Error("Platform not supported");
}
}
}

exports.initBrowser = function initBrowser() {

return {

resolveUrl: (function makeResolveUrl() {
Expand Down Expand Up @@ -118,7 +118,7 @@

if (!params) {
params = this._params = {};

// Find the <script> that loads us, so we can divine our
// parameters from its attributes.
var i, j, match, script, attr, name,
Expand Down Expand Up @@ -172,7 +172,7 @@

return params;
},

loadPackage: function (dependency, config, packageDescription) {
return mr.loadPackage(dependency, config, packageDescription);
},
Expand Down Expand Up @@ -221,8 +221,8 @@
var module = dependencies[id];

if (
module &&
moduleHasExport(module) === false &&
module &&
moduleHasExport(module) === false &&
typeof module.factory === "function"
) {
module.exports = module.factory(bootModule, (module.exports = {})) || module.exports;
Expand All @@ -241,9 +241,9 @@
if (!dependencies.hasOwnProperty(id)) {
return;
}

dependencies[id].factory = factory;

for (id in dependencies) {
if (dependencies.hasOwnProperty(id)) {
// this causes the function to exit if there are any remaining
Expand All @@ -262,7 +262,7 @@

// Restore inital Boostrap
if (initalBoostrap) {
global.bootstrap = initalBoostrap;
global.bootstrap = initalBoostrap;
}

// At least bootModule in order
Expand All @@ -275,10 +275,10 @@

// 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';
var defaultStrategy = params.packagesStrategy || 'nested';

function bootstrapModuleScript(module, strategy) {
module.strategy = strategy || defaultStrategy;
module.strategy = strategy || defaultStrategy;
var locationRoot = strategy === "flat" ? params.packagesLocation : params.location;
module.script = resolveUrl(locationRoot, module.location);
loadScript(module.script, function (err, script) {
Expand All @@ -293,7 +293,7 @@
defaultStrategy = module.strategy;
bootstrapModule(module.id, function (bootRequire, exports) {
if (module.export) {
exports[module.export] = global[module.global];
exports[module.export] = global[module.global];
} else {
return global[module.global];
}
Expand All @@ -319,11 +319,11 @@
location: module
};
} else {
module.id = id;
module.id = id;
}

// Update dependency
dependencies[id] = module;
dependencies[id] = module;
// Update locatiom from param
module.location = params.hasOwnProperty(paramModuleLocation) ? params[paramModuleLocation] : module.location;

Expand All @@ -336,7 +336,7 @@
bootstrapModuleScript(module);
}
}
}
}
}
};
};
Expand All @@ -363,7 +363,7 @@
params.location = params[paramNamespace + 'Location'] = location;
// Detect command line
if (
typeof process !== "undefined" &&
typeof process !== "undefined" &&
typeof process.argv !== "undefined"
) {

Expand All @@ -381,20 +381,20 @@
}

params.module = PATH.basename(module);
params.package = PATH.dirname(FS.realpathSync(module)) + "/";
params.package = PATH.dirname(FS.realpathSync(module)) + "/";
}
}
}

return params;
return params;
},

loadPackage: function (dependency, config, packageDescription) {
return mr.loadPackage(dependency, config, packageDescription);
},

bootstrap: function (callback) {


var self = this,
params = self.getParams();
Expand Down Expand Up @@ -437,7 +437,8 @@
params = platform.getParams(),
location = params.location,
applicationModuleId = params.module || "",
applicationLocation = miniURL.resolve(mrRequire.getLocation(), params.package || ".");
applicationLocation = miniURL.resolve(mrRequire.getLocation(), params.package || "."),
loadRequire, loadPackageLock;

// execute the preloading plan and stall the fallback module loader
// until it has finished
Expand All @@ -451,11 +452,11 @@
global.montageDefine = function (hash, id, module) {
return getDefinition(hash, id).resolve(module);
};

global.bundleLoaded = function (name) {
return getDefinition(name).resolve();
};

var preloading = Promise.resolve();
config.preloaded = preloading.promise;

Expand All @@ -477,10 +478,19 @@
}));
}

return mrRequire.loadPackage({
loadRequire = mrRequire.loadPackage({
location: params.location,
hash: params.hash
}, config).then(function (boostrapRequire) {
}, config);

loadPackageLock = mrRequire.loadPackageLock({
location: applicationLocation
});

return Promise.all([loadRequire, loadPackageLock])
.then(function (res) {
var bootstrapRequire = res[0],
packageLock = res[1];

if ("autoPackage" in params) {
mrRequire.injectPackageDescription(applicationLocation, {
Expand All @@ -490,17 +500,17 @@
}, config);
}

return boostrapRequire.loadPackage({
return bootstrapRequire.loadPackage({
location: applicationLocation,
hash: params.applicationHash
}).then(function (applicationRequire) {

}, { packageLock: packageLock })
.then(function (applicationRequire) {
// Self exports
applicationRequire.inject("bootstrap", exports);

// Default free module
applicationRequire.inject("mini-url", miniURL);
applicationRequire.inject("promise", mrPromise);
applicationRequire.inject("promise", mrPromise);
applicationRequire.inject("require", applicationRequire);

return applicationRequire.async(applicationModuleId);
Expand All @@ -510,7 +520,7 @@
};

if (
typeof window !== "undefined" ||
typeof window !== "undefined" ||
(typeof module === 'object' && module.exports &&
typeof require !== "undefined")
) {
Expand All @@ -525,4 +535,4 @@
// may cause additional exports to be injected:
exports.getPlatform();
}
}));
}));
10 changes: 2 additions & 8 deletions require.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@
};

exports.loadPackageLock = function (dependency, config) {
var read = config.read || exports.read,
var read = (config && config.read) || exports.read,
packageLockLocation = URL.resolve(dependency.location, "package-lock.json");
return read(packageLockLocation)
.then(function (content) {
Expand Down Expand Up @@ -1205,13 +1205,7 @@
if (typeof packageDescription === "object") {
pkg = exports.injectLoadedPackageDescription(location, packageDescription, config);
} else {
pkg = exports.loadPackageLock(dependency, config)
.then(function (packageLock) {
if (packageLock) {
config.packageLock = packageLock;
}
return config.loadPackage(dependency);
});
pkg = config.loadPackage(dependency);
}
if (typeof pkg.then === "function") {
pkg = pkg.then(function (pkg) {
Expand Down

0 comments on commit 122984a

Please sign in to comment.