Skip to content

Commit 4569dab

Browse files
authored
fix: resolve module system upwards from resource path (#492)
1 parent 319e650 commit 4569dab

File tree

7 files changed

+55
-5
lines changed

7 files changed

+55
-5
lines changed

client/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

loader/utils/getModuleSystem.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { promises: fsPromises } = require('fs');
22
const path = require('path');
3+
const commonPathPrefix = require('common-path-prefix');
4+
const findUp = require('find-up');
35

46
/** @type {string | undefined} */
57
let packageJsonType;
@@ -45,7 +47,17 @@ async function getModuleSystem(ModuleFilenameHelpers, options) {
4547
// We will cache the results in a global variable so it will only be parsed once.
4648
if (!packageJsonType) {
4749
try {
48-
const packageJsonPath = require.resolve(path.join(this.rootContext, 'package.json'));
50+
const commonPath = commonPathPrefix([this.rootContext, this.resourcePath], '/');
51+
const stopPath = path.resolve(commonPath, '..');
52+
53+
const packageJsonPath = await findUp(
54+
(dir) => {
55+
if (dir === stopPath) return findUp.stop;
56+
return 'package.json';
57+
},
58+
{ cwd: path.dirname(this.resourcePath) }
59+
);
60+
4961
const buffer = await fsPromises.readFile(packageJsonPath, { encoding: 'utf-8' });
5062
const rawPackageJson = buffer.toString('utf-8');
5163
({ type: packageJsonType } = JSON.parse(rawPackageJson));

overlay/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@
5656
},
5757
"dependencies": {
5858
"ansi-html": "^0.0.7",
59+
"common-path-prefix": "^3.0.0",
5960
"core-js-pure": "^3.8.1",
6061
"error-stack-parser": "^2.0.6",
62+
"find-up": "^5.0.0",
6163
"html-entities": "^2.1.0",
6264
"loader-utils": "^2.0.0",
6365
"schema-utils": "^3.0.0",

sockets/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

test/loader/unit/getModuleSystem.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('getModuleSystem', () => {
6262
await expect(
6363
getModuleSystem.call(
6464
{
65-
resourcePath: 'index.js',
65+
resourcePath: path.resolve(__dirname, '..', 'fixtures/esm', 'index.js'),
6666
rootContext: path.resolve(__dirname, '..', 'fixtures/esm'),
6767
},
6868
ModuleFilenameHelpers,
@@ -75,7 +75,7 @@ describe('getModuleSystem', () => {
7575
await expect(
7676
getModuleSystem.call(
7777
{
78-
resourcePath: 'index.js',
78+
resourcePath: path.resolve(__dirname, '..', 'fixtures/cjs', 'index.js'),
7979
rootContext: path.resolve(__dirname, '..', 'fixtures/cjs'),
8080
},
8181
ModuleFilenameHelpers,
@@ -88,7 +88,7 @@ describe('getModuleSystem', () => {
8888
await expect(
8989
getModuleSystem.call(
9090
{
91-
resourcePath: 'index.js',
91+
resourcePath: path.resolve(__dirname, '..', 'fixtures/auto', 'index.js'),
9292
rootContext: path.resolve(__dirname, '..', 'fixtures/auto'),
9393
},
9494
ModuleFilenameHelpers,

yarn.lock

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,11 @@ commander@^7.0.0:
20062006
resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7"
20072007
integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==
20082008

2009+
common-path-prefix@^3.0.0:
2010+
version "3.0.0"
2011+
resolved "https://registry.yarnpkg.com/common-path-prefix/-/common-path-prefix-3.0.0.tgz#7d007a7e07c58c4b4d5f433131a19141b29f11e0"
2012+
integrity sha512-QE33hToZseCH3jS0qN96O/bSh3kaw/h+Tq7ngyY9eWDUnTlTNUyqfqvCXioLe5Na5jFsL78ra/wuBU4iuEgd4w==
2013+
20092014
commondir@^1.0.1:
20102015
version "1.0.1"
20112016
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
@@ -3167,6 +3172,14 @@ find-up@^4.0.0, find-up@^4.1.0:
31673172
locate-path "^5.0.0"
31683173
path-exists "^4.0.0"
31693174

3175+
find-up@^5.0.0:
3176+
version "5.0.0"
3177+
resolved "https://registry.yarnpkg.com/find-up/-/find-up-5.0.0.tgz#4c92819ecb7083561e4f4a240a86be5198f536fc"
3178+
integrity sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==
3179+
dependencies:
3180+
locate-path "^6.0.0"
3181+
path-exists "^4.0.0"
3182+
31703183
findup-sync@^3.0.0:
31713184
version "3.0.0"
31723185
resolved "https://registry.yarnpkg.com/findup-sync/-/findup-sync-3.0.0.tgz#17b108f9ee512dfb7a5c7f3c8b27ea9e1a9c08d1"
@@ -5058,6 +5071,13 @@ locate-path@^5.0.0:
50585071
dependencies:
50595072
p-locate "^4.1.0"
50605073

5074+
locate-path@^6.0.0:
5075+
version "6.0.0"
5076+
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-6.0.0.tgz#55321eb309febbc59c4801d931a72452a681d286"
5077+
integrity sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==
5078+
dependencies:
5079+
p-locate "^5.0.0"
5080+
50615081
lodash._reinterpolate@^3.0.0:
50625082
version "3.0.0"
50635083
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
@@ -5783,7 +5803,7 @@ p-limit@^2.0.0, p-limit@^2.2.0:
57835803
dependencies:
57845804
p-try "^2.0.0"
57855805

5786-
p-limit@^3.1.0:
5806+
p-limit@^3.0.2, p-limit@^3.1.0:
57875807
version "3.1.0"
57885808
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b"
57895809
integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==
@@ -5804,6 +5824,13 @@ p-locate@^4.1.0:
58045824
dependencies:
58055825
p-limit "^2.2.0"
58065826

5827+
p-locate@^5.0.0:
5828+
version "5.0.0"
5829+
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834"
5830+
integrity sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==
5831+
dependencies:
5832+
p-limit "^3.0.2"
5833+
58075834
p-map@^2.0.0:
58085835
version "2.1.0"
58095836
resolved "https://registry.yarnpkg.com/p-map/-/p-map-2.1.0.tgz#310928feef9c9ecc65b68b17693018a665cea175"

0 commit comments

Comments
 (0)