Skip to content

Commit

Permalink
fix(pnp): esm - support import attributes (#6268)
Browse files Browse the repository at this point in the history
**What's the problem this PR addresses?**

In Node.js v22 import assertions were replaced with import attributes so
we need to add support for those as well.

Fixes #6267

**How did you fix it?**

Added support for the `importAttributes` property.

**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.
  • Loading branch information
merceyz authored May 6, 2024
1 parent 578d896 commit ebf9a0d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 28 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ jobs:
fail-fast: false
matrix:
# We run the ubuntu tests on multiple Node versions with 2 shards since they're the fastest.
node: [18, 19, 20, 21]
node: [18, 19, 20, 21, 22]
platform: [[ubuntu, 20.04]]
shard: ['1/2', '2/2']
include:
Expand All @@ -221,13 +221,13 @@ jobs:
- {node: 18, platform: [macos, latest], shard: 3/3}
# We also run them on the maximum Node version we support, to catch potential regressions in Node.js.
# Windows tests
- {node: 21, platform: [windows, latest], shard: 1/3}
- {node: 21, platform: [windows, latest], shard: 2/3}
- {node: 21, platform: [windows, latest], shard: 3/3}
- {node: 22, platform: [windows, latest], shard: 1/3}
- {node: 22, platform: [windows, latest], shard: 2/3}
- {node: 22, platform: [windows, latest], shard: 3/3}
# macOS tests
- {node: 21, platform: [macos, latest], shard: 1/3}
- {node: 21, platform: [macos, latest], shard: 2/3}
- {node: 21, platform: [macos, latest], shard: 3/3}
- {node: 22, platform: [macos, latest], shard: 1/3}
- {node: 22, platform: [macos, latest], shard: 2/3}
- {node: 22, platform: [macos, latest], shard: 3/3}

name: '${{matrix.platform[0]}}-latest w/ Node.js ${{matrix.node}}.x (${{matrix.shard}})'
runs-on: ${{matrix.platform[0]}}-${{matrix.platform[1]}}
Expand Down
21 changes: 17 additions & 4 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/a5b83769.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
@@ -1,6 +1,6 @@
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';
import {ALLOWS_EXTENSIONLESS_FILES, HAS_LOADERS_AFFECTING_LOADERS} 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} from '@yarnpkg/pnp/sources/esm-loader/loaderFlags';
import {pathToFileURL} from 'url';

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

test(
`it should not resolve JSON modules without an import assertion`,
`it should not resolve JSON modules without an import assertion/attribute`,
makeTemporaryEnv(
{
type: `module`,
Expand All @@ -236,14 +236,14 @@ describe(`Plug'n'Play - ESM`, () => {

await expect(run(`node`, `./index.js`)).rejects.toMatchObject({
code: 1,
stderr: expect.stringContaining(`ERR_IMPORT_ASSERTION_TYPE_MISSING`),
stderr: expect.stringContaining(SUPPORTS_IMPORT_ATTRIBUTES_ONLY ? `ERR_IMPORT_ATTRIBUTE_MISSING` : `ERR_IMPORT_ASSERTION_TYPE_MISSING`),
});
},
),
);

test(
`it should resolve JSON modules with an import assertion`,
`it should resolve JSON modules with an import assertion/attribute`,
makeTemporaryEnv(
{
type: `module`,
Expand All @@ -254,7 +254,7 @@ describe(`Plug'n'Play - ESM`, () => {
await xfs.writeFilePromise(
ppath.join(path, `index.js`),
`
import foo from './foo.json' assert { type: 'json' };
import foo from './foo.json' ${SUPPORTS_IMPORT_ATTRIBUTES ? `with` : `assert`} { type: 'json' };
console.log(foo.name);
`,
);
Expand Down
Loading

0 comments on commit ebf9a0d

Please sign in to comment.