Skip to content

Commit

Permalink
refactor top level declarations
Browse files Browse the repository at this point in the history
- add topLevelDeclarations to code generation results
- set topLevelDeclarations=undefined if any child topLevelDeclarations=undefined
  • Loading branch information
vankop committed Feb 7, 2022
1 parent fee6837 commit 58a2fd0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
18 changes: 15 additions & 3 deletions lib/library/AssignLibraryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const isNameValid = name => {
return !KEYWORD_REGEX.test(name) && IDENTIFIER_REGEX.test(name);
};

const topLevelDeclarationContradictionError = base =>
`it declares '${base}' on top-level, which conflicts with the current library output.`;

/**
* @param {string[]} accessor variable plus properties
* @param {number} existingLength items of accessor that are existing already
Expand Down Expand Up @@ -222,8 +225,12 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
* @param {LibraryContext<T>} libraryContext context
* @returns {string | undefined} bailout reason
*/
embedInRuntimeBailout(module, { chunk }, { options, compilation }) {
const topLevelDeclarations =
embedInRuntimeBailout(
module,
{ chunk, codeGenerationResults },
{ options, compilation }
) {
let topLevelDeclarations =
module.buildInfo && module.buildInfo.topLevelDeclarations;
if (!topLevelDeclarations)
return "it doesn't tell about top level declarations.";
Expand All @@ -234,7 +241,12 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
);
const base = fullNameResolved[0];
if (topLevelDeclarations.has(base))
return `it declares '${base}' on top-level, which conflicts with the current library output.`;
return topLevelDeclarationContradictionError(base);
const { data } = codeGenerationResults.get(module, chunk.runtime);
topLevelDeclarations = data.get("topLevelDeclarations");
if (topLevelDeclarations && topLevelDeclarations.has(base)) {
return topLevelDeclarationContradictionError(base);
}
}

/**
Expand Down
11 changes: 6 additions & 5 deletions lib/optimize/ConcatenatedModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,13 +822,11 @@ class ConcatenatedModule extends Module {
const topLevelDeclarations = this.buildInfo.topLevelDeclarations;
if (topLevelDeclarations !== undefined) {
for (const decl of m.buildInfo.topLevelDeclarations) {
// reserved names will always be renamed
if (RESERVED_NAMES.has(decl)) continue;
// TODO actually this is incorrect since with renaming there could be more
// We should do the renaming during build
topLevelDeclarations.add(decl);
}
}
} else {
this.buildInfo.topLevelDeclarations = undefined;
}

// populate assets
Expand Down Expand Up @@ -1112,7 +1110,7 @@ class ConcatenatedModule extends Module {
// List of all used names to avoid conflicts
const allUsedNames = new Set(RESERVED_NAMES);
// Top level declarations should be updated with respect of renaming
const topLevelDeclarations = this.buildInfo.topLevelDeclarations;
const topLevelDeclarations = new Set(this.buildInfo.topLevelDeclarations);

// List of additional names in scope for module references
/** @type {Map<string, { usedNames: Set<string>, alreadyCheckedScopes: Set<TODO> }>} */
Expand Down Expand Up @@ -1625,6 +1623,9 @@ ${defineGetters}`
const data = new Map();
if (chunkInitFragments.length > 0)
data.set("chunkInitFragments", chunkInitFragments);
if (topLevelDeclarations.size > 0) {
data.set("topLevelDeclarations", topLevelDeclarations);
}

/** @type {CodeGenerationResult} */
const resultEntry = {
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/StatsTestCases.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ chunk (runtime: main) trees.js (trees) 215 bytes [rendered]
`;

exports[`StatsTestCases should print correct stats for ignore-warnings 1`] = `
"asset main.js 989 bytes [emitted] (name: main)
"asset main.js 1.37 KiB [emitted] (name: main)
orphan modules 617 bytes [orphan] 9 modules
./index.js + 9 modules 790 bytes [built] [code generated]
Expand Down
1 change: 1 addition & 0 deletions test/configCases/concatenate-modules/issue-13022/a.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const doc = console;
const setTimeout = 1;

export default 1;
2 changes: 2 additions & 0 deletions test/configCases/concatenate-modules/issue-13022/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "./a";

setTimeout(() => {}, 0);

const doc = console;

export default 1;
Expand Down

0 comments on commit 58a2fd0

Please sign in to comment.