Skip to content

Commit

Permalink
refactor top level declarations usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Feb 11, 2022
1 parent de7ec7b commit cd3ec1d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ const { isSourceEqual } = require("./util/source");

/**
* @typedef {Object} ChunkHashContext
* @property {CodeGenerationResults} codeGenerationResults results of code generation
* @property {RuntimeTemplate} runtimeTemplate the runtime template
* @property {ModuleGraph} moduleGraph the module graph
* @property {ChunkGraph} chunkGraph the chunk graph
Expand Down Expand Up @@ -4137,6 +4138,7 @@ This prevents using hashes of each other and should be avoided.`);
chunk.updateHash(chunkHash, chunkGraph);
this.hooks.chunkHash.call(chunk, chunkHash, {
chunkGraph,
codeGenerationResults: this.codeGenerationResults,
moduleGraph: this.moduleGraph,
runtimeTemplate: this.runtimeTemplate
});
Expand Down
23 changes: 22 additions & 1 deletion lib/javascript/JavascriptModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const printGeneratedCodeForStack = (module, code) => {
/**
* @typedef {Object} RenderBootstrapContext
* @property {Chunk} chunk the chunk
* @property {CodeGenerationResults} codeGenerationResults results of code generation
* @property {RuntimeTemplate} runtimeTemplate the runtime template
* @property {ModuleGraph} moduleGraph the module graph
* @property {ChunkGraph} chunkGraph the chunk graph
Expand Down Expand Up @@ -332,6 +333,7 @@ class JavascriptModulesPlugin {
{
hash: "0000",
chunk,
codeGenerationResults: context.codeGenerationResults,
chunkGraph: context.chunkGraph,
moduleGraph: context.moduleGraph,
runtimeTemplate: context.runtimeTemplate
Expand All @@ -344,6 +346,7 @@ class JavascriptModulesPlugin {
compilation.hooks.contentHash.tap("JavascriptModulesPlugin", chunk => {
const {
chunkGraph,
codeGenerationResults,
moduleGraph,
runtimeTemplate,
outputOptions: {
Expand All @@ -361,6 +364,7 @@ class JavascriptModulesPlugin {
{
hash: "0000",
chunk,
codeGenerationResults,
chunkGraph: compilation.chunkGraph,
moduleGraph: compilation.moduleGraph,
runtimeTemplate: compilation.runtimeTemplate
Expand All @@ -373,6 +377,7 @@ class JavascriptModulesPlugin {
}
hooks.chunkHash.call(chunk, hash, {
chunkGraph,
codeGenerationResults,
moduleGraph,
runtimeTemplate
});
Expand Down Expand Up @@ -978,7 +983,13 @@ class JavascriptModulesPlugin {
* @returns {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} the generated source of the bootstrap code
*/
renderBootstrap(renderContext, hooks) {
const { chunkGraph, moduleGraph, chunk, runtimeTemplate } = renderContext;
const {
chunkGraph,
codeGenerationResults,
moduleGraph,
chunk,
runtimeTemplate
} = renderContext;

const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk);

Expand Down Expand Up @@ -1102,8 +1113,18 @@ class JavascriptModulesPlugin {
);
result.allowInlineStartup = false;
}

let data;
if (codeGenerationResults.has(entryModule, chunk.runtime)) {
const result = codeGenerationResults.get(
entryModule,
chunk.runtime
);
data = result.data;
}
if (
result.allowInlineStartup &&
(!data || !data.get("topLevelDeclarations")) &&
(!entryModule.buildInfo ||
!entryModule.buildInfo.topLevelDeclarations)
) {
Expand Down
16 changes: 5 additions & 11 deletions lib/library/AssignLibraryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ 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 @@ -230,8 +227,10 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
{ chunk, codeGenerationResults },
{ options, compilation }
) {
let topLevelDeclarations =
module.buildInfo && module.buildInfo.topLevelDeclarations;
const { data } = codeGenerationResults.get(module, chunk.runtime);
const topLevelDeclarations =
(data && data.get("topLevelDeclarations")) ||
(module.buildInfo && module.buildInfo.topLevelDeclarations);
if (!topLevelDeclarations)
return "it doesn't tell about top level declarations.";
const fullNameResolved = this._getResolvedFullName(
Expand All @@ -241,12 +240,7 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
);
const base = fullNameResolved[0];
if (topLevelDeclarations.has(base))
return topLevelDeclarationContradictionError(base);
const { data } = codeGenerationResults.get(module, chunk.runtime);
topLevelDeclarations = data && data.get("topLevelDeclarations");
if (topLevelDeclarations && topLevelDeclarations.has(base)) {
return topLevelDeclarationContradictionError(base);
}
return `it declares '${base}' on top-level, which conflicts with the current library output.`;
}

/**
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 1.37 KiB [emitted] (name: main)
"asset main.js 989 bytes [emitted] (name: main)
orphan modules 617 bytes [orphan] 9 modules
./index.js + 9 modules 790 bytes [built] [code generated]
Expand Down
10 changes: 10 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,11 @@ declare abstract class ChunkGroup {
}
type ChunkGroupOptions = RawChunkGroupOptions & { name?: string };
declare interface ChunkHashContext {
/**
* results of code generation
*/
codeGenerationResults: CodeGenerationResults;

/**
* the runtime template
*/
Expand Down Expand Up @@ -9215,6 +9220,11 @@ declare interface RenderBootstrapContext {
*/
chunk: Chunk;

/**
* results of code generation
*/
codeGenerationResults: CodeGenerationResults;

/**
* the runtime template
*/
Expand Down

0 comments on commit cd3ec1d

Please sign in to comment.