Skip to content

Commit

Permalink
chore: ESM should be used in CTS files
Browse files Browse the repository at this point in the history
  • Loading branch information
condorheroblog committed Dec 31, 2023
1 parent 2167b6a commit 020f5e0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/playground/react-sample/fake/index.fake.cjs
Original file line number Diff line number Diff line change
@@ -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: () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/playground/react-sample/fake/index.fake.cts
Original file line number Diff line number Diff line change
@@ -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: () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-fake-server/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down

0 comments on commit 020f5e0

Please sign in to comment.