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

Update file extensions for Node TS support #6596

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions .pnp.loader.mjs

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

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

declined:
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
52 changes: 49 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_LOADERS_AFFECTING_LOADERS, SUPPORTS_IMPORT_ATTRIBUTES, SUPPORTS_IMPORT_ATTRIBUTES_ONLY, SUPPORTS_TYPE_STRIPPING} from '@yarnpkg/pnp/sources/esm-loader/loaderFlags';
import {pathToFileURL} from 'url';

describe(`Plug'n'Play - ESM`, () => {
test(
Expand Down Expand Up @@ -1160,4 +1160,50 @@ describe(`Plug'n'Play - ESM`, () => {
},
),
);

(SUPPORTS_TYPE_STRIPPING ? describe : describe.skip)(`Node builtin type stripping`, () => {
it(
`should be able to resolve a .cts file`,
makeTemporaryEnv(
{
type: `module`,
},
async ({path, run, source}) => {
await run(`install`);

await xfs.writeFilePromise(
ppath.join(path, `index.cts`),
`const {TextDecoder} = require('node:util');\nconst decoder = new TextDecoder();\nconst u8arr = new Uint8Array([72, 101, 108, 108, 111]);\nconsole.log(decoder.decode(u8arr));`,
);

await expect(run(`node`, `./index.cts`)).resolves.toMatchObject({
code: 0,
stdout: `Hello\n`,
});
},
),
);

it(
`should be able to resolve a .mts file`,
makeTemporaryEnv(
{
type: `module`,
},
async ({path, run, source}) => {
await run(`install`);

await xfs.writeFilePromise(
ppath.join(path, `index.mts`),
`import {TextDecoder} from 'node:util';\nconst decoder = new TextDecoder();\nconst u8arr = new Uint8Array([72, 101, 108, 108, 111]);\nconsole.log(decoder.decode(u8arr));`,
);

await expect(run(`node`, `./index.mts`)).resolves.toMatchObject({
code: 0,
stdout: `Hello\n`,
});
},
),
);
});
});
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/yarnpkg-pnp/sources/esm-loader/loaderFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ export const SUPPORTS_IMPORT_ATTRIBUTES = major >= 21 || (major === 20 && minor

// https://github.com/nodejs/node/pull/52104
export const SUPPORTS_IMPORT_ATTRIBUTES_ONLY = major >= 22;

// https://github.com/nodejs/node/pull/53725
export const SUPPORTS_TYPE_STRIPPING = major > 23 || (major === 23 && minor > 6);
6 changes: 6 additions & 0 deletions packages/yarnpkg-pnp/sources/esm-loader/loaderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export function getFileFormat(filepath: string): string | null {
case `.mjs`: {
return `module`;
}
case `.mts`: {
return `module`;
}
case `.cts`: {
return `commonjs`;
}
case `.cjs`: {
return `commonjs`;
}
Expand Down
Loading