fix(nuxt): try to resolve node_modules before alias logic (#3505)

Fixes issues with @<organization>/<package> for plugins and more.
This commit is contained in:
Blake Kostner 2018-07-06 07:27:21 -07:00 committed by Pooya Parsa
parent 78494dac92
commit 842cc19431

View File

@ -181,6 +181,14 @@ module.exports = class Nuxt {
} }
resolveAlias(path) { resolveAlias(path) {
const modulePath = this.resolveModule(path)
// Try to resolve it as if it were a regular node_module
// Package first. Fixes issue with @<org> scoped packages
if (modulePath != null) {
return modulePath
}
if (path.indexOf('@@') === 0 || path.indexOf('~~') === 0) { if (path.indexOf('@@') === 0 || path.indexOf('~~') === 0) {
return join(this.options.rootDir, path.substr(2)) return join(this.options.rootDir, path.substr(2))
} }
@ -192,22 +200,31 @@ module.exports = class Nuxt {
return resolve(this.options.srcDir, path) return resolve(this.options.srcDir, path)
} }
resolvePath(path) { resolveModule(path) {
// Try to resolve using NPM resolve path first
try { try {
const resolvedPath = Module._resolveFilename(path, { const resolvedPath = Module._resolveFilename(path, {
paths: this.options.modulesDir paths: this.options.modulesDir
}) })
return resolvedPath return resolvedPath
} catch (error) { } catch (error) {
if (error.code === 'MODULE_NOT_FOUND') { if (error.code === 'MODULE_NOT_FOUND') {
// Continue to try other methods return null
} else { } else {
throw error throw error
} }
} }
}
let _path = this.resolveAlias(path) resolvePath(path) {
const modulePath = this.resolveModule(path)
// Try to resolve path as a node_module package first
if (modulePath != null) {
return modulePath
}
const _path = this.resolveAlias(path)
if (existsSync(_path)) { if (existsSync(_path)) {
return _path return _path