diff --git a/src/config.ts b/src/config.ts index fe04c84..c874bc9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -20,7 +20,7 @@ import assetPlugin from './plugins/asset' import workerPlugin from './plugins/worker' import importMetaUrlPlugin from './plugins/importMetaUrl' import esmShimPlugin from './plugins/esm' -import { isObject } from './utils' +import { isObject, isFilePathESM } from './utils' export { defineConfig as defineViteConfig } from 'vite' @@ -240,11 +240,7 @@ export async function loadConfigFromFile( } } - // electron does not support adding type: "module" to package.json - let isESM = false - if (/\.m[jt]s$/.test(resolvedPath) || resolvedPath.endsWith('.ts')) { - isESM = true - } + const isESM = isFilePathESM(resolvedPath) try { const bundled = await bundleConfigFile(resolvedPath, isESM) diff --git a/src/utils.ts b/src/utils.ts index e53be1a..6ba3f7b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -73,3 +73,14 @@ export function loadPackageData(root = process.cwd()): PackageData | null { } return null } + +export function isFilePathESM(filePath: string): boolean { + if (/\.m[jt]s$/.test(filePath) || filePath.endsWith('.ts')) { + return true + } else if (/\.c[jt]s$/.test(filePath)) { + return false + } else { + const pkg = loadPackageData() + return pkg?.type === 'module' + } +}