Skip to content

Commit

Permalink
Add more entry and package cache guards to env modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Jan 15, 2019
1 parent b77cf1a commit 8e8c354
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
34 changes: 29 additions & 5 deletions src/env/has-loader-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { extname, sep } from "../safe/path.js"

import CHAR_CODE from "../constant/char-code.js"

import Loader from "../loader.js"
import Module from "../module.js"

import esmResolveFilename from "../module/esm/resolve-filename.js"
Expand Down Expand Up @@ -48,15 +49,38 @@ function init() {
}

function tryResolveFilename(request, parent, isMain) {
try {
return esmResolveFilename(request, parent, isMain)
} catch {}
const entryState = shared.entry
const entryCache = entryState.cache

const pkgState = Loader.state.package
const pkgCache = pkgState.cache

entryState.cache = new WeakMap
pkgState.cache = new Map

let result
let threw = true

try {
return Module._resolveFilename(request, parent, isMain)
result = esmResolveFilename(request, parent, isMain)
threw = false
} catch {}

return ""
if (threw) {
try {
result = Module._resolveFilename(request, parent, isMain)
threw = false
} catch {}
}

entryState.cache = entryCache
pkgState.cache = pkgCache

if (threw) {
return ""
}

return result
}

return hasLoaderValue
Expand Down
21 changes: 11 additions & 10 deletions src/env/is-sideloaded.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ function init() {

const args = argv.slice(2)

let filename
let nodeModulesIndex = -1
if (args.length === 0) {
return false
}

const filename = realpath(argv[1])
const nodeModulesIndex = normalize(filename).lastIndexOf("/node_modules/")

if (args.length) {
filename = realpath(argv[1])
nodeModulesIndex = normalize(filename).lastIndexOf("/node_modules/")
if (nodeModulesIndex === -1 ||
! hasLoaderArg(args)) {
return false
}

const entryState = shared.entry
Expand All @@ -36,11 +40,8 @@ function init() {

let result = false

// From a package like Mocha.
if (nodeModulesIndex !== -1 &&
hasLoaderArg(args) &&
(Package.get(cwd()) !== null ||
Package.get(filename.slice(0, nodeModulesIndex + 1)) !== null)) {
if (Package.get(cwd()) !== null ||
Package.get(filename.slice(0, nodeModulesIndex + 1)) !== null) {
result = true
}

Expand Down

0 comments on commit 8e8c354

Please sign in to comment.