Skip to content

Commit 8ad5292

Browse files
authored
Redirect source-map-support to @cspotcode/source-map-support (#1496)
* initial commit * install source-map-support from git for tests
1 parent b255b0e commit 8ad5292

File tree

7 files changed

+162
-5
lines changed

7 files changed

+162
-5
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ tests/main-realpath/symlink/tsconfig.json
1010
tests/throw error.ts
1111
tests/throw error react tsx.tsx
1212
tests/esm/throw error.ts
13+
tests/legacy-source-map-support-interop/index.ts

package-lock.json

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
}
153153
},
154154
"dependencies": {
155-
"@cspotcode/source-map-support": "0.6.1",
155+
"@cspotcode/source-map-support": "github:cspotcode/node-source-map-support",
156156
"@tsconfig/node10": "^1.0.7",
157157
"@tsconfig/node12": "^1.0.7",
158158
"@tsconfig/node14": "^1.0.0",

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,20 @@ export function create(rawOptions: CreateOptions = {}): Service {
677677
path = normalizeSlashes(path);
678678
return outputCache.get(path)?.content || '';
679679
},
680+
redirectConflictingLibrary: true,
681+
onConflictingLibraryRedirect(
682+
request,
683+
parent,
684+
isMain,
685+
options,
686+
redirectedRequest
687+
) {
688+
debug(
689+
`Redirected an attempt to require source-map-support to instead receive @cspotcode/source-map-support. "${
690+
(parent as NodeJS.Module).filename
691+
}" attempted to require or resolve "${request}" and was redirected to "${redirectedRequest}".`
692+
);
693+
},
680694
});
681695

682696
const shouldHavePrettyErrors =

src/test/sourcemaps.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as expect from 'expect';
2+
import { createExec, createExecTester } from './exec-helpers';
3+
import {
4+
CMD_TS_NODE_WITH_PROJECT_FLAG,
5+
contextTsNodeUnderTest,
6+
TEST_DIR,
7+
} from './helpers';
8+
import { test as _test } from './testlib';
9+
const test = _test.context(contextTsNodeUnderTest);
10+
11+
const exec = createExecTester({
12+
cmd: CMD_TS_NODE_WITH_PROJECT_FLAG,
13+
exec: createExec({
14+
cwd: TEST_DIR,
15+
}),
16+
});
17+
18+
test('Redirects source-map-support to @cspotcode/source-map-support so that third-party libraries get correct source-mapped locations', async () => {
19+
const { stdout } = await exec({
20+
flags: `./legacy-source-map-support-interop/index.ts`,
21+
});
22+
expect(stdout.split('\n')).toMatchObject([
23+
expect.stringContaining('.ts:2 '),
24+
'true',
25+
'true',
26+
expect.stringContaining('.ts:100:'),
27+
expect.stringContaining('.ts:101 '),
28+
'',
29+
]);
30+
});
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { Logger } from 'tslog';
2+
new Logger().info('hi');
3+
console.log(require.resolve('source-map-support') === require.resolve('@cspotcode/source-map-support'));
4+
console.log(require.resolve('source-map-support/register') === require.resolve('@cspotcode/source-map-support/register'));
5+
/*
6+
tslog uses `require('source-map-support').wrapCallSite` directly.
7+
Without redirection to @cspotcode/source-map-support it does not have access to the sourcemap information we provide.
8+
*/
9+
interface Foo {
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
}
100+
console.log(new Error().stack!.split('\n')[1]);
101+
new Logger().info('hi');

tests/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {
33
"@swc/core": "latest",
4-
"ts-node": "file:ts-node-packed.tgz"
4+
"ts-node": "file:ts-node-packed.tgz",
5+
"tslog": "3.2.2"
56
}
67
}

0 commit comments

Comments
 (0)