Skip to content

Commit 772056e

Browse files
authored
Adds support for ESM loaders to the SDKs (#6278)
## What's the problem this PR addresses? ESM loaders aren't supported by the sdks because we only `require().setup()` but we don't `register()` the ESM loader hook. Fixes #6219 ## How did you fix it? - Add support for ESM loaders to the SDK. - Check whether the `findPnpApi` function could be found in the `module` builtin. If not, tries to setup the environment accordingly. This is because in the case of the SDK, the subprocesses have been created without the `--require .pnp.loader.mjs` flag, so the PnP runtime won't be setup. ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent e1e8000 commit 772056e

File tree

19 files changed

+186
-24
lines changed

19 files changed

+186
-24
lines changed

.pnp.cjs

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.pnp.loader.mjs

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"watchDirectory": "useFsEvents"
88
},
99
"npm.packageManager": "yarn",
10+
"eslint.experimental.useFlatConfig": true,
1011
"eslint.validate": [
1112
"javascript",
1213
"typescript",

.yarn/sdks/eslint/bin/eslint.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/usr/bin/env node
22

33
const {existsSync} = require(`fs`);
4-
const {createRequire} = require(`module`);
4+
const {createRequire, register} = require(`module`);
55
const {resolve} = require(`path`);
6+
const {pathToFileURL} = require(`url`);
67

78
const relPnpApiPath = "../../../../.pnp.cjs";
89

910
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
1011
const absRequire = createRequire(absPnpApiPath);
1112

13+
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
14+
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);
15+
1216
if (existsSync(absPnpApiPath)) {
1317
if (!process.versions.pnp) {
1418
// Setup the environment to be able to require eslint/bin/eslint.js
1519
require(absPnpApiPath).setup();
20+
if (isPnpLoaderEnabled && register) {
21+
register(pathToFileURL(absPnpLoaderPath));
22+
}
1623
}
1724
}
1825

.yarn/sdks/eslint/lib/api.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/usr/bin/env node
22

33
const {existsSync} = require(`fs`);
4-
const {createRequire} = require(`module`);
4+
const {createRequire, register} = require(`module`);
55
const {resolve} = require(`path`);
6+
const {pathToFileURL} = require(`url`);
67

78
const relPnpApiPath = "../../../../.pnp.cjs";
89

910
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
1011
const absRequire = createRequire(absPnpApiPath);
1112

13+
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
14+
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);
15+
1216
if (existsSync(absPnpApiPath)) {
1317
if (!process.versions.pnp) {
1418
// Setup the environment to be able to require eslint
1519
require(absPnpApiPath).setup();
20+
if (isPnpLoaderEnabled && register) {
21+
register(pathToFileURL(absPnpLoaderPath));
22+
}
1623
}
1724
}
1825

.yarn/sdks/eslint/lib/unsupported-api.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/usr/bin/env node
22

33
const {existsSync} = require(`fs`);
4-
const {createRequire} = require(`module`);
4+
const {createRequire, register} = require(`module`);
55
const {resolve} = require(`path`);
6+
const {pathToFileURL} = require(`url`);
67

78
const relPnpApiPath = "../../../../.pnp.cjs";
89

910
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
1011
const absRequire = createRequire(absPnpApiPath);
1112

13+
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
14+
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);
15+
1216
if (existsSync(absPnpApiPath)) {
1317
if (!process.versions.pnp) {
1418
// Setup the environment to be able to require eslint/use-at-your-own-risk
1519
require(absPnpApiPath).setup();
20+
if (isPnpLoaderEnabled && register) {
21+
register(pathToFileURL(absPnpLoaderPath));
22+
}
1623
}
1724
}
1825

.yarn/sdks/eslint/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint",
3-
"version": "8.45.0-sdk",
3+
"version": "8.57.0-sdk",
44
"main": "./lib/api.js",
55
"type": "commonjs",
66
"bin": {

.yarn/sdks/typescript/bin/tsc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/usr/bin/env node
22

33
const {existsSync} = require(`fs`);
4-
const {createRequire} = require(`module`);
4+
const {createRequire, register} = require(`module`);
55
const {resolve} = require(`path`);
6+
const {pathToFileURL} = require(`url`);
67

78
const relPnpApiPath = "../../../../.pnp.cjs";
89

910
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
1011
const absRequire = createRequire(absPnpApiPath);
1112

13+
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
14+
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);
15+
1216
if (existsSync(absPnpApiPath)) {
1317
if (!process.versions.pnp) {
1418
// Setup the environment to be able to require typescript/bin/tsc
1519
require(absPnpApiPath).setup();
20+
if (isPnpLoaderEnabled && register) {
21+
register(pathToFileURL(absPnpLoaderPath));
22+
}
1623
}
1724
}
1825

.yarn/sdks/typescript/bin/tsserver

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/usr/bin/env node
22

33
const {existsSync} = require(`fs`);
4-
const {createRequire} = require(`module`);
4+
const {createRequire, register} = require(`module`);
55
const {resolve} = require(`path`);
6+
const {pathToFileURL} = require(`url`);
67

78
const relPnpApiPath = "../../../../.pnp.cjs";
89

910
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
1011
const absRequire = createRequire(absPnpApiPath);
1112

13+
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
14+
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);
15+
1216
if (existsSync(absPnpApiPath)) {
1317
if (!process.versions.pnp) {
1418
// Setup the environment to be able to require typescript/bin/tsserver
1519
require(absPnpApiPath).setup();
20+
if (isPnpLoaderEnabled && register) {
21+
register(pathToFileURL(absPnpLoaderPath));
22+
}
1623
}
1724
}
1825

.yarn/sdks/typescript/lib/tsc.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/usr/bin/env node
22

33
const {existsSync} = require(`fs`);
4-
const {createRequire} = require(`module`);
4+
const {createRequire, register} = require(`module`);
55
const {resolve} = require(`path`);
6+
const {pathToFileURL} = require(`url`);
67

78
const relPnpApiPath = "../../../../.pnp.cjs";
89

910
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
1011
const absRequire = createRequire(absPnpApiPath);
1112

13+
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
14+
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);
15+
1216
if (existsSync(absPnpApiPath)) {
1317
if (!process.versions.pnp) {
1418
// Setup the environment to be able to require typescript/lib/tsc.js
1519
require(absPnpApiPath).setup();
20+
if (isPnpLoaderEnabled && register) {
21+
register(pathToFileURL(absPnpLoaderPath));
22+
}
1623
}
1724
}
1825

0 commit comments

Comments
 (0)