Skip to content

Commit

Permalink
feat(plugin-esm-loader-typescript): support for Yarn 3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
kherock committed Feb 26, 2023
1 parent 86dbcb8 commit 09deee9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-esm-loader-typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kherock/yarn-plugin-esm-loader-typescript",
"version": "0.2.2",
"version": "0.3.0-force",
"license": "BSD-2-Clause",
"main": "./sources/index.ts",
"author": "Kyle Herock <[email protected]>",
Expand Down
28 changes: 23 additions & 5 deletions packages/pnp-esm-loader-typescript/sources/hooks/load.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {load as loadHook} from '@yarnpkg/pnp/lib/esm-loader/hooks/load';
import * as loaderUtils from '@yarnpkg/pnp/lib/esm-loader/loaderUtils';
import fs from 'fs';
import {fileURLToPath} from 'url';
import {npath, VirtualFS} from '@yarnpkg/fslib';
import {load as loadHook} from '@yarnpkg/pnp/lib/esm-loader/hooks/load';
import {WATCH_MODE_MESSAGE_USES_ARRAYS} from '@yarnpkg/pnp/lib/esm-loader/loaderFlags';
import * as loaderUtils from '@yarnpkg/pnp/lib/esm-loader/loaderUtils';
import fs from 'fs';
import {fileURLToPath, pathToFileURL} from 'url';

import * as tsLoaderUtils from '../loaderUtils';
import * as tsLoaderUtils from '../loaderUtils';

export async function load(
urlString: string,
Expand All @@ -21,6 +23,22 @@ export async function load(
if (!format)
return nextLoad(urlString, context, nextLoad);

// https://github.com/nodejs/node/pull/44366/files#diff-f6796082f599554ec3a29c47cf026cb24fc5104884f2632e472c05fe622d778bR477-R479
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
// At the time of writing Node.js reports all loaded URLs itself so
// we technically only need to do this for virtual files but in the
// event that ever changes we report everything.
const pathToSend = pathToFileURL(
npath.fromPortablePath(
VirtualFS.resolveVirtual(npath.toPortablePath(filePath)),
),
).href;
process.send({
// eslint-disable-next-line @typescript-eslint/naming-convention
'watch:import': WATCH_MODE_MESSAGE_USES_ARRAYS ? [pathToSend] : pathToSend,
});
}

const source = await fs.promises.readFile(filePath, `utf8`);

return {
Expand Down
13 changes: 5 additions & 8 deletions packages/pnp-esm-loader-typescript/sources/loader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {HAS_CONSOLIDATED_HOOKS} from '@yarnpkg/pnp/lib/esm-loader/loaderFlags';

import {getFormat as getFormatHook} from './hooks/getFormat';
import {getSource as getSourceHook} from './hooks/getSource';
import {load as loadHook} from './hooks/load';
Expand All @@ -6,12 +8,7 @@ import {resolve as resolveHook} from './hooks/resolve';
// eslint-disable-next-line arca/import-ordering
import '@yarnpkg/pnp/lib/esm-loader/fspatch';

const [major, minor] = process.versions.node.split(`.`).map(value => parseInt(value, 10));

// The hooks were consolidated in https://github.com/nodejs/node/pull/37468
const hasConsolidatedHooks = major > 16 || (major === 16 && minor >= 12);

export const resolve = resolveHook;
export const getFormat = hasConsolidatedHooks ? undefined : getFormatHook;
export const getSource = hasConsolidatedHooks ? undefined : getSourceHook;
export const load = hasConsolidatedHooks ? loadHook : undefined;
export const getFormat = HAS_CONSOLIDATED_HOOKS ? undefined : getFormatHook;
export const getSource = HAS_CONSOLIDATED_HOOKS ? undefined : getSourceHook;
export const load = HAS_CONSOLIDATED_HOOKS ? loadHook : undefined;

0 comments on commit 09deee9

Please sign in to comment.