Skip to content

Commit 47cf7c3

Browse files
Cache module id with module info
1 parent d9623d9 commit 47cf7c3

1 file changed

Lines changed: 40 additions & 37 deletions

File tree

index.js

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,39 @@ function getImplicitModuleRoot() {
7676
return implicitModuleRoot;
7777
}
7878

79+
function getModuleId(modulePath) {
80+
// Use moduleRoot if set
81+
if (moduleRootAbsolute) {
82+
return path
83+
.relative(moduleRootAbsolute, modulePath)
84+
.replace(extensionReplaceRegEx, '')
85+
.replace(leadingPathSegmentRegEx, '');
86+
}
87+
88+
// Search for explicit module id
89+
if (fileNodes[modulePath]) {
90+
for (const comment of fileNodes[modulePath].comments) {
91+
if (!/@module(?=\s)/.test(comment.value)) {
92+
continue;
93+
}
94+
95+
const explicitModuleId = comment.value
96+
.split(/@module(?=\s)/)[1]
97+
.split(/\n+\s*\*\s*@\w+/)[0] // Split before the next tag
98+
.replace(/\n+\s*\*|\{[^\}]*\}/g, '') // Remove new lines with asterisks, and type annotations
99+
.trim();
100+
101+
if (explicitModuleId) {
102+
return explicitModuleId;
103+
}
104+
}
105+
}
106+
107+
return path
108+
.relative(getImplicitModuleRoot(), modulePath)
109+
.replace(extensionReplaceRegEx, '');
110+
}
111+
79112
function getModuleInfo(modulePath, parser) {
80113
if (!moduleInfos[modulePath]) {
81114
if (!fileNodes[modulePath]) {
@@ -88,7 +121,10 @@ function getModuleInfo(modulePath, parser) {
88121
fileNodes[modulePath] = parser.astBuilder.build(file, modulePath);
89122
}
90123

91-
moduleInfos[modulePath] = {namedExports: {}};
124+
moduleInfos[modulePath] = {
125+
id: getModuleId(modulePath),
126+
namedExports: {},
127+
};
92128

93129
const moduleInfo = moduleInfos[modulePath];
94130
const node = fileNodes[modulePath];
@@ -127,39 +163,6 @@ function getDelimiter(modulePath, symbol) {
127163
return getModuleInfo(modulePath).namedExports[symbol] ? '.' : '~';
128164
}
129165

130-
function getModuleId(modulePath) {
131-
// Use moduleRoot if set
132-
if (moduleRootAbsolute) {
133-
return path
134-
.relative(moduleRootAbsolute, modulePath)
135-
.replace(extensionReplaceRegEx, '')
136-
.replace(leadingPathSegmentRegEx, '');
137-
}
138-
139-
// Search for explicit module id
140-
if (fileNodes[modulePath]) {
141-
for (const comment of fileNodes[modulePath].comments) {
142-
if (!/@module(?=\s)/.test(comment.value)) {
143-
continue;
144-
}
145-
146-
const explicitModuleId = comment.value
147-
.split(/@module(?=\s)/)[1]
148-
.split(/\n+\s*\*\s*@\w+/)[0] // Split before the next tag
149-
.replace(/\n+\s*\*|\{[^\}]*\}/g, '') // Remove new lines with asterisks, and type annotations
150-
.trim();
151-
152-
if (explicitModuleId) {
153-
return explicitModuleId;
154-
}
155-
}
156-
}
157-
158-
return path
159-
.relative(getImplicitModuleRoot(), modulePath)
160-
.replace(extensionReplaceRegEx, '');
161-
}
162-
163166
function withJsExt(filePath) {
164167
return filePath.replace(extensionEnsureRegEx, '.js');
165168
}
@@ -336,7 +339,7 @@ exports.astNodeVisitor = {
336339
);
337340

338341
if (getModuleInfo(absolutePath, parser)) {
339-
const moduleId = getModuleId(absolutePath);
342+
const moduleId = moduleInfos[absolutePath];
340343

341344
const exportName = identifier.defaultImport
342345
? getDefaultExportName(absolutePath)
@@ -404,7 +407,7 @@ exports.astNodeVisitor = {
404407
);
405408

406409
if (getModuleInfo(rel, parser)) {
407-
const moduleId = getModuleId(rel);
410+
const moduleId = moduleInfos[rel];
408411

409412
const name =
410413
exportName === 'default'
@@ -460,7 +463,7 @@ exports.astNodeVisitor = {
460463
);
461464

462465
if (getModuleInfo(absolutePath, parser)) {
463-
const moduleId = getModuleId(absolutePath);
466+
const moduleId = moduleInfos[absolutePath];
464467

465468
const exportName = identifier.defaultImport
466469
? getDefaultExportName(absolutePath)

0 commit comments

Comments
 (0)