Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev-tools): drop deprecated node option #465

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
/**
* Support module alias in ESM mode
* Support module alias in ESM mode by implementing Node.js custom module resolver
* tsconfig-paths does not work in ESM, see https://github.com/dividab/tsconfig-paths/issues/122
* Adapted from https://github.com/TypeStrong/ts-node/discussions/1450
*/
import path from 'path';
import fs from 'fs';
import {pathToFileURL} from 'url';
import {resolve as resolveTs, getFormat, transformSource, load} from 'ts-node/esm';
import {getValidPath, ocularRoot} from '../utils/utils.js';
export {getFormat, transformSource, load};

/** Node.js resolve hook,
* https://nodejs.org/api/module.html#resolvespecifier-context-nextresolve
*/
type ResolveHook = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add TSDoc on the type (and fields if possible). Specify if this is this an ocular type or are we compensating for esbuild 's lack of types, if so which version of esbuild was this written for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is Node.js API. I'll add a comment.

specifier: string,
context: {
conditions?: unknown;
importAssertions?: unknown;
parentURL: string;
},
nextResolve: ResolveHook
) => Promise<{
url: string;
format?: 'builtin' | 'commonjs' | 'json' | 'module' | 'wasm';
shortCircuit?: boolean;
}>;

// Load alias from file
const pathJSON = fs.readFileSync(path.resolve(ocularRoot, '.alias.json'), 'utf-8');
const paths = JSON.parse(pathJSON);
const paths: Record<string, string> = JSON.parse(pathJSON);
const matchPath = createMatchPath(paths);

export function resolve(specifier, context, defaultResolver) {
export const resolve: ResolveHook = (specifier, context, nextResolver) => {
const mappedSpecifier = matchPath(specifier);
if (mappedSpecifier) {
if (mappedSpecifier.match(/(\/\*|\.jsx?|\.tsx?|\.cjs|\.json)$/)) {
Expand All @@ -26,20 +41,25 @@ export function resolve(specifier, context, defaultResolver) {
specifier = `${pathToFileURL(mappedSpecifier)}`;
}
}
const result = resolveTs(specifier, context, defaultResolver);
return result;
}
// @ts-ignore
return nextResolver(specifier);
};

/** Checks if a path matches aliased pattern.
* If so, returns mapped path, otherwise returns null
*/
type AliasTest = (specifier: string) => string | null;

/** Convert ocular alias object to TS config paths object */
function createMatchPath(aliases) {
const tests = [];
/** Get alias mapping function from ocular config */
function createMatchPath(aliases: Record<string, string>): AliasTest {
const tests: AliasTest[] = [];

for (const key in aliases) {
const alias = aliases[key];
let testFunc;
let testFunc: AliasTest;
if (key.includes('*')) {
const regex = new RegExp(`^${key.replace('*', '(.+)')}`);
testFunc = (specifier) => {
testFunc = (specifier: string) => {
const match = specifier.match(regex);
if (match) {
return specifier.replace(match[0], alias.replace('*', match[1]));
Expand All @@ -53,7 +73,7 @@ function createMatchPath(aliases) {
defaultEntry = getValidPath(`${alias}/index.ts`, `${alias}/index.js`) || defaultEntry;
}

testFunc = (specifier) => {
testFunc = (specifier: string) => {
if (key === specifier) {
return defaultEntry;
}
Expand All @@ -66,7 +86,7 @@ function createMatchPath(aliases) {
tests.push(testFunc);
}

return (specifier) => {
return (specifier: string) => {
for (const test of tests) {
const result = test(specifier);
if (result) {
Expand Down
4 changes: 4 additions & 0 deletions modules/dev-tools/src/helpers/esm-register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {register} from 'node:module';

register('ts-node/esm', import.meta.url);
register('./esm-alias.js', import.meta.url);
2 changes: 1 addition & 1 deletion modules/dev-tools/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function runNodeTest(entry: string, command: string = '') {

if (ocularConfig.esm) {
execShellCommand(
`NODE_OPTIONS="--experimental-modules --es-module-specifier-resolution=node --loader ${ocularConfig.ocularPath}/dist/helpers/esm-loader.js" ${command} node "${entry}"`
`${command} node --import "${ocularConfig.ocularPath}/dist/helpers/esm-register.js" --es-module-specifier-resolution=node "${entry}"`
);
} else {
execShellCommand(
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
},
"pre-commit": "pre-commit",
"pre-push": "pre-push",
"dependencies": {}
"dependencies": {},
"volta": {
"node": "18.19.0",
"yarn": "1.22.19"
}
}
Loading