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

add tree-shaking to ProvidedDependency #15627

Merged
merged 2 commits into from Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 53 additions & 7 deletions lib/dependencies/ProvidedDependency.js
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually you don't need that for tree shaking.

these [sg]etIds stuff is only used for https://github.com/webpack/webpack/blob/main/lib/optimize/SideEffectsFlagPlugin.js and that checks only for HarmonyExportImportedSpecifierDependency and HarmonyImportSpecifierDependency.

/**
* 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
Expand All @@ -54,22 +96,22 @@ 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);
}

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);
}
}
Expand All @@ -90,6 +132,7 @@ class ProvidedDependencyTemplate extends ModuleDependency.Template {
dependency,
source,
{
runtime,
runtimeTemplate,
moduleGraph,
chunkGraph,
Expand All @@ -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 ${
Expand All @@ -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}`
Expand Down
2 changes: 2 additions & 0 deletions test/configCases/plugins/provide-plugin/a.js
@@ -0,0 +1,2 @@
export * as c from "./b";
export * as c2 from "./harmony2";
7 changes: 7 additions & 0 deletions test/configCases/plugins/provide-plugin/b.js
@@ -0,0 +1,7 @@
export function square(x) {
return x * x;
}

export function cube(x) {
return x * x * x;
}
2 changes: 2 additions & 0 deletions test/configCases/plugins/provide-plugin/harmony2.js
@@ -0,0 +1,2 @@
export const a = 1;
export const aUsed = __webpack_exports_info__.a.used;
5 changes: 5 additions & 0 deletions test/configCases/plugins/provide-plugin/index.js
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions test/configCases/plugins/provide-plugin/webpack.config.js
Expand Up @@ -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"],
Expand Down