Skip to content

Commit e291bb9

Browse files
committed
sourcemap-tools: improve sourcemap search from source file
1 parent dfc71b9 commit e291bb9

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

tools/cli/tests/sourcemaps/add-sources.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ describe('add-sources', () => {
270270
});
271271

272272
assert(result.isErr(), 'result should be an error');
273-
expect(result.data).toMatch(/invalid1\.js\.map/);
273+
expect(result.data).toMatch(/invalid11\.js\.map/);
274274
}),
275275
);
276276

tools/sourcemap-tools/src/SourceProcessor.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,54 @@ export class SourceProcessor {
170170
}
171171

172172
public async getSourceMapPathFromSource(source: string, sourcePath: string): Promise<string | undefined> {
173-
const resolveFile = (filePath: string) =>
174-
pipe(filePath, statFile, (result) =>
175-
!result.isOk() || result.data.isFile()
176-
? filePath
177-
: (path.join(filePath, path.basename(sourcePath) + '.map') as string | undefined),
173+
const matchAll = (str: string, regex: RegExp) => {
174+
const result: RegExpMatchArray[] = [];
175+
// eslint-disable-next-line no-constant-condition
176+
while (true) {
177+
const match = regex.exec(str);
178+
if (!match) {
179+
return result;
180+
}
181+
result.push(match);
182+
}
183+
};
184+
185+
const checkFile = (filePath: string) =>
186+
pipe(filePath, statFile, (result) => (result.isOk() && result.data.isFile() ? filePath : undefined));
187+
188+
const sourceMapName = path.basename(sourcePath) + '.map';
189+
const checkFileInDir = (dir: string) =>
190+
pipe(dir, statFile, (result) =>
191+
result.isOk() && result.data.isDirectory()
192+
? // If path exists and is a directory, check if file exists in that dir
193+
checkFile(path.join(dir, sourceMapName))
194+
: // If path does not exist or is not a directory, check if file exists in dir of that path
195+
checkFile(path.join(path.dirname(dir), sourceMapName)),
178196
);
179197

180-
return pipe(source.match(/^\/\/# sourceMappingURL=(.+)$/m), (match) =>
181-
!match || !match[1]
182-
? undefined
183-
: pipe(match[1], (match) => path.resolve(path.dirname(sourcePath), match), resolveFile),
184-
);
198+
const matches = matchAll(source, /^\s*\/\/# sourceMappingURL=(.+)$/gm);
199+
if (!matches.length) {
200+
return checkFileInDir(sourcePath);
201+
}
202+
203+
for (const match of matches.reverse()) {
204+
const file = match[1];
205+
if (!file) {
206+
continue;
207+
}
208+
209+
const fullPath = path.resolve(path.dirname(sourcePath), file);
210+
if (await checkFile(fullPath)) {
211+
return fullPath;
212+
}
213+
214+
const fileInDir = await checkFileInDir(fullPath);
215+
if (fileInDir) {
216+
return fileInDir;
217+
}
218+
}
219+
220+
return checkFileInDir(sourcePath);
185221
}
186222

187223
public async addSourcesToSourceMap(

0 commit comments

Comments
 (0)