Skip to content

Commit

Permalink
test: add tests for transform (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 18, 2023
1 parent 497f69a commit 5bdf022
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 66 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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.

81 changes: 47 additions & 34 deletions src/utils/transform/get-esbuild-options.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,63 @@
import path from 'path';
import type { TransformOptions } from 'esbuild';

const nodeVersion = process.versions.node;
import type { TransformOptions, TransformResult } from 'esbuild';

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

// "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,

// TODO: Is this necessary if minifyIdentifiers is false?
keepNames: true,
};

export const getEsbuildOptions = (
extendOptions: TransformOptions,
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;
};
};
45 changes: 20 additions & 25 deletions src/utils/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
applyTransformers,
type Transformed,
} from './apply-transformers';
import { getEsbuildOptions } from './get-esbuild-options';
import {
cacheConfig,
patchOptions,
} from './get-esbuild-options';

// Used by cjs-loader
export function transformSync(
Expand All @@ -27,14 +30,15 @@ export function transformSync(
define['import.meta.url'] = `'${pathToFileURL(filePath)}'`;
}

const esbuildOptions = getEsbuildOptions({
const esbuildOptions = {
...cacheConfig,
format: 'cjs',
sourcefile: filePath,
define,
banner: '(()=>{',
footer: '})()',
...extendOptions,
});
} as const;

const hash = sha1([
code,
Expand All @@ -50,16 +54,11 @@ export function transformSync(
code,
[
// eslint-disable-next-line @typescript-eslint/no-shadow
(filePath, code) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const transformed = esbuildTransformSync(code, esbuildOptions);
if (esbuildOptions.sourcefile !== filePath) {
transformed.map = transformed.map.replace(
JSON.stringify(esbuildOptions.sourcefile),
JSON.stringify(filePath),
);
}
return transformed;
(_filePath, code) => {
const patchResults = patchOptions(esbuildOptions);
return patchResults(
esbuildTransformSync(code, esbuildOptions),
);
},
transformDynamicImport,
] as const,
Expand All @@ -84,11 +83,12 @@ export async function transform(
filePath: string,
extendOptions?: TransformOptions,
): Promise<Transformed> {
const esbuildOptions = getEsbuildOptions({
const esbuildOptions = {
...cacheConfig,
format: 'esm',
sourcefile: filePath,
...extendOptions,
});
} as const;

const hash = sha1([
code,
Expand All @@ -104,16 +104,11 @@ export async function transform(
code,
[
// eslint-disable-next-line @typescript-eslint/no-shadow
async (filePath, code) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const transformed = await esbuildTransform(code, esbuildOptions);
if (esbuildOptions.sourcefile !== filePath) {
transformed.map = transformed.map.replace(
JSON.stringify(esbuildOptions.sourcefile),
JSON.stringify(filePath),
);
}
return transformed;
async (_filePath, code) => {
const patchResults = patchOptions(esbuildOptions);
return patchResults(
await esbuildTransform(code, esbuildOptions),
);
},
transformDynamicImport,
] as const,
Expand Down
1 change: 1 addition & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { nodeVersions } from './utils/node-versions';
await runTestSuite(import('./specs/watch'));
await runTestSuite(import('./specs/repl'));
await runTestSuite(import('./specs/source-map'));
await runTestSuite(import('./specs/transform'));

for (const nodeVersion of nodeVersions) {
const node = await createNode(nodeVersion);
Expand Down
Loading

0 comments on commit 5bdf022

Please sign in to comment.