Skip to content

Commit

Permalink
chore: add back and update warning
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed May 5, 2022
1 parent 8ee36ed commit 6d928a3
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion packages/vite/src/node/ssr/ssrExternal.ts
@@ -1,7 +1,14 @@
import fs from 'fs'
import path from 'path'
import type { InternalResolveOptions } from '../plugins/resolve'
import { tryNodeResolve } from '../plugins/resolve'
import { createDebugger, isDefined, lookupFile, resolveFrom } from '../utils'
import {
createDebugger,
isDefined,
lookupFile,
normalizePath,
resolveFrom
} from '../utils'
import type { Logger, ResolvedConfig } from '..'
import { createFilter } from '@rollup/pluginutils'

Expand Down Expand Up @@ -105,6 +112,7 @@ function collectExternals(
seen.add(id)

let esmEntry: string | undefined
let requireEntry: string
try {
esmEntry = tryNodeResolve(
id,
Expand All @@ -114,6 +122,9 @@ function collectExternals(
undefined,
true
)?.id
// normalizePath required for windows. tryNodeResolve uses normalizePath
// which returns with '/', require.resolve returns with '\\'
requireEntry = normalizePath(require.resolve(id, { paths: [root] }))
} catch (e) {
try {
// no main entry, but deep imports may be allowed
Expand All @@ -130,6 +141,29 @@ function collectExternals(
debug(`Failed to resolve entries for package "${id}"\n`, e)
continue
}

const pkgPath = resolveFrom(`${id}/package.json`, root)
const pkgContent = fs.readFileSync(pkgPath, 'utf-8')

if (!pkgContent) {
continue
}
const pkg = JSON.parse(pkgContent)

if (pkg.type === 'module') {
if (requireEntry && !requireEntry.endsWith('.cjs')) {
logger.warn(
`${id} must use a .cjs extension for the CJS entry point when "type": "module" is set. Please contact the package author to fix.`
)
}
} else {
if (esmEntry && !esmEntry.endsWith('.mjs')) {
logger.warn(
`${id} must either set "type": "module" or use an .mjs extension for the ESM entry point. Please contact the package author to fix.`
)
}
}

if (esmEntry && !esmEntry.includes('node_modules')) {
// trace the dependencies of linked packages
const pkgPath = resolveFrom(`${id}/package.json`, root)
Expand Down

0 comments on commit 6d928a3

Please sign in to comment.