From 8ad5292626f1bbac52cf3e1f23de88919d7b2058 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 9 Oct 2021 20:48:09 -0400 Subject: [PATCH] Redirect source-map-support to @cspotcode/source-map-support (#1496) * initial commit * install source-map-support from git for tests --- .prettierignore | 1 + package-lock.json | 16 ++- package.json | 2 +- src/index.ts | 14 +++ src/test/sourcemaps.spec.ts | 30 ++++++ .../index.ts | 101 ++++++++++++++++++ tests/package.json | 3 +- 7 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 src/test/sourcemaps.spec.ts create mode 100644 tests/legacy-source-map-support-interop/index.ts diff --git a/.prettierignore b/.prettierignore index 4b463a062..e0158cb5c 100644 --- a/.prettierignore +++ b/.prettierignore @@ -10,3 +10,4 @@ tests/main-realpath/symlink/tsconfig.json tests/throw error.ts tests/throw error react tsx.tsx tests/esm/throw error.ts +tests/legacy-source-map-support-interop/index.ts diff --git a/package-lock.json b/package-lock.json index 3f8ca56be..43292e8a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -317,9 +317,8 @@ "integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==" }, "@cspotcode/source-map-support": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.6.1.tgz", - "integrity": "sha512-DX3Z+T5dt1ockmPdobJS/FAsQPW4V4SrWEhD2iYQT2Cb2tQsiMnYxrcUH9By/Z3B+v0S5LMBkQtV/XOBbpLEOg==", + "version": "github:cspotcode/node-source-map-support#63248d0f2b65074dd0fe936ae6f194c1b52b1f74", + "from": "github:cspotcode/node-source-map-support", "requires": { "@cspotcode/source-map-consumer": "0.8.0" } @@ -4668,6 +4667,17 @@ "diff": "^4.0.1", "make-error": "^1.1.1", "yn": "3.1.1" + }, + "dependencies": { + "@cspotcode/source-map-support": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.6.1.tgz", + "integrity": "sha512-DX3Z+T5dt1ockmPdobJS/FAsQPW4V4SrWEhD2iYQT2Cb2tQsiMnYxrcUH9By/Z3B+v0S5LMBkQtV/XOBbpLEOg==", + "dev": true, + "requires": { + "@cspotcode/source-map-consumer": "0.8.0" + } + } } }, "tslib": { diff --git a/package.json b/package.json index e440ef3b0..e12c6e74b 100644 --- a/package.json +++ b/package.json @@ -152,7 +152,7 @@ } }, "dependencies": { - "@cspotcode/source-map-support": "0.6.1", + "@cspotcode/source-map-support": "github:cspotcode/node-source-map-support", "@tsconfig/node10": "^1.0.7", "@tsconfig/node12": "^1.0.7", "@tsconfig/node14": "^1.0.0", diff --git a/src/index.ts b/src/index.ts index e1db16cac..f93005eea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -677,6 +677,20 @@ export function create(rawOptions: CreateOptions = {}): Service { path = normalizeSlashes(path); return outputCache.get(path)?.content || ''; }, + redirectConflictingLibrary: true, + onConflictingLibraryRedirect( + request, + parent, + isMain, + options, + redirectedRequest + ) { + debug( + `Redirected an attempt to require source-map-support to instead receive @cspotcode/source-map-support. "${ + (parent as NodeJS.Module).filename + }" attempted to require or resolve "${request}" and was redirected to "${redirectedRequest}".` + ); + }, }); const shouldHavePrettyErrors = diff --git a/src/test/sourcemaps.spec.ts b/src/test/sourcemaps.spec.ts new file mode 100644 index 000000000..505107c88 --- /dev/null +++ b/src/test/sourcemaps.spec.ts @@ -0,0 +1,30 @@ +import * as expect from 'expect'; +import { createExec, createExecTester } from './exec-helpers'; +import { + CMD_TS_NODE_WITH_PROJECT_FLAG, + contextTsNodeUnderTest, + TEST_DIR, +} from './helpers'; +import { test as _test } from './testlib'; +const test = _test.context(contextTsNodeUnderTest); + +const exec = createExecTester({ + cmd: CMD_TS_NODE_WITH_PROJECT_FLAG, + exec: createExec({ + cwd: TEST_DIR, + }), +}); + +test('Redirects source-map-support to @cspotcode/source-map-support so that third-party libraries get correct source-mapped locations', async () => { + const { stdout } = await exec({ + flags: `./legacy-source-map-support-interop/index.ts`, + }); + expect(stdout.split('\n')).toMatchObject([ + expect.stringContaining('.ts:2 '), + 'true', + 'true', + expect.stringContaining('.ts:100:'), + expect.stringContaining('.ts:101 '), + '', + ]); +}); diff --git a/tests/legacy-source-map-support-interop/index.ts b/tests/legacy-source-map-support-interop/index.ts new file mode 100644 index 000000000..a4ae13c92 --- /dev/null +++ b/tests/legacy-source-map-support-interop/index.ts @@ -0,0 +1,101 @@ +import { Logger } from 'tslog'; +new Logger().info('hi'); +console.log(require.resolve('source-map-support') === require.resolve('@cspotcode/source-map-support')); +console.log(require.resolve('source-map-support/register') === require.resolve('@cspotcode/source-map-support/register')); +/* +tslog uses `require('source-map-support').wrapCallSite` directly. +Without redirection to @cspotcode/source-map-support it does not have access to the sourcemap information we provide. +*/ +interface Foo { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +} +console.log(new Error().stack!.split('\n')[1]); +new Logger().info('hi'); diff --git a/tests/package.json b/tests/package.json index 1b91c8b50..371b90084 100644 --- a/tests/package.json +++ b/tests/package.json @@ -1,6 +1,7 @@ { "dependencies": { "@swc/core": "latest", - "ts-node": "file:ts-node-packed.tgz" + "ts-node": "file:ts-node-packed.tgz", + "tslog": "3.2.2" } }