Skip to content

Commit

Permalink
Merge branch 'main' into feat/plugin-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin authored Jan 23, 2025
2 parents c89aaa2 + b69c8fa commit 5506892
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .changeset/kind-panthers-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/plugin-v2': patch
---

feat: use `jiti` to read config file for supporting esm import

feat: 使用 `jiti` 读取配置文件,支持 esm 导入
6 changes: 6 additions & 0 deletions .changeset/plenty-cobras-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/app-tools': patch
---

fix: remove register esbuild in registerEsm & should log status correctly
fix: 移除在 registerEsm 中注册 esbuild & 正确地打印 status
9 changes: 8 additions & 1 deletion packages/server/core/src/plugins/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ function logHandler(): Middleware<ServerEnv> {
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),
);
};
}

Expand Down
6 changes: 0 additions & 6 deletions packages/solutions/app-tools/src/esm/register-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/plugin-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 26 additions & 30 deletions packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -60,35 +56,39 @@ export const clearFilesOverTime = async (
}
};

const bundleRequireWithCatch = async (
configFile: string,
{ appDirectory }: { appDirectory: string },
): Promise<any> => {
/**
* Load a configuration file dynamically
* @param {string} configFile - Path to the configuration file (absolute or relative)
* @returns {any} - The loaded configuration object
*/
function loadConfigContent<T>(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 || ''
}`;
}
throw e;
}
};
}

/**
* Parse and load user config file, support extensions like .ts, mjs, js, ejs.
Expand Down Expand Up @@ -118,11 +118,7 @@ export const loadConfig = async <T>(
let config: T | undefined;

if (configFile) {
delete require.cache[configFile];

const mod = await bundleRequireWithCatch(configFile, { appDirectory });

config = mod.default || mod;
config = loadConfigContent<T>(configFile);
}

return {
Expand Down
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5506892

Please sign in to comment.