add tree-shaking to ProvidedDependency

This commit is contained in:
Ivan Kopeykin 2022-04-04 17:59:01 +03:00
parent 05ebf5bba6
commit 0aabe2a884
6 changed files with 71 additions and 7 deletions

View File

@ -5,19 +5,23 @@
"use strict";
const Dependency = require("../Dependency");
const InitFragment = require("../InitFragment");
const makeSerializable = require("../util/makeSerializable");
const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("../util/Hash")} Hash */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
const idsSymbol = Symbol("ProvidedDependency.ids");
/**
* @param {string[]|null} path the property path array
@ -29,10 +33,16 @@ const pathToString = path =>
: "";
class ProvidedDependency extends ModuleDependency {
constructor(request, identifier, path, range) {
/**
* @param {string} request request
* @param {string} identifier identifier
* @param {string[]} ids ids
* @param {[number, number]} range range
*/
constructor(request, identifier, ids, range) {
super(request);
this.identifier = identifier;
this.path = path;
this.ids = ids;
this.range = range;
this._hashUpdate = undefined;
}
@ -45,6 +55,38 @@ class ProvidedDependency extends ModuleDependency {
return "esm";
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {string[]} the imported ids
*/
getIds(moduleGraph) {
const meta = moduleGraph.getMetaIfExisting(this);
if (meta === undefined) return this.ids;
const ids = meta[idsSymbol];
return ids !== undefined ? ids : this.ids;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {string[]} ids the imported ids
* @returns {void}
*/
setIds(moduleGraph, ids) {
moduleGraph.getMeta(this)[idsSymbol] = ids;
}
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
* @returns {(string[] | ReferencedExport)[]} referenced exports
*/
getReferencedExports(moduleGraph, runtime) {
let ids = this.getIds(moduleGraph);
if (ids.length === 0) return Dependency.EXPORTS_OBJECT_REFERENCED;
return [ids];
}
/**
* Update the hash
* @param {Hash} hash hash to be updated
@ -54,7 +96,7 @@ class ProvidedDependency extends ModuleDependency {
updateHash(hash, context) {
if (this._hashUpdate === undefined) {
this._hashUpdate =
this.identifier + (this.path ? this.path.join(",") : "null");
this.identifier + (this.ids ? this.ids.join(",") : "null");
}
hash.update(this._hashUpdate);
}
@ -62,14 +104,14 @@ class ProvidedDependency extends ModuleDependency {
serialize(context) {
const { write } = context;
write(this.identifier);
write(this.path);
write(this.ids);
super.serialize(context);
}
deserialize(context) {
const { read } = context;
this.identifier = read();
this.path = read();
this.ids = read();
super.deserialize(context);
}
}
@ -90,6 +132,7 @@ class ProvidedDependencyTemplate extends ModuleDependency.Template {
dependency,
source,
{
runtime,
runtimeTemplate,
moduleGraph,
chunkGraph,
@ -98,6 +141,9 @@ class ProvidedDependencyTemplate extends ModuleDependency.Template {
}
) {
const dep = /** @type {ProvidedDependency} */ (dependency);
const connection = moduleGraph.getConnection(dep);
const exportsInfo = moduleGraph.getExportsInfo(connection.module);
const usedName = exportsInfo.getUsedName(dep.getIds(moduleGraph), runtime);
initFragments.push(
new InitFragment(
`/* provided dependency */ var ${
@ -107,7 +153,7 @@ class ProvidedDependencyTemplate extends ModuleDependency.Template {
chunkGraph,
request: dep.request,
runtimeRequirements
})}${pathToString(dep.path)};\n`,
})}${pathToString(/** @type {string[]} */ (usedName))};\n`,
InitFragment.STAGE_PROVIDES,
1,
`provided ${dep.identifier}`

View File

@ -0,0 +1,2 @@
export * as c from "./b";
export * as c2 from "./harmony2";

View File

@ -0,0 +1,7 @@
export function square(x) {
return x * x;
}
export function cube(x) {
return x * x * x;
}

View File

@ -0,0 +1,2 @@
export const a = 1;
export const aUsed = __webpack_exports_info__.a.used;

View File

@ -48,6 +48,11 @@ it("should provide a module for a property request", function() {
expect(x).toBe("fff");
});
it("should tree-shake unused exports", function() {
expect(aa1(2)).toBe(8);
expect(es2015_aUsed).toBe(false);
});
it("should provide ES2015 modules", function() {
expect((es2015.default)).toBe("ECMAScript 2015");
expect((es2015.alias)).toBe("ECMAScript Harmony");

View File

@ -6,6 +6,8 @@ module.exports = {
aaa: "./aaa",
"bbb.ccc": "./bbbccc",
dddeeefff: ["./ddd", "eee", "3-f"],
aa1: ["./a", "c", "cube"],
es2015_aUsed: ["./harmony2", "aUsed"],
"process.env.NODE_ENV": "./env",
es2015: "./harmony",
es2015_name: ["./harmony", "default"],