Skip to content

Commit

Permalink
refactor: remove source-map-support (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 22, 2023
1 parent 7a0eb7e commit 7db0747
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 93 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
},
"dependencies": {
"esbuild": "~0.18.20",
"get-tsconfig": "^4.7.2",
"source-map-support": "^0.5.21"
"get-tsconfig": "^4.7.2"
},
"optionalDependencies": {
"fsevents": "~2.3.3"
Expand All @@ -65,7 +64,6 @@
"@types/node": "^20.9.4",
"@types/react": "^18.2.38",
"@types/semver": "^7.5.6",
"@types/source-map-support": "^0.5.10",
"cachedir": "^2.4.0",
"chokidar": "^3.5.3",
"clean-pkg-json": "^1.2.0",
Expand Down
27 changes: 0 additions & 27 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const transformer = (
// Contains native ESM check
const transformed = transformDynamicImport(filePath, code);
if (transformed) {
code = applySourceMap(transformed, filePath);
code = applySourceMap(transformed);
}
} else if (
transformTs
Expand All @@ -89,7 +89,7 @@ const transformer = (
},
);

code = applySourceMap(transformed, filePath);
code = applySourceMap(transformed);
}

module._compile(code, filePath);
Expand Down
13 changes: 2 additions & 11 deletions src/esm/loaders.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { MessagePort } from 'node:worker_threads';
import path from 'path';
import { pathToFileURL, fileURLToPath } from 'url';
import type {
Expand Down Expand Up @@ -37,8 +36,6 @@ type resolve = (
recursiveCall?: boolean,
) => MaybePromise<ResolveFnOutput>;

let mainThreadPort: MessagePort | undefined;

type SendToParent = (data: {
type: 'dependency';
path: string;
Expand All @@ -52,7 +49,6 @@ export const initialize: InitializeHook = async (data) => {
}

const { port } = data;
mainThreadPort = port;
sendToParent = port.postMessage.bind(port);
};

Expand All @@ -61,7 +57,6 @@ export const initialize: InitializeHook = async (data) => {
* but it shares a closure with the new load hook
*/
export const globalPreload: GlobalPreloadHook = ({ port }) => {
mainThreadPort = port;
sendToParent = port.postMessage.bind(port);

return `
Expand Down Expand Up @@ -298,18 +293,14 @@ export const load: LoadHook = async function (

return {
format: 'module',
source: applySourceMap(transformed, url, mainThreadPort),
source: applySourceMap(transformed),
};
}

if (loaded.format === 'module') {
const dynamicImportTransformed = transformDynamicImport(filePath, code);
if (dynamicImportTransformed) {
loaded.source = applySourceMap(
dynamicImportTransformed,
url,
mainThreadPort,
);
loaded.source = applySourceMap(dynamicImportTransformed);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/esm/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { installSourceMapSupport } from '../source-map.js';
export const registerLoader = () => {
const { port1, port2 } = new MessageChannel();

installSourceMapSupport(port1);
installSourceMapSupport();
if (process.send) {
port1.addListener('message', (message) => {
if (message.type === 'dependency') {
Expand Down
52 changes: 3 additions & 49 deletions src/source-map.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import type { MessagePort } from 'node:worker_threads';
import sourceMapSupport, { type UrlAndMap } from 'source-map-support';
import type { Transformed, SourceMap } from './utils/transform/apply-transformers.js';
import { isolatedLoader } from './utils/node-features.js';

type PortMessage = {
filePath: string;
map: SourceMap;
};
import type { Transformed } from './utils/transform/apply-transformers.js';

const inlineSourceMapPrefix = '\n//# sourceMappingURL=data:application/json;base64,';

export const installSourceMapSupport = (
/**
* To support Node v20 where loaders are executed in its own thread
* https://nodejs.org/docs/latest-v20.x/api/esm.html#globalpreload
*/
loaderPort?: MessagePort,
) => {
export const installSourceMapSupport = () => {
const hasNativeSourceMapSupport = (
/**
* Check if native source maps are supported by seeing if the API is available
Expand Down Expand Up @@ -46,37 +32,5 @@ export const installSourceMapSupport = (
);
}

const sourcemaps = new Map<string, SourceMap>();

sourceMapSupport.install({
environment: 'node',
retrieveSourceMap(url) {
const map = sourcemaps.get(url);
return (
map
? ({ url, map } as unknown as UrlAndMap)
: null
);
},
});

if (isolatedLoader && loaderPort) {
loaderPort.addListener(
'message',
({ filePath, map }: PortMessage) => sourcemaps.set(filePath, map),
);
}

return (
{ code, map }: Transformed,
filePath: string,
mainThreadPort?: MessagePort,
) => {
if (isolatedLoader && mainThreadPort) {
mainThreadPort.postMessage({ filePath, map } satisfies PortMessage);
} else {
sourcemaps.set(filePath, map);
}
return code;
};
return ({ code }: Transformed) => code;
};

0 comments on commit 7db0747

Please sign in to comment.