Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { minNode } = require('./nodeVersion')

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

const paths = []

for (var 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 (var j = 0; j < lookupPaths.length; j++) {
if (!paths.includes(lookupPaths[j])) paths.push(lookupPaths[j])
}
}

var filename = Module._findPath(request, paths, isMain)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this var while others are const?

if (!filename) {
var err = new Error(`Cannot find module '${request}'`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

err.code = 'MODULE_NOT_FOUND'
throw err
}
return filename
}

const resolve = minNode(8, 10) ? require.resolve : resolveFallback

exports.resolveModule = function (request, context) {
let resolvedPath
try {
resolvedPath = require.resolve(request, {
resolvedPath = resolve(request, {
paths: [context]
})
} catch (e) {}
Expand Down
5 changes: 5 additions & 0 deletions packages/@vue/cli/lib/util/nodeVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.minNode = function (major, minor = 0, patch = 0) {
const parts = process.versions.node.split('.').map(v => parseInt(v))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use destructuring and give part names like const [major,minor,patch] = ...

i’m aware these are already taken, just suggesting.

if (parts[0] >= major && parts[1] >= minor && parts[2] >= patch) return true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could return whole expression

return false
}