Skip to content

Commit 3b6d5b2

Browse files
author
Guillaume Chau
committed
fix: vuejs#1396 require.resolve custom paths on node < 8.10.0
1 parent 4f39461 commit 3b6d5b2

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

packages/@vue/cli/lib/util/module.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1+
const { minNode } = require('./nodeVersion')
2+
3+
function resolveFallback (request, options) {
4+
const Module = require('module')
5+
const isMain = false
6+
const fakeParent = new Module('', null)
7+
8+
const paths = []
9+
10+
for (var i = 0; i < options.paths.length; i++) {
11+
const path = options.paths[i]
12+
fakeParent.paths = Module._nodeModulePaths(path)
13+
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true)
14+
15+
if (!paths.includes(path)) paths.push(path)
16+
17+
for (var j = 0; j < lookupPaths.length; j++) {
18+
if (!paths.includes(lookupPaths[j])) paths.push(lookupPaths[j])
19+
}
20+
}
21+
22+
var filename = Module._findPath(request, paths, isMain)
23+
if (!filename) {
24+
var err = new Error(`Cannot find module '${request}'`)
25+
err.code = 'MODULE_NOT_FOUND'
26+
throw err
27+
}
28+
return filename
29+
}
30+
31+
const resolve = minNode(8, 10) ? require.resolve : resolveFallback
32+
133
exports.resolveModule = function (request, context) {
234
let resolvedPath
335
try {
4-
resolvedPath = require.resolve(request, {
36+
resolvedPath = resolve(request, {
537
paths: [context]
638
})
739
} catch (e) {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.minNode = function (major, minor = 0, patch = 0) {
2+
const parts = process.versions.node.split('.').map(v => parseInt(v))
3+
if (parts[0] >= major && parts[1] >= minor && parts[2] >= patch) return true
4+
return false
5+
}

0 commit comments

Comments
 (0)