Skip to content

Commit 08f6f70

Browse files
authored
fix(pnp): esm - return undefined source for commonjs (#5677)
**What's the problem this PR addresses?** The ESM loader always returns a source which after nodejs/node#47999 landed causes stuff to break. **How did you fix it?** Return undefined source for commonjs modules **Checklist** - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). - [x] I have set the packages that need to be released for my changes to be effective. - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent 68039e0 commit 08f6f70

5 files changed

Lines changed: 59 additions & 4 deletions

File tree

.pnp.loader.mjs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.yarn/versions/d70173f5.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-pnp": patch
4+
"@yarnpkg/pnp": patch
5+
6+
declined:
7+
- "@yarnpkg/plugin-compat"
8+
- "@yarnpkg/plugin-constraints"
9+
- "@yarnpkg/plugin-dlx"
10+
- "@yarnpkg/plugin-essentials"
11+
- "@yarnpkg/plugin-init"
12+
- "@yarnpkg/plugin-interactive-tools"
13+
- "@yarnpkg/plugin-nm"
14+
- "@yarnpkg/plugin-npm-cli"
15+
- "@yarnpkg/plugin-pack"
16+
- "@yarnpkg/plugin-patch"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-version"
21+
- "@yarnpkg/plugin-workspace-tools"
22+
- "@yarnpkg/builder"
23+
- "@yarnpkg/core"
24+
- "@yarnpkg/doctor"
25+
- "@yarnpkg/nm"
26+
- "@yarnpkg/pnpify"
27+
- "@yarnpkg/sdks"

packages/acceptance-tests/pkg-tests-specs/sources/pnp-esm.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,4 +1004,32 @@ describe(`Plug'n'Play - ESM`, () => {
10041004
),
10051005
);
10061006
});
1007+
1008+
it(
1009+
`should use the commonjs resolver in commonjs files imported from ESM`,
1010+
makeTemporaryEnv(
1011+
{
1012+
type: `module`,
1013+
},
1014+
async ({path, run, source}) => {
1015+
await xfs.writeFilePromise(ppath.join(path, `foo.js`), `import './bar.cjs';`);
1016+
await xfs.writeFilePromise(
1017+
ppath.join(path, `bar.cjs`),
1018+
`
1019+
require('module')._extensions['.custom'] = require('module')._extensions['.js'];
1020+
require('./baz');
1021+
`,
1022+
);
1023+
await xfs.writeFilePromise(ppath.join(path, `baz.custom`), `console.log(42);`);
1024+
1025+
await expect(run(`install`)).resolves.toMatchObject({code: 0});
1026+
1027+
await expect(run(`node`, `./foo.js`)).resolves.toMatchObject({
1028+
code: 0,
1029+
stdout: `42\n`,
1030+
stderr: ``,
1031+
});
1032+
},
1033+
),
1034+
);
10071035
});

packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/yarnpkg-pnp/sources/esm-loader/hooks/load.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function load(
1515
};
1616
},
1717
nextLoad: typeof load,
18-
): Promise<{ format: string, source: string, shortCircuit: boolean }> {
18+
): Promise<{ format: string, source?: string, shortCircuit: boolean }> {
1919
const url = loaderUtils.tryParseURL(urlString);
2020
if (url?.protocol !== `file:`)
2121
return nextLoad(urlString, context, nextLoad);
@@ -49,7 +49,7 @@ export async function load(
4949

5050
return {
5151
format,
52-
source: await fs.promises.readFile(filePath, `utf8`),
52+
source: format === `commonjs` ? undefined : await fs.promises.readFile(filePath, `utf8`),
5353
shortCircuit: true,
5454
};
5555
}

0 commit comments

Comments
 (0)