From 0f70a92ec93e8abff6dcc7d726804aff2550f281 Mon Sep 17 00:00:00 2001 From: Lachlan Heywood Date: Fri, 22 Nov 2024 17:59:55 -0500 Subject: [PATCH 1/5] chore: update jiti to v2 --- packages/docusaurus-utils/package.json | 2 +- yarn.lock | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-utils/package.json b/packages/docusaurus-utils/package.json index b4bfab548f78..46177a65b313 100644 --- a/packages/docusaurus-utils/package.json +++ b/packages/docusaurus-utils/package.json @@ -28,7 +28,7 @@ "github-slugger": "^1.5.0", "globby": "^11.1.0", "gray-matter": "^4.0.3", - "jiti": "^1.20.0", + "jiti": "^2.4.0", "js-yaml": "^4.1.0", "lodash": "^4.17.21", "micromatch": "^4.0.5", diff --git a/yarn.lock b/yarn.lock index 1ead5c589d83..5cc243205a91 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11021,6 +11021,11 @@ jiti@^1.20.0: resolved "https://registry.yarnpkg.com/jiti/-/jiti-1.21.6.tgz#6c7f7398dd4b3142767f9a168af2f317a428d268" integrity sha512-2yTgeWTWzMWkHu6Jp9NKgePDaYHbntiwvYuuJLbbN9vl7DC9DvXKOB2BC3ZZ92D3cvV/aflH0osDfwpHepQ53w== +jiti@^2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/jiti/-/jiti-2.4.0.tgz#393d595fb6031a11d11171b5e4fc0b989ba3e053" + integrity sha512-H5UpaUI+aHOqZXlYOaFP/8AzKsg+guWu+Pr3Y8i7+Y3zr1aXAvCvTAQ1RxSc6oVD8R8c7brgNtTVP91E7upH/g== + jkroso-type@1: version "1.1.1" resolved "https://registry.yarnpkg.com/jkroso-type/-/jkroso-type-1.1.1.tgz#bc4ced6d6c45fe0745282bafc86a9f8c4fc9ce61" From 7577a0bf6307db957ce53a01c8d2bfd8fd0e8d49 Mon Sep 17 00:00:00 2001 From: Lachlan Heywood Date: Sat, 23 Nov 2024 00:18:41 -0500 Subject: [PATCH 2/5] fix: update loadFreshModule to be compatible with jiti1, but reflect jiti2 usage --- packages/docusaurus-utils/src/moduleUtils.ts | 24 ++++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus-utils/src/moduleUtils.ts b/packages/docusaurus-utils/src/moduleUtils.ts index e7adaf9880e5..814f59630724 100644 --- a/packages/docusaurus-utils/src/moduleUtils.ts +++ b/packages/docusaurus-utils/src/moduleUtils.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import jiti from 'jiti'; +import {createJiti} from 'jiti'; import logger from '@docusaurus/logger'; /* @@ -18,12 +18,12 @@ export async function loadFreshModule(modulePath: string): Promise { logger.interpolate`Invalid module path of type name=${modulePath}`, ); } - const load = jiti(__filename, { + const jiti = createJiti(__dirname, { // Transpilation cache, can be safely enabled - cache: true, + fsCache: true, // Bypass Node.js runtime require cache // Same as "import-fresh" package we used previously - requireCache: false, + moduleCache: false, // Only take into consideration the default export // For now we don't need named exports // This also helps normalize return value for both CJS/ESM/TS modules @@ -31,7 +31,21 @@ export async function loadFreshModule(modulePath: string): Promise { // debug: true, }); - return load(modulePath); + const module = await jiti.import(modulePath); + if (!module) { + return undefined; + } + + if (typeof module !== 'object') { + return module; + } + + if ('default' in module) { + const {default: def, ...rest} = module; + return {...(def || {}), ...rest}; + } + + return module; } catch (error) { throw new Error( logger.interpolate`Docusaurus could not load module at path path=${modulePath}\nCause: ${ From ffc2a0042afb6323780132273dc7ce487104473c Mon Sep 17 00:00:00 2001 From: Lachlan Heywood Date: Sat, 23 Nov 2024 00:39:01 -0500 Subject: [PATCH 3/5] fix: use mlly.interopDefault instead of custom impl --- packages/docusaurus-utils/package.json | 1 + packages/docusaurus-utils/src/moduleUtils.ts | 16 ++-------- yarn.lock | 31 +++++++++++++++++++- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/packages/docusaurus-utils/package.json b/packages/docusaurus-utils/package.json index 46177a65b313..0ecb2b73bb58 100644 --- a/packages/docusaurus-utils/package.json +++ b/packages/docusaurus-utils/package.json @@ -32,6 +32,7 @@ "js-yaml": "^4.1.0", "lodash": "^4.17.21", "micromatch": "^4.0.5", + "mlly": "^1.7.3", "prompts": "^2.4.2", "resolve-pathname": "^3.0.0", "shelljs": "^0.8.5", diff --git a/packages/docusaurus-utils/src/moduleUtils.ts b/packages/docusaurus-utils/src/moduleUtils.ts index 814f59630724..b526c731da15 100644 --- a/packages/docusaurus-utils/src/moduleUtils.ts +++ b/packages/docusaurus-utils/src/moduleUtils.ts @@ -32,20 +32,8 @@ export async function loadFreshModule(modulePath: string): Promise { }); const module = await jiti.import(modulePath); - if (!module) { - return undefined; - } - - if (typeof module !== 'object') { - return module; - } - - if ('default' in module) { - const {default: def, ...rest} = module; - return {...(def || {}), ...rest}; - } - - return module; + const {interopDefault} = await import('mlly'); + return interopDefault(module); } catch (error) { throw new Error( logger.interpolate`Docusaurus could not load module at path path=${modulePath}\nCause: ${ diff --git a/yarn.lock b/yarn.lock index 5cc243205a91..13e79f484008 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4803,6 +4803,11 @@ acorn@^8.0.0, acorn@^8.0.4, acorn@^8.1.0, acorn@^8.10.0, acorn@^8.11.0, acorn@^8 resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.13.0.tgz#2a30d670818ad16ddd6a35d3842dacec9e5d7ca3" integrity sha512-8zSiw54Oxrdym50NlZ9sUusyO1Z1ZchgRLWRaK6c86XJFClyCgFKetdowBg5bKxyp/u+CDBJG4Mpp0m3HLZl9w== +acorn@^8.14.0: + version "8.14.0" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.14.0.tgz#063e2c70cac5fb4f6467f0b11152e04c682795b0" + integrity sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA== + add-stream@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/add-stream/-/add-stream-1.0.0.tgz#6a7990437ca736d5e1288db92bd3266d5f5cb2aa" @@ -6238,6 +6243,11 @@ confbox@^0.1.7: resolved "https://registry.yarnpkg.com/confbox/-/confbox-0.1.7.tgz#ccfc0a2bcae36a84838e83a3b7f770fb17d6c579" integrity sha512-uJcB/FKZtBMCJpK8MQji6bJHgu1tixKPxRLeGkNzBoOZzpnZUJm0jm2/sBDWcuBx1dYgxV4JU+g5hmNxCyAmdA== +confbox@^0.1.8: + version "0.1.8" + resolved "https://registry.yarnpkg.com/confbox/-/confbox-0.1.8.tgz#820d73d3b3c82d9bd910652c5d4d599ef8ff8b06" + integrity sha512-RMtmw0iFkeR4YV+fUOSucriAQNb9g8zFR52MWCtl+cCZOFRNL6zeB395vPzFhEjjn4fMxXudmELnl/KF/WrK6w== + config-chain@1.1.12: version "1.1.12" resolved "https://registry.yarnpkg.com/config-chain/-/config-chain-1.1.12.tgz#0fde8d091200eb5e808caf25fe618c02f48e4efa" @@ -12955,6 +12965,16 @@ mlly@^1.4.2, mlly@^1.7.1: pkg-types "^1.1.1" ufo "^1.5.3" +mlly@^1.7.2, mlly@^1.7.3: + version "1.7.3" + resolved "https://registry.yarnpkg.com/mlly/-/mlly-1.7.3.tgz#d86c0fcd8ad8e16395eb764a5f4b831590cee48c" + integrity sha512-xUsx5n/mN0uQf4V548PKQ+YShA4/IW0KI1dZhrNrPCLG+xizETbHTkOa1f8/xut9JRPp8kQuMnz0oqwkTiLo/A== + dependencies: + acorn "^8.14.0" + pathe "^1.1.2" + pkg-types "^1.2.1" + ufo "^1.5.4" + modify-values@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022" @@ -14139,6 +14159,15 @@ pkg-types@^1.0.3, pkg-types@^1.1.1: mlly "^1.7.1" pathe "^1.1.2" +pkg-types@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/pkg-types/-/pkg-types-1.2.1.tgz#6ac4e455a5bb4b9a6185c1c79abd544c901db2e5" + integrity sha512-sQoqa8alT3nHjGuTjuKgOnvjo4cljkufdtLMnO2LBP/wRwuDlo1tkaEdMxCRhyGRPacv/ztlZgDPm2b7FAmEvw== + dependencies: + confbox "^0.1.8" + mlly "^1.7.2" + pathe "^1.1.2" + pkg-up@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-3.1.0.tgz#100ec235cc150e4fd42519412596a28512a0def5" @@ -17647,7 +17676,7 @@ typescript@~5.6.2: resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.3.tgz#5f3449e31c9d94febb17de03cc081dd56d81db5b" integrity sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw== -ufo@^1.5.3: +ufo@^1.5.3, ufo@^1.5.4: version "1.5.4" resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.5.4.tgz#16d6949674ca0c9e0fbbae1fa20a71d7b1ded754" integrity sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ== From bd88110d417ef357a592ebc218680302abb4798d Mon Sep 17 00:00:00 2001 From: Lachlan Heywood Date: Tue, 26 Nov 2024 10:10:58 -0500 Subject: [PATCH 4/5] fix: use __filename instead of __dirname --- packages/docusaurus-utils/src/moduleUtils.ts | 2 +- yarn.lock | 37 +++----------------- 2 files changed, 5 insertions(+), 34 deletions(-) diff --git a/packages/docusaurus-utils/src/moduleUtils.ts b/packages/docusaurus-utils/src/moduleUtils.ts index b526c731da15..24faccdb8309 100644 --- a/packages/docusaurus-utils/src/moduleUtils.ts +++ b/packages/docusaurus-utils/src/moduleUtils.ts @@ -18,7 +18,7 @@ export async function loadFreshModule(modulePath: string): Promise { logger.interpolate`Invalid module path of type name=${modulePath}`, ); } - const jiti = createJiti(__dirname, { + const jiti = createJiti(__filename, { // Transpilation cache, can be safely enabled fsCache: true, // Bypass Node.js runtime require cache diff --git a/yarn.lock b/yarn.lock index 13e79f484008..0b01850e9e15 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4798,12 +4798,7 @@ acorn@^6.1.1: resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.4.2.tgz#35866fd710528e92de10cf06016498e47e39e1e6" integrity sha512-XtGIhXwF8YM8bJhGxG5kXgjkEuNGLTkoYqVE+KMR+aspr4KGYmKYg7yUe3KghyQ9yheNwLnjmzh/7+gfDBmHCQ== -acorn@^8.0.0, acorn@^8.0.4, acorn@^8.1.0, acorn@^8.10.0, acorn@^8.11.0, acorn@^8.11.3, acorn@^8.7.1, acorn@^8.8.1, acorn@^8.8.2, acorn@^8.9.0: - version "8.13.0" - resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.13.0.tgz#2a30d670818ad16ddd6a35d3842dacec9e5d7ca3" - integrity sha512-8zSiw54Oxrdym50NlZ9sUusyO1Z1ZchgRLWRaK6c86XJFClyCgFKetdowBg5bKxyp/u+CDBJG4Mpp0m3HLZl9w== - -acorn@^8.14.0: +acorn@^8.0.0, acorn@^8.0.4, acorn@^8.1.0, acorn@^8.10.0, acorn@^8.11.0, acorn@^8.14.0, acorn@^8.7.1, acorn@^8.8.1, acorn@^8.8.2, acorn@^8.9.0: version "8.14.0" resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.14.0.tgz#063e2c70cac5fb4f6467f0b11152e04c682795b0" integrity sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA== @@ -6238,11 +6233,6 @@ concat-stream@^2.0.0: readable-stream "^3.0.2" typedarray "^0.0.6" -confbox@^0.1.7: - version "0.1.7" - resolved "https://registry.yarnpkg.com/confbox/-/confbox-0.1.7.tgz#ccfc0a2bcae36a84838e83a3b7f770fb17d6c579" - integrity sha512-uJcB/FKZtBMCJpK8MQji6bJHgu1tixKPxRLeGkNzBoOZzpnZUJm0jm2/sBDWcuBx1dYgxV4JU+g5hmNxCyAmdA== - confbox@^0.1.8: version "0.1.8" resolved "https://registry.yarnpkg.com/confbox/-/confbox-0.1.8.tgz#820d73d3b3c82d9bd910652c5d4d599ef8ff8b06" @@ -12955,17 +12945,7 @@ mkdirp@^1.0.3, mkdirp@^1.0.4: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== -mlly@^1.4.2, mlly@^1.7.1: - version "1.7.1" - resolved "https://registry.yarnpkg.com/mlly/-/mlly-1.7.1.tgz#e0336429bb0731b6a8e887b438cbdae522c8f32f" - integrity sha512-rrVRZRELyQzrIUAVMHxP97kv+G786pHmOKzuFII8zDYahFBS7qnHh2AlYSl1GAHhaMPCz6/oHjVMcfFYgFYHgA== - dependencies: - acorn "^8.11.3" - pathe "^1.1.2" - pkg-types "^1.1.1" - ufo "^1.5.3" - -mlly@^1.7.2, mlly@^1.7.3: +mlly@^1.4.2, mlly@^1.7.1, mlly@^1.7.2, mlly@^1.7.3: version "1.7.3" resolved "https://registry.yarnpkg.com/mlly/-/mlly-1.7.3.tgz#d86c0fcd8ad8e16395eb764a5f4b831590cee48c" integrity sha512-xUsx5n/mN0uQf4V548PKQ+YShA4/IW0KI1dZhrNrPCLG+xizETbHTkOa1f8/xut9JRPp8kQuMnz0oqwkTiLo/A== @@ -14150,16 +14130,7 @@ pkg-dir@^7.0.0: dependencies: find-up "^6.3.0" -pkg-types@^1.0.3, pkg-types@^1.1.1: - version "1.2.0" - resolved "https://registry.yarnpkg.com/pkg-types/-/pkg-types-1.2.0.tgz#d0268e894e93acff11a6279de147e83354ebd42d" - integrity sha512-+ifYuSSqOQ8CqP4MbZA5hDpb97n3E8SVWdJe+Wms9kj745lmd3b7EZJiqvmLwAlmRfjrI7Hi5z3kdBJ93lFNPA== - dependencies: - confbox "^0.1.7" - mlly "^1.7.1" - pathe "^1.1.2" - -pkg-types@^1.2.1: +pkg-types@^1.0.3, pkg-types@^1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/pkg-types/-/pkg-types-1.2.1.tgz#6ac4e455a5bb4b9a6185c1c79abd544c901db2e5" integrity sha512-sQoqa8alT3nHjGuTjuKgOnvjo4cljkufdtLMnO2LBP/wRwuDlo1tkaEdMxCRhyGRPacv/ztlZgDPm2b7FAmEvw== @@ -17676,7 +17647,7 @@ typescript@~5.6.2: resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.3.tgz#5f3449e31c9d94febb17de03cc081dd56d81db5b" integrity sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw== -ufo@^1.5.3, ufo@^1.5.4: +ufo@^1.5.4: version "1.5.4" resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.5.4.tgz#16d6949674ca0c9e0fbbae1fa20a71d7b1ded754" integrity sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ== From 2322b1de4d2467d63311fbeaa82b64546fdd9029 Mon Sep 17 00:00:00 2001 From: Lachlan Heywood Date: Tue, 26 Nov 2024 12:16:12 -0500 Subject: [PATCH 5/5] fix: add test for named export in fresh module --- .../src/__tests__/moduleUtils.test.ts | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/moduleUtils.test.ts b/packages/docusaurus-utils/src/__tests__/moduleUtils.test.ts index 653667292a9a..dd0c8bf5c3c0 100644 --- a/packages/docusaurus-utils/src/__tests__/moduleUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/moduleUtils.test.ts @@ -122,8 +122,10 @@ describe('loadFreshModule', () => { import dependency1 from "./dependency1"; import dependency2 from "./dependency2"; + export const entryValue = "entryVal1"; + export default { - someEntryValue: "entryVal", + someEntryValue: "entryVal2", dependency1, dependency2 }; @@ -132,7 +134,8 @@ describe('loadFreshModule', () => { // Should be able to read the initial module graph await expect(entryFile.load()).resolves.toEqual({ - someEntryValue: 'entryVal', + entryValue: 'entryVal1', + someEntryValue: 'entryVal2', dependency1: { dep1Export: 'dep1 val1', dep1Val: 'dep1 val2', @@ -159,7 +162,8 @@ describe('loadFreshModule', () => { `, ); await expect(entryFile.load()).resolves.toEqual({ - someEntryValue: 'entryVal', + entryValue: 'entryVal1', + someEntryValue: 'entryVal2', dependency1: { dep1Export: 'dep1 val1 updated', dep1Val: 'dep1 val2 updated', @@ -184,7 +188,8 @@ describe('loadFreshModule', () => { `, ); await expect(entryFile.load()).resolves.toEqual({ - someEntryValue: 'entryVal', + entryValue: 'entryVal1', + someEntryValue: 'entryVal2', dependency1: { dep1Export: 'dep1 val1 updated', dep1Val: 'dep1 val2 updated', @@ -208,8 +213,10 @@ describe('loadFreshModule', () => { import dependency1 from "./dependency1"; import dependency2 from "./dependency2"; + export const entryValue = "entryVal1 updated"; + export default { - someEntryValue: "entryVal updated", + someEntryValue: "entryVal2 updated", dependency1, dependency2, newAttribute: "is there" @@ -217,7 +224,8 @@ describe('loadFreshModule', () => { `, ); await expect(entryFile.load()).resolves.toEqual({ - someEntryValue: 'entryVal updated', + entryValue: 'entryVal1 updated', + someEntryValue: 'entryVal2 updated', newAttribute: 'is there', dependency1: { dep1Export: 'dep1 val1 updated',