From f3876c19bf5176a496d14634dc4f030bdcb91e61 Mon Sep 17 00:00:00 2001 From: Ming Date: Thu, 23 Jan 2025 14:58:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20remove=20register=20esbuild=20in=20r?= =?UTF-8?q?egisterEsm=20&=20should=20log=20status=20corre=E2=80=A6=20(#675?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/plenty-cobras-brush.md | 6 ++++++ packages/server/core/src/plugins/log.ts | 9 ++++++++- packages/solutions/app-tools/src/esm/register-esm.mjs | 6 ------ 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 .changeset/plenty-cobras-brush.md diff --git a/.changeset/plenty-cobras-brush.md b/.changeset/plenty-cobras-brush.md new file mode 100644 index 000000000000..236da8fa03a1 --- /dev/null +++ b/.changeset/plenty-cobras-brush.md @@ -0,0 +1,6 @@ +--- +'@modern-js/app-tools': patch +--- + +fix: remove register esbuild in registerEsm & should log status correctly +fix: 移除在 registerEsm 中注册 esbuild & 正确地打印 status diff --git a/packages/server/core/src/plugins/log.ts b/packages/server/core/src/plugins/log.ts index 19526851cb58..0dcd6a121b64 100644 --- a/packages/server/core/src/plugins/log.ts +++ b/packages/server/core/src/plugins/log.ts @@ -73,7 +73,14 @@ function logHandler(): Middleware { log(logFn, LogPrefix.Incoming, method, path); const start = Date.now(); await next(); - log(logFn, LogPrefix.Outgoing, method, path, c.res.status, time(start)); + log( + logFn, + LogPrefix.Outgoing, + method, + path, + (c?.env as any)?.node?.res?.statusCode ?? c.res.status, + time(start), + ); }; } diff --git a/packages/solutions/app-tools/src/esm/register-esm.mjs b/packages/solutions/app-tools/src/esm/register-esm.mjs index 4f2680029a02..f887f0daa4ae 100644 --- a/packages/solutions/app-tools/src/esm/register-esm.mjs +++ b/packages/solutions/app-tools/src/esm/register-esm.mjs @@ -48,12 +48,6 @@ export const registerEsm = async ({ appDir, distDir, alias }) => { if (hasTsconfig) { tsConfig = readTsConfigByFile(tsconfigPath); } - const esbuildRegister = await import('esbuild-register/dist/node'); - esbuildRegister.register({ - tsconfigRaw: hasTsconfig ? tsConfig : undefined, - hookIgnoreNodeModules: true, - hookMatcher: fileName => !fileName.startsWith(distDir), - }); register('./esbuild-loader.mjs', import.meta.url, { data: { appDir, From b69c8fa3027df050255893cce24f4e9c937ce30b Mon Sep 17 00:00:00 2001 From: Belinda Cao Date: Thu, 23 Jan 2025 16:34:30 +0800 Subject: [PATCH 2/2] feat: use `jiti` to read config file for supporting esm import (#6751) --- .changeset/kind-panthers-fail.md | 7 +++ packages/toolkit/plugin-v2/package.json | 2 +- .../src/cli/run/config/loadConfig.ts | 56 +++++++++---------- pnpm-lock.yaml | 12 +++- 4 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 .changeset/kind-panthers-fail.md diff --git a/.changeset/kind-panthers-fail.md b/.changeset/kind-panthers-fail.md new file mode 100644 index 000000000000..fa18abd0d279 --- /dev/null +++ b/.changeset/kind-panthers-fail.md @@ -0,0 +1,7 @@ +--- +'@modern-js/plugin-v2': patch +--- + +feat: use `jiti` to read config file for supporting esm import + +feat: 使用 `jiti` 读取配置文件,支持 esm 导入 diff --git a/packages/toolkit/plugin-v2/package.json b/packages/toolkit/plugin-v2/package.json index 3de46af57f77..f3936fed46d3 100644 --- a/packages/toolkit/plugin-v2/package.json +++ b/packages/toolkit/plugin-v2/package.json @@ -70,7 +70,7 @@ "test": "jest" }, "dependencies": { - "@modern-js/node-bundle-require": "workspace:*", + "jiti": "1.21.7", "@modern-js/utils": "workspace:*", "@modern-js/runtime-utils": "workspace:*", "@swc/helpers": "0.5.13" diff --git a/packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts b/packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts index d762c0bb4706..bc6e12f4ec59 100644 --- a/packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts +++ b/packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts @@ -1,11 +1,7 @@ import type { Stats } from 'fs'; import path from 'path'; -import { - bundleRequire, - defaultGetOutputFile, -} from '@modern-js/node-bundle-require'; import { fs, CONFIG_CACHE_DIR, globby } from '@modern-js/utils'; - +import jiti from 'jiti'; /** * Get user config from package.json. * @param appDirectory - App root directory. @@ -60,27 +56,31 @@ export const clearFilesOverTime = async ( } }; -const bundleRequireWithCatch = async ( - configFile: string, - { appDirectory }: { appDirectory: string }, -): Promise => { +/** + * Load a configuration file dynamically + * @param {string} configFile - Path to the configuration file (absolute or relative) + * @returns {any} - The loaded configuration object + */ +function loadConfigContent(configFile: string): T { + // Create a jiti instance + const _require = jiti(__filename, { + esmResolve: true, + // disable require cache to support restart CLI and read the new config + requireCache: false, + interopDefault: true, + }); + // Check if the file exists + if (!fs.existsSync(configFile)) { + throw new Error(`Configuration file does not exist: ${configFile}`); + } + try { - const mod = await bundleRequire(configFile, { - autoClear: false, - getOutputFile: async (filePath: string) => { - const defaultOutputFileName = path.basename( - await defaultGetOutputFile(filePath), - ); - const outputPath = path.join(appDirectory, CONFIG_CACHE_DIR); - // 10 min - const timeLimit = 10 * 60; - await clearFilesOverTime(outputPath, timeLimit); - return path.join(outputPath, defaultOutputFileName); - }, - }); + // Dynamically load the configuration file using jiti + const config = _require(configFile); - return mod; - } catch (e) { + // If the file exports as ESM, access the `default` property + return config.default || config; + } catch (e: any) { if (e instanceof Error) { e.message = `Get Error while loading config file: ${configFile}, please check it and retry.\n${ e.message || '' @@ -88,7 +88,7 @@ const bundleRequireWithCatch = async ( } throw e; } -}; +} /** * Parse and load user config file, support extensions like .ts, mjs, js, ejs. @@ -118,11 +118,7 @@ export const loadConfig = async ( let config: T | undefined; if (configFile) { - delete require.cache[configFile]; - - const mod = await bundleRequireWithCatch(configFile, { appDirectory }); - - config = mod.default || mod; + config = loadConfigContent(configFile); } return { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 867300913dea..57812dc9b2a4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4012,9 +4012,6 @@ importers: packages/toolkit/plugin-v2: dependencies: - '@modern-js/node-bundle-require': - specifier: workspace:* - version: link:../node-bundle-require '@modern-js/runtime-utils': specifier: workspace:* version: link:../runtime-utils @@ -4024,6 +4021,9 @@ importers: '@swc/helpers': specifier: 0.5.13 version: 0.5.13 + jiti: + specifier: 1.21.7 + version: 1.21.7 devDependencies: '@modern-js/types': specifier: workspace:* @@ -16735,6 +16735,10 @@ packages: resolution: {integrity: sha512-3TV69ZbrvV6U5DfQimop50jE9Dl6J8O1ja1dvBbMba/sZ3YBEQqJ2VZRoQPVnhlzjNtU1vaXRZVrVjU4qtm8yA==} hasBin: true + jiti@1.21.7: + resolution: {integrity: sha512-/imKNG4EbWNrVjoNC/1H5/9GFy+tqjGBHCaSsN+P2RnPqjsLmv6UD3Ej+Kj8nBWaRAwyk7kK5ZUc+OEatnTR3A==} + hasBin: true + jju@1.4.0: resolution: {integrity: sha512-8wb9Yw966OSxApiCt0K3yNJL8pnNeIv+OEq2YMidz4FKP6nonSRoOXc80iXY4JaN2FC11B9qsNmDsm+ZOfMROA==} @@ -33670,6 +33674,8 @@ snapshots: jiti@1.20.0: {} + jiti@1.21.7: {} + jju@1.4.0: {} joi@17.13.3: