Skip to content

Commit

Permalink
chore: optimize esm flag (#5778)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 authored Nov 21, 2021
1 parent 5588eb9 commit a32b105
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,14 +775,14 @@ export async function loadConfigFromFile(

let resolvedPath: string | undefined
let isTS = false
let isMjs = false
let isESM = false
let dependencies: string[] = []

// check package.json for type: "module" and set `isMjs` to true
try {
const pkg = lookupFile(configRoot, ['package.json'])
if (pkg && JSON.parse(pkg).type === 'module') {
isMjs = true
isESM = true
}
} catch (e) {}

Expand All @@ -792,7 +792,7 @@ export async function loadConfigFromFile(
isTS = configFile.endsWith('.ts')

if (configFile.endsWith('.mjs')) {
isMjs = true
isESM = true
}
} else {
// implicit config file loaded from inline root (if present)
Expand All @@ -806,7 +806,7 @@ export async function loadConfigFromFile(
const mjsconfigFile = path.resolve(configRoot, 'vite.config.mjs')
if (fs.existsSync(mjsconfigFile)) {
resolvedPath = mjsconfigFile
isMjs = true
isESM = true
}
}

Expand All @@ -827,7 +827,7 @@ export async function loadConfigFromFile(
try {
let userConfig: UserConfigExport | undefined

if (isMjs) {
if (isESM) {
const fileUrl = require('url').pathToFileURL(resolvedPath)
if (isTS) {
// before we can register loaders without requiring users to run node
Expand All @@ -850,7 +850,7 @@ export async function loadConfigFromFile(
}
}

if (!isTS && !isMjs) {
if (!isTS && !isESM) {
// 1. try to directly require the module (assuming commonjs)
try {
// clear cache in case of server restart
Expand Down Expand Up @@ -906,7 +906,7 @@ export async function loadConfigFromFile(

async function bundleConfigFile(
fileName: string,
mjs = false
isESM = false
): Promise<{ code: string; dependencies: string[] }> {
const result = await build({
absWorkingDir: process.cwd(),
Expand All @@ -915,7 +915,7 @@ async function bundleConfigFile(
write: false,
platform: 'node',
bundle: true,
format: mjs ? 'esm' : 'cjs',
format: isESM ? 'esm' : 'cjs',
sourcemap: 'inline',
metafile: true,
plugins: [
Expand Down

0 comments on commit a32b105

Please sign in to comment.