From cd8e286e2c9d83817f7f5742b8bd9e3c523f57d9 Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Thu, 25 Jul 2024 12:42:40 +0800 Subject: [PATCH 1/3] fix(server-core): support cjs async server loader entry --- .changeset/chilly-rules-dress.md | 6 ++++++ packages/server/core/src/adapters/node/plugins/resource.ts | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 .changeset/chilly-rules-dress.md diff --git a/.changeset/chilly-rules-dress.md b/.changeset/chilly-rules-dress.md new file mode 100644 index 000000000000..27edf70edad5 --- /dev/null +++ b/.changeset/chilly-rules-dress.md @@ -0,0 +1,6 @@ +--- +'@modern-js/server-core': patch +--- + +fix(server-core): support cjs async server loader entry +fix(server-core): 支持 cjs 类型的异步 server loader entry diff --git a/packages/server/core/src/adapters/node/plugins/resource.ts b/packages/server/core/src/adapters/node/plugins/resource.ts index d72c72a82703..79cc7e70d1cb 100644 --- a/packages/server/core/src/adapters/node/plugins/resource.ts +++ b/packages/server/core/src/adapters/node/plugins/resource.ts @@ -52,6 +52,9 @@ export function injectTemplates( const dynamicImport = (filePath: string) => { try { const module = require(filePath); + if (module.default) { + return Promise.resolve(module.default); + } return Promise.resolve(module); } catch (e) { return Promise.reject(e); From e77d2511050d23955fe2795847de8827ff22ef36 Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Mon, 29 Jul 2024 17:47:18 +0800 Subject: [PATCH 2/3] chore: resolve m.default --- packages/server/core/src/adapters/node/plugins/resource.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/server/core/src/adapters/node/plugins/resource.ts b/packages/server/core/src/adapters/node/plugins/resource.ts index 79cc7e70d1cb..298660258e30 100644 --- a/packages/server/core/src/adapters/node/plugins/resource.ts +++ b/packages/server/core/src/adapters/node/plugins/resource.ts @@ -55,7 +55,12 @@ const dynamicImport = (filePath: string) => { if (module.default) { return Promise.resolve(module.default); } - return Promise.resolve(module); + return Promise.resolve(module).then(m => { + if (m.default) { + return m.default; + } + return m; + }); } catch (e) { return Promise.reject(e); } From f1c7477c25eef1e70d9701b6555a9b8a195e7b76 Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Mon, 29 Jul 2024 19:13:00 +0800 Subject: [PATCH 3/3] fix: support async node target --- packages/cli/plugin-data-loader/src/cli/loader.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/cli/plugin-data-loader/src/cli/loader.ts b/packages/cli/plugin-data-loader/src/cli/loader.ts index e972b80612f8..c80cba6787fd 100644 --- a/packages/cli/plugin-data-loader/src/cli/loader.ts +++ b/packages/cli/plugin-data-loader/src/cli/loader.ts @@ -35,6 +35,13 @@ export default async function loader( return source; } + if ( + target === 'async-node' || + (Array.isArray(target) && target.includes('async-node')) + ) { + return source; + } + const { resourceQuery } = this; // parse options from resouceQuery const options = resourceQuery