diff --git a/.changeset/kind-panthers-fail.md b/.changeset/kind-panthers-fail.md new file mode 100644 index 00000000000..fa18abd0d27 --- /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 3de46af57f7..f3936fed46d 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 d762c0bb470..bc6e12f4ec5 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 867300913de..57812dc9b2a 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: