Skip to content

Commit

Permalink
Refactor shared code
Browse files Browse the repository at this point in the history
  • Loading branch information
bworline committed Oct 2, 2023
1 parent dc66572 commit b14922c
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 124 deletions.
73 changes: 12 additions & 61 deletions lib/dependencies/CommonJsFullRequireDependency.js
Expand Up @@ -7,6 +7,7 @@

const Template = require("../Template");
const { equals } = require("../util/ArrayHelpers");
const { getTrimmedIdsAndRange } = require("../util/chainedImports");
const makeSerializable = require("../util/makeSerializable");
const propertyAccess = require("../util/propertyAccess");
const ModuleDependency = require("./ModuleDependency");
Expand Down Expand Up @@ -127,30 +128,16 @@ CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemp
runtimeRequirements
});

const ids = dep.names;
let trimmedIds = this._trimIdsToThoseImported(ids, moduleGraph, dep);

let [rangeStart, rangeEnd] = dep.range;
if (trimmedIds.length !== ids.length) {
// The array returned from dep.idRanges is right-aligned with the array returned from dep.names.
// Meaning, the two arrays may not always have the same number of elements, but the last element of
// dep.idRanges corresponds to [the expression fragment to the left of] the last element of dep.names.
// Use this to find the correct replacement range based on the number of ids that were trimmed.
const idx =
dep.idRanges === undefined
? -1 /* trigger failure case below */
: dep.idRanges.length + (trimmedIds.length - ids.length);
if (idx < 0 || idx >= dep.idRanges.length) {
// cspell:ignore minifiers
// Should not happen but we can't throw an error here because of backward compatibility with
// external plugins in wp5. Instead, we just disable trimming for now. This may break some minifiers.
trimmedIds = ids;
// TODO webpack 6 remove the "trimmedIds = ids" above and uncomment the following line instead.
// throw new Error("Missing range starts data for id replacement trimming.");
} else {
[rangeStart, rangeEnd] = dep.idRanges[idx];
}
}
const {
trimmedRange: [trimmedRangeStart, trimmedRangeEnd],
trimmedIds
} = getTrimmedIdsAndRange(
dep.names,
dep.range,
dep.idRanges,
moduleGraph,
dep
);

if (importedModule) {
const usedImported = moduleGraph
Expand All @@ -167,43 +154,7 @@ CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemp
: `${requireExpr}${access}`;
}
}
source.replace(rangeStart, rangeEnd - 1, requireExpr);
}

/**
* @summary Determine which IDs in the id chain are actually referring to namespaces or imports,
* and which are deeper member accessors on the imported object. Only the former should be re-rendered.
* @param {string[]} ids ids
* @param {ModuleGraph} moduleGraph moduleGraph
* @param {CommonJsFullRequireDependency} dependency dependency
* @returns {string[]} generated code
*/
_trimIdsToThoseImported(ids, moduleGraph, dependency) {
let trimmedIds = [];
const exportsInfo = moduleGraph.getExportsInfo(
moduleGraph.getModule(dependency)
);
let currentExportsInfo = /** @type {ExportsInfo=} */ exportsInfo;
for (let i = 0; i < ids.length; i++) {
if (i === 0 && ids[i] === "default") {
continue; // ExportInfo for the next level under default is still at the root ExportsInfo, so don't advance currentExportsInfo
}
const exportInfo = currentExportsInfo.getExportInfo(ids[i]);
if (exportInfo.provided === false) {
// json imports have nested ExportInfo for elements that things that are not actually exported, so check .provided
trimmedIds = ids.slice(0, i);
break;
}
const nestedInfo = exportInfo.getNestedExportsInfo();
if (!nestedInfo) {
// once all nested exports are traversed, the next item is the actual import so stop there
trimmedIds = ids.slice(0, i + 1);
break;
}
currentExportsInfo = nestedInfo;
}
// Never trim to nothing. This can happen for invalid imports (e.g. import { notThere } from "./module", or import { anything } from "./missingModule")
return trimmedIds.length ? trimmedIds : ids;
source.replace(trimmedRangeStart, trimmedRangeEnd - 1, requireExpr);
}
};

Expand Down
76 changes: 13 additions & 63 deletions lib/dependencies/HarmonyImportSpecifierDependency.js
Expand Up @@ -9,6 +9,7 @@ const Dependency = require("../Dependency");
const {
getDependencyUsedByExportsCondition
} = require("../optimize/InnerGraph");
const { getTrimmedIdsAndRange } = require("../util/chainedImports");
const makeSerializable = require("../util/makeSerializable");
const propertyAccess = require("../util/propertyAccess");
const HarmonyImportDependency = require("./HarmonyImportDependency");
Expand Down Expand Up @@ -324,30 +325,16 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
// Skip rendering depending when dependency is conditional
if (connection && !connection.isTargetActive(runtime)) return;

const ids = dep.getIds(moduleGraph); // determine minimal set of IDs.
let trimmedIds = this._trimIdsToThoseImported(ids, moduleGraph, dep);

let [rangeStart, rangeEnd] = dep.range;
if (trimmedIds.length !== ids.length) {
// The array returned from dep.idRanges is right-aligned with the array returned from dep.getIds.
// Meaning, the two arrays may not always have the same number of elements, but the last element of
// dep.idRanges corresponds to [the expression fragment to the left of] the last element of dep.getIds.
// Use this to find the correct replacement range based on the number of ids that were trimmed.
const idx =
dep.idRanges === undefined
? -1 /* trigger failure case below */
: dep.idRanges.length + (trimmedIds.length - ids.length);
if (idx < 0 || idx >= dep.idRanges.length) {
// cspell:ignore minifiers
// Should not happen but we can't throw an error here because of backward compatibility with
// external plugins in wp5. Instead, we just disable trimming for now. This may break some minifiers.
trimmedIds = ids;
// TODO webpack 6 remove the "trimmedIds = ids" above and uncomment the following line instead.
// throw new Error("Missing range starts data for id replacement trimming.");
} else {
[rangeStart, rangeEnd] = dep.idRanges[idx];
}
}
const {
trimmedRange: [trimmedRangeStart, trimmedRangeEnd],
trimmedIds
} = getTrimmedIdsAndRange(
dep.getIds(moduleGraph),
dep.range,
dep.idRanges,
moduleGraph,
dep
);

const exportExpr = this._getCodeForIds(
dep,
Expand All @@ -356,47 +343,10 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
trimmedIds
);
if (dep.shorthand) {
source.insert(rangeEnd, `: ${exportExpr}`);
source.insert(trimmedRangeEnd, `: ${exportExpr}`);
} else {
source.replace(rangeStart, rangeEnd - 1, exportExpr);
}
}

/**
* @summary Determine which IDs in the id chain are actually referring to namespaces or imports,
* and which are deeper member accessors on the imported object. Only the former should be re-rendered.
* @param {string[]} ids ids
* @param {ModuleGraph} moduleGraph moduleGraph
* @param {HarmonyImportSpecifierDependency} dependency dependency
* @returns {string[]} generated code
*/
_trimIdsToThoseImported(ids, moduleGraph, dependency) {
/** @type {string[]} */
let trimmedIds = [];
const exportsInfo = moduleGraph.getExportsInfo(
/** @type {Module} */ (moduleGraph.getModule(dependency))
);
let currentExportsInfo = /** @type {ExportsInfo=} */ exportsInfo;
for (let i = 0; i < ids.length; i++) {
if (i === 0 && ids[i] === "default") {
continue; // ExportInfo for the next level under default is still at the root ExportsInfo, so don't advance currentExportsInfo
}
const exportInfo = currentExportsInfo.getExportInfo(ids[i]);
if (exportInfo.provided === false) {
// json imports have nested ExportInfo for elements that things that are not actually exported, so check .provided
trimmedIds = ids.slice(0, i);
break;
}
const nestedInfo = exportInfo.getNestedExportsInfo();
if (!nestedInfo) {
// once all nested exports are traversed, the next item is the actual import so stop there
trimmedIds = ids.slice(0, i + 1);
break;
}
currentExportsInfo = nestedInfo;
source.replace(trimmedRangeStart, trimmedRangeEnd - 1, exportExpr);
}
// Never trim to nothing. This can happen for invalid imports (e.g. import { notThere } from "./module", or import { anything } from "./missingModule")
return trimmedIds.length ? trimmedIds : ids;
}

/**
Expand Down
96 changes: 96 additions & 0 deletions lib/util/chainedImports.js
@@ -0,0 +1,96 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/

"use strict";

/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */

/**
* @summary Get the subset of ids and their corresponding range in an id chain that should be re-rendered by webpack.
* Only those in the chain that are actually referring to namespaces or imports should be re-rendered.
* Deeper member accessors on the imported object should not be re-rendered. If deeper member accessors are re-rendered,
* there is a potential loss of meaning with rendering a quoted accessor as an unquoted accessor, or vice versa,
* because minifiers treat quoted accessors differently. e.g. import { a } from "./module"; a["b"] vs a.b
* @param {string[]} untrimmedIds chained ids
* @param {Range} untrimmedRange range encompassing allIds
* @param {Range[]} ranges cumulative range of ids for each of allIds
* @param {ModuleGraph} moduleGraph moduleGraph
* @param {Dependency} dependency dependency
* @returns {{trimmedIds: string[], trimmedRange: Range}} computed trimmed ids and cumulative range of those ids
*/
exports.getTrimmedIdsAndRange = (
untrimmedIds,
untrimmedRange,
ranges,
moduleGraph,
dependency
) => {
let trimmedIds = trimIdsToThoseImported(
untrimmedIds,
moduleGraph,
dependency
);
let trimmedRange = untrimmedRange;
if (trimmedIds.length !== untrimmedIds.length) {
// The array returned from dep.idRanges is right-aligned with the array returned from dep.names.
// Meaning, the two arrays may not always have the same number of elements, but the last element of
// dep.idRanges corresponds to [the expression fragment to the left of] the last element of dep.names.
// Use this to find the correct replacement range based on the number of ids that were trimmed.
const idx =
ranges === undefined
? -1 /* trigger failure case below */
: ranges.length + (trimmedIds.length - untrimmedIds.length);
if (idx < 0 || idx >= ranges.length) {
// cspell:ignore minifiers
// Should not happen but we can't throw an error here because of backward compatibility with
// external plugins in wp5. Instead, we just disable trimming for now. This may break some minifiers.
trimmedIds = untrimmedIds;
// TODO webpack 6 remove the "trimmedIds = ids" above and uncomment the following line instead.
// throw new Error("Missing range starts data for id replacement trimming.");
} else {
trimmedRange = ranges[idx];
}
}

return { trimmedIds, trimmedRange };
};

/**
* @summary Determine which IDs in the id chain are actually referring to namespaces or imports,
* and which are deeper member accessors on the imported object.
* @param {string[]} ids untrimmed ids
* @param {ModuleGraph} moduleGraph moduleGraph
* @param {Dependency} dependency dependency
* @returns {string[]} trimmed ids
*/
function trimIdsToThoseImported(ids, moduleGraph, dependency) {
let trimmedIds = [];
const exportsInfo = moduleGraph.getExportsInfo(
moduleGraph.getModule(dependency)
);
let currentExportsInfo = /** @type {ExportsInfo=} */ exportsInfo;
for (let i = 0; i < ids.length; i++) {
if (i === 0 && ids[i] === "default") {
continue; // ExportInfo for the next level under default is still at the root ExportsInfo, so don't advance currentExportsInfo
}
const exportInfo = currentExportsInfo.getExportInfo(ids[i]);
if (exportInfo.provided === false) {
// json imports have nested ExportInfo for elements that things that are not actually exported, so check .provided
trimmedIds = ids.slice(0, i);
break;
}
const nestedInfo = exportInfo.getNestedExportsInfo();
if (!nestedInfo) {
// once all nested exports are traversed, the next item is the actual import so stop there
trimmedIds = ids.slice(0, i + 1);
break;
}
currentExportsInfo = nestedInfo;
}
// Never trim to nothing. This can happen for invalid imports (e.g. import { notThere } from "./module", or import { anything } from "./missingModule")
return trimmedIds.length ? trimmedIds : ids;
}

0 comments on commit b14922c

Please sign in to comment.