feat: config file supports "type": "module" in package.json

This commit is contained in:
alex8088 2024-01-04 00:00:57 +08:00
parent 40a1b64639
commit 694e134a52
2 changed files with 13 additions and 6 deletions

View file

@ -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)

View file

@ -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'
}
}