Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix pureDep returns null in some js files #16701

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3332,6 +3332,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
dependencyTemplates,
runtimeTemplate,
runtime,
runtimes,
codeGenerationResults: results,
compilation: this
});
Expand Down
3 changes: 2 additions & 1 deletion lib/DependencyTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
* @property {ChunkGraph} chunkGraph the chunk graph
* @property {Set<string>} runtimeRequirements the requirements for runtime
* @property {Module} module current module
* @property {RuntimeSpec} runtime current runtimes, for which code is generated
* @property {RuntimeSpec} runtime current runtime, for which code is generated
* @property {RuntimeSpec[]} [runtimes] current runtimes, for which code is generated
* @property {InitFragment<GenerateContext>[]} initFragments mutable array of init fragments for the current module
* @property {ConcatenationScope=} concatenationScope when in a concatenated module, information about other concatenated modules
* @property {CodeGenerationResults} codeGenerationResults the code generation results
Expand Down
1 change: 1 addition & 0 deletions lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @property {ChunkGraph} chunkGraph the chunk graph
* @property {Set<string>} runtimeRequirements the requirements for runtime
* @property {RuntimeSpec} runtime the runtime
* @property {RuntimeSpec[]} [runtimes] the runtimes
* @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
* @property {CodeGenerationResults=} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
* @property {string} type which kind of code should be generated
Expand Down
3 changes: 2 additions & 1 deletion lib/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ const makeSerializable = require("./util/makeSerializable");
* @property {RuntimeTemplate} runtimeTemplate the runtime template
* @property {ModuleGraph} moduleGraph the module graph
* @property {ChunkGraph} chunkGraph the chunk graph
* @property {RuntimeSpec} runtime the runtimes code should be generated for
* @property {RuntimeSpec} runtime the runtime code should be generated for
* @property {RuntimeSpec[]} [runtimes] the runtimes code should be generated for
* @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
* @property {CodeGenerationResults} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
* @property {Compilation=} compilation the compilation
Expand Down
2 changes: 2 additions & 0 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ class NormalModule extends Module {
moduleGraph,
chunkGraph,
runtime,
runtimes,
concatenationScope,
codeGenerationResults,
sourceTypes
Expand Down Expand Up @@ -1208,6 +1209,7 @@ class NormalModule extends Module {
chunkGraph,
runtimeRequirements,
runtime,
runtimes,
concatenationScope,
codeGenerationResults,
getData,
Expand Down
16 changes: 12 additions & 4 deletions lib/dependencies/PureExpressionDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const { UsageState } = require("../ExportsInfo");
const makeSerializable = require("../util/makeSerializable");
const { filterRuntime } = require("../util/runtime");
const { filterRuntime, deepMergeRuntime } = require("../util/runtime");
const NullDependency = require("./NullDependency");

/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
Expand Down Expand Up @@ -84,15 +84,23 @@ PureExpressionDependency.Template = class PureExpressionDependencyTemplate exten
apply(
dependency,
source,
{ chunkGraph, moduleGraph, runtime, runtimeTemplate, runtimeRequirements }
{
chunkGraph,
moduleGraph,
runtime,
runtimes,
runtimeTemplate,
runtimeRequirements
}
) {
const dep = /** @type {PureExpressionDependency} */ (dependency);

const usedByExports = dep.usedByExports;
if (usedByExports !== false) {
const selfModule = moduleGraph.getParentModule(dep);
const exportsInfo = moduleGraph.getExportsInfo(selfModule);
const runtimeCondition = filterRuntime(runtime, runtime => {
const merged = deepMergeRuntime(runtimes, runtime);
const runtimeCondition = filterRuntime(merged, runtime => {
for (const exportName of usedByExports) {
if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused) {
return true;
Expand All @@ -104,7 +112,7 @@ PureExpressionDependency.Template = class PureExpressionDependencyTemplate exten
if (runtimeCondition !== false) {
const condition = runtimeTemplate.runtimeConditionExpression({
chunkGraph,
runtime,
runtime: merged,
runtimeCondition,
runtimeRequirements
});
Expand Down
1 change: 1 addition & 0 deletions lib/javascript/JavascriptGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class JavascriptGenerator extends Generator {
chunkGraph: generateContext.chunkGraph,
module,
runtime: generateContext.runtime,
runtimes: generateContext.runtimes,
runtimeRequirements: generateContext.runtimeRequirements,
concatenationScope: generateContext.concatenationScope,
codeGenerationResults: generateContext.codeGenerationResults,
Expand Down
19 changes: 18 additions & 1 deletion lib/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ exports.getEntryRuntime = (compilation, name, options) => {
* @param {boolean} deterministicOrder enforce a deterministic order
* @returns {void}
*/
exports.forEachRuntime = (runtime, fn, deterministicOrder = false) => {
const forEachRuntime = (runtime, fn, deterministicOrder = false) => {
if (runtime === undefined) {
fn(undefined);
} else if (typeof runtime === "string") {
Expand All @@ -69,6 +69,7 @@ exports.forEachRuntime = (runtime, fn, deterministicOrder = false) => {
}
}
};
exports.forEachRuntime = forEachRuntime;

const getRuntimesKey = set => {
set.sort();
Expand Down Expand Up @@ -218,6 +219,22 @@ const mergeRuntime = (a, b) => {
};
exports.mergeRuntime = mergeRuntime;

/**
* @param {RuntimeSpec[]} runtimes first
* @param {RuntimeSpec} runtime second
* @returns {RuntimeSpec} merged
*/
exports.deepMergeRuntime = (runtimes, runtime) => {
if (!Array.isArray(runtimes)) {
return runtime;
}
let merged = runtime;
for (const r of runtimes) {
merged = mergeRuntime(runtime, r);
}
return merged;
};

/**
* @param {RuntimeCondition} a first
* @param {RuntimeCondition} b second
Expand Down
23 changes: 21 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1241,10 +1241,15 @@ declare interface CodeGenerationContext {
chunkGraph: ChunkGraph;

/**
* the runtimes code should be generated for
* the runtime code should be generated for
*/
runtime: RuntimeSpec;

/**
* the runtimes code should be generated for
*/
runtimes?: RuntimeSpec[];

/**
* when in concatenated module, information about other concatenated modules
*/
Expand Down Expand Up @@ -2765,10 +2770,15 @@ declare interface DependencyTemplateContext {
module: Module;

/**
* current runtimes, for which code is generated
* current runtime, for which code is generated
*/
runtime: RuntimeSpec;

/**
* current runtimes, for which code is generated
*/
runtimes?: RuntimeSpec[];

/**
* mutable array of init fragments for the current module
*/
Expand Down Expand Up @@ -4371,6 +4381,11 @@ declare interface GenerateContext {
*/
runtime: RuntimeSpec;

/**
* the runtimes
*/
runtimes?: RuntimeSpec[];

/**
* when in concatenated module, information about other concatenated modules
*/
Expand Down Expand Up @@ -13005,6 +13020,10 @@ declare namespace exports {
export let runtimeEqual: (a: RuntimeSpec, b: RuntimeSpec) => boolean;
export let compareRuntime: (a: RuntimeSpec, b: RuntimeSpec) => 0 | 1 | -1;
export let mergeRuntime: (a: RuntimeSpec, b: RuntimeSpec) => RuntimeSpec;
export let deepMergeRuntime: (
runtimes: RuntimeSpec[],
runtime: RuntimeSpec
) => RuntimeSpec;
export let mergeRuntimeCondition: (
a: RuntimeCondition,
b: RuntimeCondition,
Expand Down