Skip to content

Commit

Permalink
feat: Add minor performance improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
avocadowastaken committed Jul 30, 2021
1 parent 6a8026b commit ea7ef45
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
14 changes: 7 additions & 7 deletions lib/internal/DependencyTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ class DependencyTree {
this.nodeModule = nodeModule;

/**
* @type {Map<string, Dependency>}
* @readonly
* @type {null | Map<string, Dependency>}
* @protected
*/
this.items = new Map();
this.items = null;

/**
* @type {Map<string, Array<import('@babel/types').Statement>>}
Expand Down Expand Up @@ -295,8 +294,9 @@ class DependencyTree {
}

/** @returns {Map<string, Dependency>} */
get dependencies() {
if (!this.items.size) {
getDependencies() {
if (!this.items) {
this.items = new Map();
for (const dependency of this.collectDependencies()) {
this.items.set(dependency.id, dependency);
}
Expand All @@ -309,7 +309,7 @@ class DependencyTree {
* @param {import("@babel/types").ImportSpecifier | import("@babel/types").ImportDefaultSpecifier | import("@babel/types").ImportNamespaceSpecifier} node
* @returns {null | import("@babel/types").ImportDeclaration}
*/
process(node) {
findReplacement(node) {
const { types: t } = this.babel;

if (t.isImportNamespaceSpecifier(node)) {
Expand All @@ -323,7 +323,7 @@ class DependencyTree {
? node.imported.name
: node.imported.value;

const dependency = this.dependencies.get(moduleName);
const dependency = this.getDependencies().get(moduleName);

if (dependency) {
const localName = t.identifier(node.local.name);
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/PluginOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,20 @@ class PluginOptions {
* @protected
*/
constructor(modules) {
/**
* @type {Map<string, NodeModule>}
* @protected
*/
this.modules = modules;
}

/**
* @param {string} id
* @returns {undefined | NodeModule}
*/
findNodeModule(id) {
return this.modules.get(id);
}
}

module.exports = PluginOptions;
20 changes: 13 additions & 7 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,29 @@ module.exports = function plugin(babel) {
if (!specifiers.length || importKind === "type") return;

const pluginOptions = PluginOptions.parse(opts);
const nodeModule = pluginOptions.modules.get(source.value);
const nodeModule = pluginOptions.findNodeModule(source.value);
if (!nodeModule) return;
const tree = DependencyTree.create(nodeModule, babel);

/** @type {Set<import("@babel/types").Node>} */
const removedSpecifiers = new Set();

for (const specifier of specifiers) {
const replacement = tree.process(specifier);
const replacement = tree.findReplacement(specifier);

if (replacement) {
declaration.node.specifiers = declaration.node.specifiers.filter(
(x) => x !== specifier
);

removedSpecifiers.add(specifier);
declaration.insertBefore(replacement);
}
}

if (!declaration.node.specifiers.length) declaration.remove();
if (removedSpecifiers.size === declaration.node.specifiers.length) {
declaration.remove();
} else {
declaration.node.specifiers = declaration.node.specifiers.filter(
(specifier) => !removedSpecifiers.has(specifier)
);
}
},
},
};
Expand Down
5 changes: 2 additions & 3 deletions test/testExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ const DependencyTree = require("../lib/internal/DependencyTree");
* @returns {unknown[]}
*/
module.exports = function testExports(id) {
const { dependencies } = DependencyTree.create(NodeModule.get(id), babel);

return Array.from(dependencies.values(), (dependency) => [
const tree = DependencyTree.create(NodeModule.get(id), babel);
return Array.from(tree.getDependencies().values(), (dependency) => [
dependency.id,
dependency.internalID,
dependency.source,
Expand Down

0 comments on commit ea7ef45

Please sign in to comment.