Skip to content
Open
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
32 changes: 27 additions & 5 deletions .pnp.loader.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .yarn/versions/cd8f12a4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 43;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 44;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
commonjs: require(`./nested`),
esm: require(`./esm`).default,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "no-deps-extensionless",
"version": "1.0.0",
"type": "module",
"exports": {
".": {
"import": "./entry-esm",
"require": "./index"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
required: require(`no-deps-extensionless`),
importEsm: () => import(`no-deps-extensionless`),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "no-deps-requires-extensionless",
"version": "1.0.0",
"dependencies": {
"no-deps-extensionless": "1.0.0"
}
}
55 changes: 52 additions & 3 deletions packages/acceptance-tests/pkg-tests-specs/sources/pnp-esm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';
import {ALLOWS_EXTENSIONLESS_FILES, HAS_LOADERS_AFFECTING_LOADERS, SUPPORTS_IMPORT_ATTRIBUTES, SUPPORTS_IMPORT_ATTRIBUTES_ONLY} from '@yarnpkg/pnp/sources/esm-loader/loaderFlags';
import {pathToFileURL} from 'url';
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';
import {ALLOWS_EXTENSIONLESS_FILES, HAS_BROKEN_FSTAT_FOR_ZIP_FDS, HAS_LOADERS_AFFECTING_LOADERS, SUPPORTS_IMPORT_ATTRIBUTES, SUPPORTS_IMPORT_ATTRIBUTES_ONLY} from '@yarnpkg/pnp/sources/esm-loader/loaderFlags';
import {pathToFileURL} from 'url';

describe(`Plug'n'Play - ESM`, () => {
test(
Expand Down Expand Up @@ -441,6 +441,55 @@ describe(`Plug'n'Play - ESM`, () => {
),
);

(HAS_BROKEN_FSTAT_FOR_ZIP_FDS ? it : it.skip)(
`it should load extensionless files from zip archives when fstat is broken`,
makeTemporaryEnv(
{
type: `module`,
dependencies: {
'no-deps-extensionless': `1.0.0`,
'no-deps-requires-extensionless': `1.0.0`,
},
},
{
pnpEnableEsmLoader: true,
},
async ({path, run}) => {
await xfs.writeFilePromise(ppath.join(path, `loader.mjs`), `
export async function resolve(specifier, context, nextResolve) {
if (specifier !== 'custom:extensionless')
return nextResolve(specifier, context);

const result = await nextResolve('no-deps-extensionless', context);
const url = new URL(result.url);
url.searchParams.set('chained', '1');
return {url: url.href, shortCircuit: true};
}
`);
await xfs.writeFilePromise(ppath.join(path, `index.mjs`), `
import commonjsBridge from 'no-deps-requires-extensionless';
import esmValue from 'no-deps-extensionless';
import chainedEsmValue from 'custom:extensionless';

const dynamicallyImportedEsm = await commonjsBridge.importEsm();
console.log(JSON.stringify({
requiredCommonjs: commonjsBridge.required.commonjs,
requiredEsm: commonjsBridge.required.esm,
importedEsm: esmValue,
chainedEsm: chainedEsmValue,
dynamicallyImportedEsm: dynamicallyImportedEsm.default,
}));
`);

await run(`install`);

await expect(run(`node`, `--loader`, `./loader.mjs`, `./index.mjs`)).resolves.toMatchObject({
stdout: `${JSON.stringify({requiredCommonjs: 42, requiredEsm: 44, importedEsm: 43, chainedEsm: 43, dynamicallyImportedEsm: 43})}\n`,
});
},
),
);

(ALLOWS_EXTENSIONLESS_FILES ? it.skip : it)(
`it should not allow extensionless files with {"type": "module"}`,
makeTemporaryEnv(
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

Large diffs are not rendered by default.

28 changes: 23 additions & 5 deletions packages/yarnpkg-pnp/sources/esm-loader/hooks/load.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import {VirtualFS, npath} from '@yarnpkg/fslib';
import fs from 'fs';
import path from 'path';
import {fileURLToPath, pathToFileURL} from 'url';

import {HAS_BROKEN_FSTAT_FOR_ZIP_FDS, SUPPORTS_IMPORT_ATTRIBUTES, SUPPORTS_IMPORT_ATTRIBUTES_ONLY, WATCH_MODE_MESSAGE_USES_ARRAYS} from '../loaderFlags';
import * as loaderUtils from '../loaderUtils';

const initialUncaughtExceptionListenerCount = process.listenerCount(`uncaughtException`);

function isSynchronousLoaderHookCall() {
// Node temporarily installs an exception handler while its async loader
// worker services a synchronous request; the hook context has no sync flag.
// https://github.com/nodejs/node/blob/bd96dfbf0361576724b65322046e2ca9f9609cb9/lib/internal/modules/esm/worker.js#L180-L184
return process.listenerCount(`uncaughtException`) > initialUncaughtExceptionListenerCount;
}

// The default `load` doesn't support reading from zip files
export async function load(
urlString: string,
Expand All @@ -25,7 +35,17 @@ export async function load(

const filePath = fileURLToPath(url);

const format = loaderUtils.getFileFormat(filePath);
const shouldReadSource = HAS_BROKEN_FSTAT_FOR_ZIP_FDS && filePath.includes(`.zip/`);
let source: string | undefined;
let format = context.format ?? loaderUtils.getFileFormat(filePath);
if (!format && shouldReadSource && path.extname(filePath) === ``) {
source = await fs.promises.readFile(filePath, `utf8`);
format = loaderUtils.getFileFormat(filePath, {
considerExtensionless: true,
extensionlessSource: isSynchronousLoaderHookCall() ? source : undefined,
});
}

if (!format)
return nextLoad(urlString, context, nextLoad);

Expand Down Expand Up @@ -61,10 +81,8 @@ export async function load(
});
}

const shouldReadSource = format === `commonjs` && HAS_BROKEN_FSTAT_FOR_ZIP_FDS && filePath.includes(`.zip/`);
const source = format !== `commonjs` || shouldReadSource
? await fs.promises.readFile(filePath, `utf8`)
: undefined;
if (source === undefined && (format !== `commonjs` || shouldReadSource))
source = await fs.promises.readFile(filePath, `utf8`);

return {
format,
Expand Down
25 changes: 20 additions & 5 deletions packages/yarnpkg-pnp/sources/esm-loader/loaderUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {NativePath} from '@yarnpkg/fslib';
import fs from 'fs';
import path from 'path';
import {NativePath} from '@yarnpkg/fslib';
import fs from 'fs';
import path from 'path';
import {pathToFileURL} from 'url';

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

const {containsModuleSyntax} = (process as any).binding(`contextify`) as {
containsModuleSyntax: (source: string, filename: string, url: string) => boolean;
};

export async function tryReadFile(path: NativePath): Promise<string | null> {
try {
Expand All @@ -29,7 +34,7 @@ export function setEntrypointPath(file: NativePath) {
entrypointPath = file;
}

export function getFileFormat(filepath: string): string | null {
export function getFileFormat(filepath: string, {considerExtensionless = false, extensionlessSource}: {considerExtensionless?: boolean, extensionlessSource?: string} = {}): string | null {
const ext = path.extname(filepath);

switch (ext) {
Expand Down Expand Up @@ -60,6 +65,16 @@ export function getFileFormat(filepath: string): string | null {
// --experimental-loader behavior but is required to work around
// https://github.com/nodejs/node/issues/33226
default: {
if (considerExtensionless && ext === ``) {
if (extensionlessSource !== undefined)
return containsModuleSyntax(extensionlessSource, filepath, pathToFileURL(filepath).href) ? `module` : `commonjs`;

const pkg = nodeUtils.readPackageScope(filepath);
if (!pkg)
return `commonjs`;
return pkg.data.type ?? `commonjs`;
}

if (entrypointPath !== filepath)
return null;
const pkg = nodeUtils.readPackageScope(filepath);
Expand Down
Loading