Skip to content

Commit

Permalink
fix: fix support for non-js module imports
Browse files Browse the repository at this point in the history
fix #132
  • Loading branch information
yyx990803 committed May 14, 2020
1 parent eb15db3 commit d7fb6a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/node/depOptimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import { createHash } from 'crypto'
import { ResolvedConfig } from './config'
import type Rollup from 'rollup'
import { createResolver, resolveNodeModule } from './resolver'
import { createResolver, resolveNodeModule, supportedExts } from './resolver'
import { createBaseRollupPlugins } from './build'
import { resolveFrom, lookupFile } from './utils'
import { init, parse } from 'es-module-lexer'
Expand Down Expand Up @@ -75,6 +75,9 @@ export async function optimizeDeps(config: OptimizeOptions, asCommand = false) {
if (!entry) {
return false
}
if (!supportedExts.includes(path.extname(entry))) {
return false
}
const content = fs.readFileSync(resolveFrom(root, entry), 'utf-8')
const [imports, exports] = parse(content)
if (!exports.length) {
Expand Down
49 changes: 33 additions & 16 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function resolveBareModule(root: string, id: string) {
if (optimized) {
return id
}
const nodeEntry = resolveNodeModule(root, id)
const nodeEntry = resolveNodeModuleEntry(root, id)
if (nodeEntry) {
return nodeEntry
}
Expand All @@ -145,17 +145,13 @@ export function resolveOptimizedModule(
}
}

const nodeModulesMap = new Map()
const nodeModulesEntryMap = new Map()

export function resolveNodeModule(
root: string,
id: string
): string | undefined {
const cached = nodeModulesMap.get(id)
export function resolveNodeModuleEntry(root: string, id: string) {
const cached = nodeModulesEntryMap.get(id)
if (cached) {
return cached
}

let pkgPath
try {
// see if the id is a valid package name
Expand All @@ -167,21 +163,42 @@ export function resolveNodeModule(
const pkg = require(pkgPath)
const entryPoint = id + '/' + (pkg.module || pkg.main || 'index.js')
debug(`(node_module entry) ${id} -> ${entryPoint}`)
nodeModulesMap.set(id, entryPoint)
nodeModulesEntryMap.set(id, entryPoint)
return entryPoint
} else {
// possibly a deep import
try {
return resolveFrom(root, id)
} catch (e) {}
}
}

const nodeModulesMap = new Map()

export function resolveNodeModule(
root: string,
id: string
): string | undefined {
const cached = nodeModulesMap.get(id)
if (cached) {
return cached
}

// no match and no ext, try all exts
let resolved
// possibly a deep import
try {
resolved = resolveFrom(root, id)
} catch (e) {}

// no match and no ext, try all exts
if (!resolved) {
if (!path.extname(id)) {
for (const ext of supportedExts) {
try {
return resolveFrom(root, id + ext)
resolved = resolveFrom(root, id + ext)
} catch (e) {}
if (resolved) {
break
}
}
}
}

nodeModulesMap.set(id, resolved)
return resolved
}
6 changes: 5 additions & 1 deletion src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@ export const resolveImport = (
if (bareImportRE.test(id)) {
// directly resolve bare module names to its entry path so that relative
// imports from it (including source map urls) can work correctly
return `/@modules/${resolveBareModule(root, id)}`
let resolvedModulePath = resolveBareModule(root, id)
if (!jsSrcRE.test(resolvedModulePath)) {
resolvedModulePath += `?import`
}
return `/@modules/${resolvedModulePath}`
} else {
let { pathname, query } = resolveRelativeRequest(importer, id)
// append an extension to extension-less imports
Expand Down

0 comments on commit d7fb6a9

Please sign in to comment.