From 4569dab82aefbba07fd74d5f72a19b95ec4a072e Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Mon, 30 Aug 2021 00:47:10 +0200 Subject: [PATCH] fix: resolve module system upwards from resource path (#492) --- client/package.json | 3 +++ loader/utils/getModuleSystem.js | 14 +++++++++++- overlay/package.json | 3 +++ package.json | 2 ++ sockets/package.json | 3 +++ test/loader/unit/getModuleSystem.test.js | 6 ++--- yarn.lock | 29 +++++++++++++++++++++++- 7 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 client/package.json create mode 100644 overlay/package.json create mode 100644 sockets/package.json diff --git a/client/package.json b/client/package.json new file mode 100644 index 00000000..5bbefffb --- /dev/null +++ b/client/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/loader/utils/getModuleSystem.js b/loader/utils/getModuleSystem.js index 1cebfe5c..7d214327 100644 --- a/loader/utils/getModuleSystem.js +++ b/loader/utils/getModuleSystem.js @@ -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; @@ -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)); diff --git a/overlay/package.json b/overlay/package.json new file mode 100644 index 00000000..5bbefffb --- /dev/null +++ b/overlay/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/package.json b/package.json index 77addc93..ed6c8c00 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/sockets/package.json b/sockets/package.json new file mode 100644 index 00000000..5bbefffb --- /dev/null +++ b/sockets/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/test/loader/unit/getModuleSystem.test.js b/test/loader/unit/getModuleSystem.test.js index 8d1a122a..1c1edc7c 100644 --- a/test/loader/unit/getModuleSystem.test.js +++ b/test/loader/unit/getModuleSystem.test.js @@ -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, @@ -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, @@ -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, diff --git a/yarn.lock b/yarn.lock index 533e7e8f..ee66804d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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== @@ -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"