Skip to content

Commit

Permalink
fix: resolve module system upwards from resource path (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmmmwh authored Aug 29, 2021
1 parent 319e650 commit 4569dab
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 5 deletions.
3 changes: 3 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
14 changes: 13 additions & 1 deletion loader/utils/getModuleSystem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { promises: fsPromises } = require('fs');
const path = require('path');
const commonPathPrefix = require('common-path-prefix');
const findUp = require('find-up');

/** @type {string | undefined} */
let packageJsonType;
Expand Down Expand Up @@ -45,7 +47,17 @@ async function getModuleSystem(ModuleFilenameHelpers, options) {
// We will cache the results in a global variable so it will only be parsed once.
if (!packageJsonType) {
try {
const packageJsonPath = require.resolve(path.join(this.rootContext, 'package.json'));
const commonPath = commonPathPrefix([this.rootContext, this.resourcePath], '/');
const stopPath = path.resolve(commonPath, '..');

const packageJsonPath = await findUp(
(dir) => {
if (dir === stopPath) return findUp.stop;
return 'package.json';
},
{ cwd: path.dirname(this.resourcePath) }
);

const buffer = await fsPromises.readFile(packageJsonPath, { encoding: 'utf-8' });
const rawPackageJson = buffer.toString('utf-8');
({ type: packageJsonType } = JSON.parse(rawPackageJson));
Expand Down
3 changes: 3 additions & 0 deletions overlay/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
},
"dependencies": {
"ansi-html": "^0.0.7",
"common-path-prefix": "^3.0.0",
"core-js-pure": "^3.8.1",
"error-stack-parser": "^2.0.6",
"find-up": "^5.0.0",
"html-entities": "^2.1.0",
"loader-utils": "^2.0.0",
"schema-utils": "^3.0.0",
Expand Down
3 changes: 3 additions & 0 deletions sockets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
6 changes: 3 additions & 3 deletions test/loader/unit/getModuleSystem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('getModuleSystem', () => {
await expect(
getModuleSystem.call(
{
resourcePath: 'index.js',
resourcePath: path.resolve(__dirname, '..', 'fixtures/esm', 'index.js'),
rootContext: path.resolve(__dirname, '..', 'fixtures/esm'),
},
ModuleFilenameHelpers,
Expand All @@ -75,7 +75,7 @@ describe('getModuleSystem', () => {
await expect(
getModuleSystem.call(
{
resourcePath: 'index.js',
resourcePath: path.resolve(__dirname, '..', 'fixtures/cjs', 'index.js'),
rootContext: path.resolve(__dirname, '..', 'fixtures/cjs'),
},
ModuleFilenameHelpers,
Expand All @@ -88,7 +88,7 @@ describe('getModuleSystem', () => {
await expect(
getModuleSystem.call(
{
resourcePath: 'index.js',
resourcePath: path.resolve(__dirname, '..', 'fixtures/auto', 'index.js'),
rootContext: path.resolve(__dirname, '..', 'fixtures/auto'),
},
ModuleFilenameHelpers,
Expand Down
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,11 @@ commander@^7.0.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7"
integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==

common-path-prefix@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/common-path-prefix/-/common-path-prefix-3.0.0.tgz#7d007a7e07c58c4b4d5f433131a19141b29f11e0"
integrity sha512-QE33hToZseCH3jS0qN96O/bSh3kaw/h+Tq7ngyY9eWDUnTlTNUyqfqvCXioLe5Na5jFsL78ra/wuBU4iuEgd4w==

commondir@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
Expand Down Expand Up @@ -3167,6 +3172,14 @@ find-up@^4.0.0, find-up@^4.1.0:
locate-path "^5.0.0"
path-exists "^4.0.0"

find-up@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-5.0.0.tgz#4c92819ecb7083561e4f4a240a86be5198f536fc"
integrity sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==
dependencies:
locate-path "^6.0.0"
path-exists "^4.0.0"

findup-sync@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/findup-sync/-/findup-sync-3.0.0.tgz#17b108f9ee512dfb7a5c7f3c8b27ea9e1a9c08d1"
Expand Down Expand Up @@ -5058,6 +5071,13 @@ locate-path@^5.0.0:
dependencies:
p-locate "^4.1.0"

locate-path@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-6.0.0.tgz#55321eb309febbc59c4801d931a72452a681d286"
integrity sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==
dependencies:
p-locate "^5.0.0"

lodash._reinterpolate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down Expand Up @@ -5783,7 +5803,7 @@ p-limit@^2.0.0, p-limit@^2.2.0:
dependencies:
p-try "^2.0.0"

p-limit@^3.1.0:
p-limit@^3.0.2, p-limit@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b"
integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==
Expand All @@ -5804,6 +5824,13 @@ p-locate@^4.1.0:
dependencies:
p-limit "^2.2.0"

p-locate@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834"
integrity sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==
dependencies:
p-limit "^3.0.2"

p-map@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/p-map/-/p-map-2.1.0.tgz#310928feef9c9ecc65b68b17693018a665cea175"
Expand Down

0 comments on commit 4569dab

Please sign in to comment.