From 694e134a5219b44d8bbc7bce36fb7a956b2f598a Mon Sep 17 00:00:00 2001 From: alex8088 <244096523@qq.com> Date: Thu, 4 Jan 2024 00:00:57 +0800 Subject: [PATCH] feat: config file supports "type": "module" in package.json --- src/config.ts | 8 ++------ src/utils.ts | 11 +++++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) 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' + } +}