Skip to content

Commit 094eb47

Browse files
authored
cli, sourcemap-tools: force snippet/comment generation (#219)
* sourcemap-tools: attempt to add snippets and comments if they do not exist * cli: improve assets logging * cli: trust sourceProcessor with finding sourcemap path * cli: always process file with force passed to sourceProcessor * cli: fix unit tests * sourcemap-tools: add doc about regexp in DebugIdGenerator --------- Co-authored-by: Sebastian Alex <sebastian.alex@saucelabs.com>
1 parent 0a4cfd1 commit 094eb47

File tree

20 files changed

+129
-76
lines changed

20 files changed

+129
-76
lines changed

tools/cli/src/helpers/common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ function resolveSourceMapPath(sourceProcessor: SourceProcessor) {
119119
return pipe(
120120
asset.path,
121121
(path) => sourceProcessor.getSourceMapPathFromSourceFile(path),
122-
R.map((result) => result ?? pathIfExists(`${asset.path}.map`)),
123122
R.map((path) => (path ? Ok(path) : Err('could not find source map for source'))),
124123
R.map((path) => ({
125124
...asset,

tools/cli/src/helpers/logs.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
import { Asset, log, LogLevel, ProcessAssetResult } from '@backtrace/sourcemap-tools';
1+
import { Asset, log, LogLevel } from '@backtrace/sourcemap-tools';
22
import { CliLogger } from '../logger';
33
import { SourceAndSourceMapPaths } from '../models/Asset';
44

55
export function createAssetLogger(
66
logger: CliLogger,
7-
): (level: LogLevel) => <T extends Asset | ProcessAssetResult>(message: string | ((t: T) => string)) => (asset: T) => T;
7+
): (level: LogLevel) => <T extends Asset>(message: string | ((t: T) => string)) => (asset: T) => T;
88
export function createAssetLogger(
99
logger: CliLogger,
1010
level: LogLevel,
11-
): <T extends Asset | ProcessAssetResult>(message: string | ((t: T) => string)) => (asset: T) => T;
11+
): <T extends Asset>(message: string | ((t: T) => string)) => (asset: T) => T;
1212
export function createAssetLogger(logger: CliLogger, level?: LogLevel) {
1313
function logAsset(level: LogLevel) {
1414
const logFn = log(logger, level);
1515

16-
return function logAsset<T extends Asset | ProcessAssetResult>(message: string | ((t: T) => string)) {
16+
return function logAsset<T extends Asset>(message: string | ((t: T) => string)) {
1717
return function logAsset(asset: T) {
18-
return logFn<T>(
19-
(t) =>
20-
`${'name' in t ? t.name : t.asset.name}: ${
21-
typeof message === 'function' ? message(asset) : message
22-
}`,
23-
)(asset);
18+
return logFn<T>((t) => `${t.name}: ${typeof message === 'function' ? message(asset) : message}`)(asset);
2419
};
2520
};
2621
}
@@ -32,4 +27,4 @@ export const logAssets =
3227
(logger: CliLogger, level: LogLevel) =>
3328
(message: string) =>
3429
<T extends SourceAndSourceMapPaths>(assets: T) =>
35-
log(logger, level)<T>(`${assets.source.name}: ${message}`)(assets);
30+
log(logger, level)<T>(`${assets.source.name}:${assets.sourceMap?.name ?? '?'}: ${message}`)(assets);

tools/cli/src/sourcemaps/process.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,28 +199,24 @@ export function processSource(force: boolean) {
199199
sourceMapDebugId: getSourceMapDebugId(sourceAndSourceMap),
200200
});
201201

202-
const shouldProcess = (sourceDebugId: string | undefined, sourceMapDebugId: string | undefined) =>
203-
force || !sourceDebugId || !sourceMapDebugId || sourceDebugId !== sourceMapDebugId;
204-
205202
return async function processSource(asset: SourceAndSourceMap): Promise<ProcessedSourceAndSourceMap> {
206203
return pipe(asset, getDebugIds, ({ sourceDebugId, sourceMapDebugId }) =>
207-
shouldProcess(sourceDebugId, sourceMapDebugId)
208-
? pipe(
209-
asset,
210-
(asset) =>
211-
sourceProcessor.processSourceAndSourceMap(
212-
asset.source.content,
213-
asset.sourceMap.content,
214-
sourceDebugId ?? sourceMapDebugId,
215-
),
216-
(result) =>
217-
({
218-
source: { ...asset.source, content: result.source },
219-
sourceMap: { ...asset.sourceMap, content: result.sourceMap },
220-
debugId: result.debugId,
221-
} as ProcessedSourceAndSourceMap),
222-
)
223-
: ({ ...asset, debugId: sourceDebugId } as ProcessedSourceAndSourceMap),
204+
pipe(
205+
asset,
206+
(asset) =>
207+
sourceProcessor.processSourceAndSourceMap(
208+
asset.source.content,
209+
asset.sourceMap.content,
210+
sourceDebugId ?? sourceMapDebugId,
211+
force,
212+
),
213+
(result) =>
214+
({
215+
source: { ...asset.source, content: result.source },
216+
sourceMap: { ...asset.sourceMap, content: result.sourceMap },
217+
debugId: result.debugId,
218+
} as ProcessedSourceAndSourceMap),
219+
),
224220
);
225221
};
226222
}

tools/cli/tests/_files/processed-directory-linked-sourcemaps/entry1.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/cli/tests/_files/processed-directory-linked-sourcemaps/entry2.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/cli/tests/_files/processed-not-linked-different-name-sourcemaps/entry1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._btDebugIds=e._btDebugIds||{},e._btDebugIds[n]="4fe9a5c9-ab48-b240-9469-04aa2db251b6")}catch(e){}}();
1+
;!function(){try{var k="_btDebugIds",u="undefined",v="4fe9a5c9-ab48-b240-9469-04aa2db251b6",a=function(x){try{x[k]=x[k]||{};x[k][n]=v}catch{}},n=(new Error).stack;n&&(u!=typeof window?a(window):u);n&&(u!=typeof global?a(global):u);n&&(u!=typeof self?a(self):u);n&&(u!=typeof globalThis?a(globalThis):u)}catch{}}();
22
function doSomething() {
33
console.log('Done something');
44
}

tools/cli/tests/_files/processed-not-linked-different-name-sourcemaps/entry2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._btDebugIds=e._btDebugIds||{},e._btDebugIds[n]="d538bdaa-8149-8111-25f0-b5c0f472366a")}catch(e){}}();
1+
;!function(){try{var k="_btDebugIds",u="undefined",v="d538bdaa-8149-8111-25f0-b5c0f472366a",a=function(x){try{x[k]=x[k]||{};x[k][n]=v}catch{}},n=(new Error).stack;n&&(u!=typeof window?a(window):u);n&&(u!=typeof global?a(global):u);n&&(u!=typeof self?a(self):u);n&&(u!=typeof globalThis?a(globalThis):u)}catch{}}();
22
function doSomething() {
33
console.log('Done something');
44
}

tools/cli/tests/_files/processed-not-linked-sourcemaps/entry1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._btDebugIds=e._btDebugIds||{},e._btDebugIds[n]="4fe9a5c9-ab48-b240-9469-04aa2db251b6")}catch(e){}}();
1+
;!function(){try{var k="_btDebugIds",u="undefined",v="4fe9a5c9-ab48-b240-9469-04aa2db251b6",a=function(x){try{x[k]=x[k]||{};x[k][n]=v}catch{}},n=(new Error).stack;n&&(u!=typeof window?a(window):u);n&&(u!=typeof global?a(global):u);n&&(u!=typeof self?a(self):u);n&&(u!=typeof globalThis?a(globalThis):u)}catch{}}();
22
function doSomething() {
33
console.log('Done something');
44
}

tools/cli/tests/_files/processed-not-linked-sourcemaps/entry2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._btDebugIds=e._btDebugIds||{},e._btDebugIds[n]="d538bdaa-8149-8111-25f0-b5c0f472366a")}catch(e){}}();
1+
;!function(){try{var k="_btDebugIds",u="undefined",v="d538bdaa-8149-8111-25f0-b5c0f472366a",a=function(x){try{x[k]=x[k]||{};x[k][n]=v}catch{}},n=(new Error).stack;n&&(u!=typeof window?a(window):u);n&&(u!=typeof global?a(global):u);n&&(u!=typeof self?a(self):u);n&&(u!=typeof globalThis?a(globalThis):u)}catch{}}();
22
function doSomething() {
33
console.log('Done something');
44
}

tools/cli/tests/_files/processed-sources/entry1.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)