Skip to content

Commit

Permalink
fix: require.resolve fallback on node < 8.10.0 (#1404)
Browse files Browse the repository at this point in the history
close #1369
  • Loading branch information
Akryum authored and yyx990803 committed May 30, 2018
1 parent 4f39461 commit ef2ecf5
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/@vue/cli/lib/util/module.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
const semver = require('semver')

function resolveFallback (request, options) {
const Module = require('module')
const isMain = false
const fakeParent = new Module('', null)

const paths = []

for (let i = 0; i < options.paths.length; i++) {
const path = options.paths[i]
fakeParent.paths = Module._nodeModulePaths(path)
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true)

if (!paths.includes(path)) paths.push(path)

for (let j = 0; j < lookupPaths.length; j++) {
if (!paths.includes(lookupPaths[j])) paths.push(lookupPaths[j])
}
}

const filename = Module._findPath(request, paths, isMain)
if (!filename) {
const err = new Error(`Cannot find module '${request}'`)
err.code = 'MODULE_NOT_FOUND'
throw err
}
return filename
}

const resolve = semver.satisfies(process.version, '>=8.10.0') ? require.resolve : resolveFallback

exports.resolveModule = function (request, context) {
let resolvedPath
try {
resolvedPath = require.resolve(request, {
resolvedPath = resolve(request, {
paths: [context]
})
} catch (e) {}
Expand Down

0 comments on commit ef2ecf5

Please sign in to comment.