Skip to content

Commit

Permalink
add tree-shaking to ProvidedDependency
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Apr 4, 2022
1 parent 05ebf5b commit 0aabe2a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
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;
}

/**
* 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

0 comments on commit 0aabe2a

Please sign in to comment.