Skip to content

Commit

Permalink
fix(esm): adjust to new Node 16 loader API
Browse files Browse the repository at this point in the history
introduced in 16.12.0
  • Loading branch information
AviVahl committed Oct 20, 2021
1 parent 660e9b4 commit cb78177
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
56 changes: 27 additions & 29 deletions packages/esm/src/create-loader.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { readFileSync } from 'fs';
import { pathToFileURL, fileURLToPath } from 'url';
import ts from 'typescript';

const isTypescriptFile = (url: string) => url.endsWith('.ts') || url.endsWith('.tsx');

/** @url https://nodejs.org/docs/latest-v16.x/api/esm.html#esm_resolve_specifier_context_defaultresolve */
export type ModuleFormat = 'builtin' | 'commonjs' | 'json' | 'module' | 'wasm';

/** @url https://nodejs.org/docs/latest-v16.x/api/esm.html#resolvespecifier-context-defaultresolve */
export type ResolveHook = (
specifier: string,
context: { parentURL: string; conditions: string[] },
context: { parentURL?: string; conditions: string[] },
defaultResolve: ResolveHook
) => { url: string };

/** @url https://nodejs.org/docs/latest-v16.x/api/esm.html#esm_getformat_url_context_defaultgetformat */
export type FormatHook = (url: string, context: {}, defaultGetFormat: FormatHook) => { format: ModuleFormat };
export type ModuleFormat = 'builtin' | 'commonjs' | 'json' | 'module' | 'wasm';
) => { url: string; format?: ModuleFormat };

/** @url https://nodejs.org/docs/latest-v16.x/api/esm.html#esm_transformsource_source_context_defaulttransformsource */
export type TransformHook = (
source: string | SharedArrayBuffer | Uint8Array,
context: { url: string; format: ModuleFormat },
defaultTransformSource: TransformHook
) => { source: string | SharedArrayBuffer | Uint8Array };
/** @url https://nodejs.org/docs/latest-v16.x/api/esm.html#loadurl-context-defaultload */
export type LoadHook = (
url: string,
context: { format?: ModuleFormat },
defaultTransformSource: LoadHook
) => { source: string | SharedArrayBuffer | Uint8Array; format: ModuleFormat };

export interface CreateLoaderOptions {
compilerOptions: ts.CompilerOptions;
Expand Down Expand Up @@ -53,25 +52,24 @@ export function createLoader({ compilerOptions, cwd }: CreateLoaderOptions) {
return defaultResolve(specifier, context, defaultResolve);
};

const getFormat: FormatHook = (url, context, defaultGetFormat) => {
return isTypescriptFile(url) ? { format: 'module' } : defaultGetFormat(url, context, defaultGetFormat);
const load: LoadHook = (url, context, defaultTransformSource) => {
if (isTypescriptFile(url)) {
const filePath = fileURLToPath(url);
const source = readFileSync(filePath, 'utf8');
return {
source: ts.transpileModule(source, {
fileName: filePath,
compilerOptions,
}).outputText,
format: 'module',
};
} else {
return defaultTransformSource(url, context, defaultTransformSource);
}
};

const transformSource: TransformHook = (source, context, defaultTransformSource) => {
const { url } = context;

return isTypescriptFile(url)
? {
source: ts.transpileModule(source.toString(), {
fileName: fileURLToPath(url),
compilerOptions,
}).outputText,
}
: defaultTransformSource(source, context, defaultTransformSource);
};
return {
resolve,
getFormat,
transformSource,
load,
};
}
4 changes: 2 additions & 2 deletions packages/esm/src/default-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ const cwd = process.cwd();
const tsconfigPath = ts.findConfigFile(cwd, ts.sys.fileExists);
const compilerOptions = tsconfigPath !== undefined ? loadTsconfig(tsconfigPath) : defaultCompilerOptions;

const { resolve, getFormat, transformSource } = createLoader({ compilerOptions, cwd });
const { resolve, load } = createLoader({ compilerOptions, cwd });

export { resolve, getFormat, transformSource };
export { resolve, load };
34 changes: 17 additions & 17 deletions packages/esm/test/esm-loader.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ export function runCommand(command: string): { output: string; exitCode: number
return { output: output.join('\n'), exitCode: exitCode || 0 };
}

describe('using node --experimental-loader @ts-tools/esm <file>', function () {
this.timeout(5000);
if (nodeMajorVersion >= 16) {
describe('using node --experimental-loader @ts-tools/esm <file>', function () {
this.timeout(5000);

describe('when tsconfig.json is found', () => {
it('allows using imports (with default interop)', () => {
const filePath = join(fixturesRoot, 'imports.ts');
describe('when tsconfig.json is found', () => {
it('allows using imports (with default interop)', () => {
const filePath = join(fixturesRoot, 'imports.ts');

const { output, exitCode } = runCommand(`node --experimental-loader @ts-tools/esm ${filePath}`);
const { output, exitCode } = runCommand(`node --experimental-loader @ts-tools/esm ${filePath}`);

expect(exitCode).to.equal(0);
expect(output).to.include(`Current platform is: ${platform()}`);
expect(output).to.include(`Path separator is: ${sep}`);
});
expect(exitCode).to.equal(0);
expect(output).to.include(`Current platform is: ${platform()}`);
expect(output).to.include(`Path separator is: ${sep}`);
});

if (nodeMajorVersion >= 14) {
it('maps stack traces using source maps when specifying --enable-source-maps', () => {
const filePath = join(fixturesRoot, 'throwing.ts');

Expand All @@ -39,14 +39,14 @@ describe('using node --experimental-loader @ts-tools/esm <file>', function () {
expect(exitCode).to.not.equal(0);
expect(output).to.include(`runMe (${filePath}:10:11)`);
});
}

it('does not throw on empty files', () => {
const filePath = join(fixturesRoot, 'empty.ts');
it('does not throw on empty files', () => {
const filePath = join(fixturesRoot, 'empty.ts');

const { exitCode, output } = runCommand(`node --experimental-loader @ts-tools/esm ${filePath}`);
const { exitCode, output } = runCommand(`node --experimental-loader @ts-tools/esm ${filePath}`);

expect(exitCode, output).to.equal(0);
expect(exitCode, output).to.equal(0);
});
});
});
});
}

0 comments on commit cb78177

Please sign in to comment.