From 020f5e0983da7b0dbdde530d6241d47a47c0ac32 Mon Sep 17 00:00:00 2001 From: CondorHero Date: Sun, 31 Dec 2023 17:35:34 +0800 Subject: [PATCH] chore: ESM should be used in CTS files --- packages/playground/react-sample/fake/index.fake.cjs | 3 ++- packages/playground/react-sample/fake/index.fake.cts | 6 +++++- packages/vite-plugin-fake-server/src/plugin.ts | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/playground/react-sample/fake/index.fake.cjs b/packages/playground/react-sample/fake/index.fake.cjs index 96ee578..bf5e75c 100644 --- a/packages/playground/react-sample/fake/index.fake.cjs +++ b/packages/playground/react-sample/fake/index.fake.cjs @@ -1,6 +1,7 @@ const { defineFakeRoute } = require("vite-plugin-fake-server/client"); -exports.default = defineFakeRoute({ +// exports.default +module.exports = defineFakeRoute({ url: "/response-in-cjs-file", timeout: 2000, response: () => { diff --git a/packages/playground/react-sample/fake/index.fake.cts b/packages/playground/react-sample/fake/index.fake.cts index 9ee6302..b0b0744 100644 --- a/packages/playground/react-sample/fake/index.fake.cts +++ b/packages/playground/react-sample/fake/index.fake.cts @@ -1,4 +1,8 @@ -module.exports = { +// https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#new-file-extensions +// https://twitter.com/atcb/status/1528501621025406976 +// `.cts` 使用的是 ESM 模块而不是 CommonJS 模块 + +export default { url: "/response-in-cts-file", timeout: 2000, response: () => { diff --git a/packages/vite-plugin-fake-server/src/plugin.ts b/packages/vite-plugin-fake-server/src/plugin.ts index 3350460..e6c0da8 100644 --- a/packages/vite-plugin-fake-server/src/plugin.ts +++ b/packages/vite-plugin-fake-server/src/plugin.ts @@ -114,6 +114,7 @@ export const vitePluginFakeServer = async (options: VitePluginFakeServerOptions convertPathToPosix("/" + relative(config.root, filePath)), ); + // import.meta.glob imports the CommonJS module, which has the default object by default const fakeTemplate = ` const modules = import.meta.glob(${JSON.stringify(relativeFakeFilePath, null, 2)}, { eager: true }); const fakeModuleList = Object.keys(modules).reduce((list, key) => { @@ -125,8 +126,7 @@ export const vitePluginFakeServer = async (options: VitePluginFakeServerOptions return [...list, ...modList]; } } else { - const modList = Array.isArray(module) ? [...module] : [module]; - return [...list, ...modList]; + return list; } }, []); window.__VITE__PLUGIN__FAKE__SERVER__.fakeModuleList = fakeModuleList;