From e3839292fc1c8f38159f59048bcda0621383c10e Mon Sep 17 00:00:00 2001 From: zodern Date: Fri, 3 Feb 2023 16:37:00 -0600 Subject: [PATCH 1/2] Fix load order of weak deps --- tools/isobuild/compiler-plugin.js | 20 +++++++++++++++++--- tools/isobuild/linker.js | 20 ++++++++------------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/tools/isobuild/compiler-plugin.js b/tools/isobuild/compiler-plugin.js index a14c5e79b6..cc966bf873 100644 --- a/tools/isobuild/compiler-plugin.js +++ b/tools/isobuild/compiler-plugin.js @@ -1072,7 +1072,10 @@ export class PackageSourceBatch { // depends on something). self.importedSymbolToPackageName = {}; // map from symbol to supplying package name - self.deps = []; + + // List of packages (including weak) that need to load before + // this unibuild + self.orderedDeps = new Set(); } async init() { @@ -1098,14 +1101,25 @@ export class PackageSourceBatch { }, (depUnibuild, { weak, unordered }) => { let packageName = depUnibuild.pkg.name; + if (!unordered) { + self.orderedDeps.add(packageName); + } + _.each(depUnibuild.declaredExports, function (symbol) { // Slightly hacky implementation of test-only exports. if (! symbol.testOnly || self.unibuild.pkg.isTest) { self.importedSymbolToPackageName[symbol.name] = packageName; } }); + }); - self.deps.push({ package: packageName, weak, unordered }); + // At this point we can't easily know all weak dependencies + // that are actually used to pass them to eachUsedUnibuild. + // So instead we loop over the packages again to add them. + self.unibuild.uses.forEach(uses => { + if (uses.weak) { + self.orderedDeps.add(uses.package); + } }); self.useMeteorInstall = @@ -1700,7 +1714,7 @@ export class PackageSourceBatch { imports: self.importedSymbolToPackageName, // XXX report an error if there is a package called global-imports includeSourceMapInstructions: isWeb, - deps: self.deps + orderedDeps: Array.from(self.orderedDeps) }; const fileHashes = []; diff --git a/tools/isobuild/linker.js b/tools/isobuild/linker.js index 573e990210..3ac5c50fcd 100644 --- a/tools/isobuild/linker.js +++ b/tools/isobuild/linker.js @@ -941,12 +941,8 @@ var getHeader = function (options) { var isApp = options.name === null; var chunks = []; - let orderedDeps = []; - - options.deps.forEach(dep => { - if (!dep.unordered) { - orderedDeps.push(JSON.stringify(dep.package)) - } + let orderedDeps = options.orderedDeps.map(packageName => { + return JSON.stringify(packageName); }); chunks.push( @@ -1140,9 +1136,9 @@ export var fullLink = Profile("linker.fullLink", async function (inputFiles, { // how to use them in a browser. includeSourceMapInstructions, - // List of packages this bundle directly uses, or is implied by the packages - // it uses - deps + // List of packages this bundle directly uses that need to load + // first. Includes weak dependencies. + orderedDeps }) { buildmessage.assertInJob(); @@ -1159,7 +1155,7 @@ export var fullLink = Profile("linker.fullLink", async function (inputFiles, { // we can be sure the runtime will be available // The main situations it is not available is the core-runtime // package itself, or any build plugins with no dependencies - let hasRuntime = deps.some(entry => entry.unordered !== true); + let hasRuntime = orderedDeps.length > 0; _.each(inputFiles, file => module.addFile(file)); @@ -1194,7 +1190,7 @@ export var fullLink = Profile("linker.fullLink", async function (inputFiles, { imports, packageVariables: [], hasRuntime, - deps + orderedDeps }); let footer = getFooter({ name: null, @@ -1255,7 +1251,7 @@ export var fullLink = Profile("linker.fullLink", async function (inputFiles, { imports, packageVariables: _.union(assignedVariables, declaredExports), hasRuntime, - deps + orderedDeps }); var footer = getFooter({ From dd82506241fee27fd599b53d8c5d81ebf5fc8271 Mon Sep 17 00:00:00 2001 From: zodern Date: Fri, 3 Feb 2023 16:51:57 -0600 Subject: [PATCH 2/2] Simplify adding to orderedDeps --- tools/isobuild/compiler-plugin.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tools/isobuild/compiler-plugin.js b/tools/isobuild/compiler-plugin.js index cc966bf873..cf74531ed1 100644 --- a/tools/isobuild/compiler-plugin.js +++ b/tools/isobuild/compiler-plugin.js @@ -1098,12 +1098,10 @@ export class PackageSourceBatch { skipDebugOnly: true, skipProdOnly: true, skipTestOnly: true, - }, (depUnibuild, { weak, unordered }) => { + }, (depUnibuild) => { let packageName = depUnibuild.pkg.name; - if (!unordered) { - self.orderedDeps.add(packageName); - } + self.orderedDeps.add(packageName); _.each(depUnibuild.declaredExports, function (symbol) { // Slightly hacky implementation of test-only exports.