Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Removes obsolete flags
  • Loading branch information
arcanis committed Jun 22, 2023
commit f516b0949c034cb754d8a4fe7b70bea4087c205f
30 changes: 3 additions & 27 deletions packages/acceptance-tests/pkg-tests-specs/sources/pnp-esm.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';
import * as loaderFlags from '@yarnpkg/pnp/sources/esm-loader/loaderFlags';
import {pathToFileURL} from 'url';

describe(`Plug'n'Play - ESM`, () => {
Expand Down Expand Up @@ -219,30 +218,7 @@ describe(`Plug'n'Play - ESM`, () => {
),
);

(loaderFlags.HAS_UNFLAGGED_JSON_MODULES === false ? test : test.skip)(
`it should not resolve JSON modules without --experimental-json-modules`,
makeTemporaryEnv(
{
type: `module`,
},
async ({path, run, source}) => {
await expect(run(`install`)).resolves.toMatchObject({code: 0});

await xfs.writeFilePromise(
ppath.join(path, `index.js`),
`import './foo.json';`,
);
await xfs.writeFilePromise(ppath.join(path, `foo.json`), `{"name": "foo"}`);

await expect(run(`node`, `./index.js`)).rejects.toMatchObject({
code: 1,
stderr: expect.stringContaining(`Unknown file extension`),
});
},
),
);

(loaderFlags.HAS_UNFLAGGED_JSON_MODULES ? test : test.skip)(
test(
`it should not resolve JSON modules without an import assertion`,
makeTemporaryEnv(
{
Expand All @@ -265,7 +241,7 @@ describe(`Plug'n'Play - ESM`, () => {
),
);

(loaderFlags.HAS_UNFLAGGED_JSON_MODULES ? test : test.skip)(
test(
`it should resolve JSON modules with an import assertion`,
makeTemporaryEnv(
{
Expand Down Expand Up @@ -972,7 +948,7 @@ describe(`Plug'n'Play - ESM`, () => {
),
);

(loaderFlags.ALLOWS_NON_FILE_PARENT ? test : test.skip)(
test(
`it should allow importing files regardless of parent URL`,
makeTemporaryEnv(
{
Expand Down
12 changes: 6 additions & 6 deletions packages/yarnpkg-pnp/sources/esm-loader/hooks/load.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {VirtualFS, npath} from '@yarnpkg/fslib';
import fs from 'fs';
import {fileURLToPath, pathToFileURL} from 'url';
import {VirtualFS, npath} from '@yarnpkg/fslib';
import fs from 'fs';
import {fileURLToPath, pathToFileURL} from 'url';

import {HAS_JSON_IMPORT_ASSERTION_REQUIREMENT, WATCH_MODE_MESSAGE_USES_ARRAYS} from '../loaderFlags';
import * as loaderUtils from '../loaderUtils';
import {WATCH_MODE_MESSAGE_USES_ARRAYS} from '../loaderFlags';
import * as loaderUtils from '../loaderUtils';

// The default `load` doesn't support reading from zip files
export async function load(
Expand All @@ -26,7 +26,7 @@ export async function load(
if (!format)
return nextLoad(urlString, context, nextLoad);

if (HAS_JSON_IMPORT_ASSERTION_REQUIREMENT && format === `json` && context.importAssertions?.type !== `json`) {
if (format === `json` && context.importAssertions?.type !== `json`) {
const err = new TypeError(`[ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "${urlString}" needs an import assertion of type "json"`) as TypeError & { code: string };
err.code = `ERR_IMPORT_ASSERTION_TYPE_MISSING`;
throw err;
Expand Down
9 changes: 0 additions & 9 deletions packages/yarnpkg-pnp/sources/esm-loader/loaderFlags.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
const [major, minor] = process.versions.node.split(`.`).map(value => parseInt(value, 10));

// JSON modules were unflagged in https://github.com/nodejs/node/pull/41736
export const HAS_UNFLAGGED_JSON_MODULES = major > 17 || (major === 17 && minor >= 5);

// JSON modules requires import assertions after https://github.com/nodejs/node/pull/40250
export const HAS_JSON_IMPORT_ASSERTION_REQUIREMENT = major > 17 || (major === 17 && minor >= 1);

// The message switched to using an array in https://github.com/nodejs/node/pull/45348
export const WATCH_MODE_MESSAGE_USES_ARRAYS = major > 19 || (major === 19 && minor >= 2) || (major === 18 && minor >= 13);

// https://github.com/nodejs/node/pull/45659 changed the internal translators to be lazy loaded
// TODO: Update the version range if https://github.com/nodejs/node/pull/46425 lands.
export const HAS_LAZY_LOADED_TRANSLATORS = major > 19 || (major === 19 && minor >= 3);

// https://github.com/nodejs/node/pull/42881
export const ALLOWS_NON_FILE_PARENT = major > 18 || (major === 18 && minor >= 1) || (major === 16 && minor >= 17);
21 changes: 6 additions & 15 deletions packages/yarnpkg-pnp/sources/esm-loader/loaderUtils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {NativePath} from '@yarnpkg/fslib';
import fs from 'fs';
import path from 'path';
import {URL} from 'url';
import {NativePath} from '@yarnpkg/fslib';
import fs from 'fs';
import path from 'path';
import {URL} from 'url';

import * as nodeUtils from '../loader/nodeUtils';

import {HAS_UNFLAGGED_JSON_MODULES} from './loaderFlags';
import * as nodeUtils from '../loader/nodeUtils';

export async function tryReadFile(path: NativePath): Promise<string | null> {
try {
Expand Down Expand Up @@ -50,14 +48,7 @@ export function getFileFormat(filepath: string): string | null {
);
}
case `.json`: {
if (HAS_UNFLAGGED_JSON_MODULES)
return `json`;

// TODO: Enable if --experimental-json-modules is present
// Waiting on https://github.com/nodejs/node/issues/36935
throw new Error(
`Unknown file extension ".json" for ${filepath}`,
);
return `json`;
}
case `.js`: {
const pkg = nodeUtils.readPackageScope(filepath);
Expand Down