Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: privatenumber/tsx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.1.3
Choose a base ref
...
head repository: privatenumber/tsx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.1.4
Choose a head ref
  • 3 commits
  • 12 files changed
  • 1 contributor

Commits on Nov 18, 2023

  1. Copy the full SHA
    5bdf022 View commit details

Commits on Nov 19, 2023

  1. revert: source-map stripping (#398)

    This reverts commit 0e83db7.
    privatenumber committed Nov 19, 2023
    Copy the full SHA
    a95a482 View commit details
  2. Copy the full SHA
    4d3d33c View commit details
Showing with 311 additions and 123 deletions.
  1. +2 −0 package.json
  2. +89 −0 pnpm-lock.yaml
  3. +1 −6 src/cjs/index.ts
  4. +2 −7 src/esm/loaders.ts
  5. +1 −20 src/source-map.ts
  6. +49 −33 src/utils/transform/get-esbuild-options.ts
  7. +20 −25 src/utils/transform/index.ts
  8. +1 −1 tests/index.ts
  9. +14 −8 tests/specs/smoke.ts
  10. +0 −23 tests/specs/source-map.ts
  11. +131 −0 tests/specs/transform.ts
  12. +1 −0 tests/specs/watch.ts
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -75,11 +75,13 @@
"eslint": "^8.49.0",
"execa": "^8.0.1",
"fs-fixture": "^1.2.0",
"fs-require": "^1.6.0",
"get-node": "^14.2.1",
"kolorist": "^1.8.0",
"lint-staged": "^14.0.1",
"magic-string": "^0.30.3",
"manten": "^1.2.0",
"memfs": "^4.6.0",
"node-pty": "^1.0.0",
"outdent": "^0.8.0",
"pkgroll": "^1.11.1",
89 changes: 89 additions & 0 deletions pnpm-lock.yaml

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

7 changes: 1 addition & 6 deletions src/cjs/index.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import {
createFilesMatcher,
} from 'get-tsconfig';
import type { TransformOptions } from 'esbuild';
import { installSourceMapSupport, shouldStripSourceMap, stripSourceMap } from '../source-map';
import { installSourceMapSupport } from '../source-map';
import { transformSync } from '../utils/transform';
import { transformDynamicImport } from '../utils/transform/transform-dynamic-import';
import { resolveTsPath } from '../utils/resolve-ts-path';
@@ -69,11 +69,6 @@ const transformer = (

let code = fs.readFileSync(filePath, 'utf8');

// Strip source maps if originally disabled
if (shouldStripSourceMap) {
code = stripSourceMap(code);
}

if (filePath.endsWith('.cjs')) {
// Contains native ESM check
const transformed = transformDynamicImport(filePath, code);
9 changes: 2 additions & 7 deletions src/esm/loaders.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import type { TransformOptions } from 'esbuild';
import { transform } from '../utils/transform';
import { transformDynamicImport } from '../utils/transform/transform-dynamic-import';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { installSourceMapSupport, shouldStripSourceMap, stripSourceMap } from '../source-map';
import { installSourceMapSupport } from '../source-map';
import { importAttributes } from '../utils/node-features';
import {
tsconfigPathsMatcher,
@@ -281,12 +281,7 @@ export const load: LoadHook = async function (
}

const filePath = url.startsWith('file://') ? fileURLToPath(url) : url;
let code = loaded.source.toString();

// Strip source maps if originally disabled
if (shouldStripSourceMap) {
code = stripSourceMap(code);
}
const code = loaded.source.toString();

if (
// Support named imports in JSON modules
21 changes: 1 addition & 20 deletions src/source-map.ts
Original file line number Diff line number Diff line change
@@ -10,26 +10,7 @@ type PortMessage = {
map: RawSourceMap;
};

// If Node.js has source map disabled, we should strip source maps to speed up processing
export const shouldStripSourceMap = (
('sourceMapsEnabled' in process)
&& process.sourceMapsEnabled === false
);

const sourceMapPrefix = '\n//# sourceMappingURL=';

export const stripSourceMap = (code: string) => {
const sourceMapIndex = code.indexOf(sourceMapPrefix);
if (sourceMapIndex === -1) {
return code;
}

const nextNewLine = code.indexOf('\n', sourceMapIndex + sourceMapPrefix.length);
const afterSourceMap = nextNewLine === -1 ? '' : code.slice(nextNewLine);
return code.slice(0, sourceMapIndex) + afterSourceMap;
};

const inlineSourceMapPrefix = `${sourceMapPrefix}data:application/json;base64,`;
const inlineSourceMapPrefix = '\n//# sourceMappingURL=data:application/json;base64,';

export const installSourceMapSupport = (
/**
82 changes: 49 additions & 33 deletions src/utils/transform/get-esbuild-options.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
import path from 'path';
import type { TransformOptions } from 'esbuild';
import type { TransformOptions, TransformResult } from 'esbuild';

const nodeVersion = process.versions.node;
export const baseConfig = Object.freeze({
target: `node${process.versions.node}`,

export const getEsbuildOptions = (
extendOptions: TransformOptions,
// "default" tells esbuild to infer loader from file name
// https://github.com/evanw/esbuild/blob/4a07b17adad23e40cbca7d2f8931e8fb81b47c33/internal/bundler/bundler.go#L158
loader: 'default',
});

export const cacheConfig = {
...baseConfig,

sourcemap: true,

/**
* Smaller output for cache and marginal performance improvement:
* https://twitter.com/evanwallace/status/1396336348366180359?s=20
*
* minifyIdentifiers is disabled because debuggers don't use the
* `names` property from the source map
*
* minifySyntax is disabled because it does some tree-shaking
* eg. unused try-catch error variable
*/
minifyWhitespace: true,

/**
* esbuild renames variables even if minification is not enabled
* https://esbuild.github.io/try/#dAAwLjE5LjUAAGNvbnN0IGEgPSAxOwooZnVuY3Rpb24gYSgpIHt9KTs
*/
keepNames: true,
};

export const patchOptions = (
options: TransformOptions,
) => {
const options: TransformOptions = {
target: `node${nodeVersion}`,

// "default" tells esbuild to infer loader from file name
// https://github.com/evanw/esbuild/blob/4a07b17adad23e40cbca7d2f8931e8fb81b47c33/internal/bundler/bundler.go#L158
loader: 'default',

sourcemap: true,

/**
* Smaller output for cache and marginal performance improvement:
* https://twitter.com/evanwallace/status/1396336348366180359?s=20
*
* minifyIdentifiers is disabled because debuggers don't use the
* `names` property from the source map
*
* minifySyntax is disabled because it does some tree-shaking
* eg. unused try-catch error variable
*/
minifyWhitespace: true,
keepNames: true,

...extendOptions,
};
const originalSourcefile = options.sourcefile;

if (options.sourcefile) {
const { sourcefile } = options;
const extension = path.extname(sourcefile);
if (originalSourcefile) {
const extension = path.extname(originalSourcefile);

if (extension) {
// https://github.com/evanw/esbuild/issues/1932
if (extension === '.cts' || extension === '.mts') {
options.sourcefile = `${sourcefile.slice(0, -3)}ts`;
options.sourcefile = `${originalSourcefile.slice(0, -3)}ts`;
}
} else {
// esbuild errors to detect loader when a file doesn't have an extension
options.sourcefile += '.js';
}
}

return options;
return (
result: TransformResult,
) => {
if (options.sourcefile !== originalSourcefile) {
result.map = result.map.replace(
JSON.stringify(options.sourcefile),
JSON.stringify(originalSourcefile),
);
}
return result;
};
};
Loading