2018-10-17 21:28:25 +00:00
|
|
|
import { resolve, join } from 'path'
|
|
|
|
import fs from 'fs-extra'
|
2019-01-06 08:02:28 +00:00
|
|
|
import consola from 'consola'
|
2020-12-22 17:07:50 +00:00
|
|
|
import createRequire from 'create-require'
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2019-10-01 11:04:24 +00:00
|
|
|
import {
|
|
|
|
startsWithRootAlias,
|
|
|
|
startsWithSrcAlias,
|
|
|
|
isExternalDependency,
|
|
|
|
clearRequireCache
|
|
|
|
} from '@nuxt/utils'
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
export default class Resolver {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (nuxt) {
|
2018-10-17 21:28:25 +00:00
|
|
|
this.nuxt = nuxt
|
|
|
|
this.options = this.nuxt.options
|
|
|
|
|
|
|
|
// Binds
|
|
|
|
this.resolvePath = this.resolvePath.bind(this)
|
|
|
|
this.resolveAlias = this.resolveAlias.bind(this)
|
|
|
|
this.resolveModule = this.resolveModule.bind(this)
|
|
|
|
this.requireModule = this.requireModule.bind(this)
|
|
|
|
|
2020-12-22 17:07:50 +00:00
|
|
|
this._createRequire = this.options.createRequire || createRequire
|
|
|
|
this._require = this._createRequire(__filename)
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 23:07:58 +00:00
|
|
|
resolveModule (path, { paths } = {}) {
|
2018-10-17 21:28:25 +00:00
|
|
|
try {
|
2020-12-22 17:07:50 +00:00
|
|
|
return this._require.resolve(path, {
|
2020-12-22 23:29:50 +00:00
|
|
|
paths: [].concat(paths || [], this.options.modulesDir, global.__NUXT_PATHS__ || [])
|
2018-10-17 21:28:25 +00:00
|
|
|
})
|
|
|
|
} catch (error) {
|
2019-05-25 19:54:28 +00:00
|
|
|
if (error.code !== 'MODULE_NOT_FOUND') {
|
2018-10-17 21:28:25 +00:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
resolveAlias (path) {
|
2018-10-17 21:28:25 +00:00
|
|
|
if (startsWithRootAlias(path)) {
|
|
|
|
return join(this.options.rootDir, path.substr(2))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startsWithSrcAlias(path)) {
|
|
|
|
return join(this.options.srcDir, path.substr(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(this.options.srcDir, path)
|
|
|
|
}
|
|
|
|
|
2020-12-22 23:07:58 +00:00
|
|
|
resolvePath (path, { alias, isAlias = alias, module, isModule = module, isStyle, paths } = {}) {
|
2019-01-06 08:02:28 +00:00
|
|
|
// TODO: Remove in Nuxt 3
|
|
|
|
if (alias) {
|
|
|
|
consola.warn('Using alias is deprecated and will be removed in Nuxt 3. Use `isAlias` instead.')
|
|
|
|
}
|
|
|
|
if (module) {
|
|
|
|
consola.warn('Using module is deprecated and will be removed in Nuxt 3. Use `isModule` instead.')
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
// Fast return in case of path exists
|
|
|
|
if (fs.existsSync(path)) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
let resolvedPath
|
|
|
|
|
|
|
|
// Try to resolve it as a regular module
|
2019-01-06 08:02:28 +00:00
|
|
|
if (isModule !== false) {
|
2020-12-22 23:07:58 +00:00
|
|
|
resolvedPath = this.resolveModule(path, { paths })
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to resolve alias
|
2019-01-06 08:02:28 +00:00
|
|
|
if (!resolvedPath && isAlias !== false) {
|
2018-10-17 21:28:25 +00:00
|
|
|
resolvedPath = this.resolveAlias(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use path for resolvedPath
|
|
|
|
if (!resolvedPath) {
|
|
|
|
resolvedPath = path
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:10:39 +00:00
|
|
|
let isDirectory
|
|
|
|
|
2018-12-13 18:39:42 +00:00
|
|
|
// Check if resolvedPath exits and is not a directory
|
2018-12-17 15:10:39 +00:00
|
|
|
if (fs.existsSync(resolvedPath)) {
|
|
|
|
isDirectory = fs.lstatSync(resolvedPath).isDirectory()
|
|
|
|
|
|
|
|
if (!isDirectory) {
|
|
|
|
return resolvedPath
|
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 21:30:19 +00:00
|
|
|
const extensions = isStyle ? this.options.styleExtensions : this.options.extensions
|
|
|
|
|
2018-12-13 18:39:42 +00:00
|
|
|
// Check if any resolvedPath.[ext] or resolvedPath/index.[ext] exists
|
2019-01-02 21:30:19 +00:00
|
|
|
for (const ext of extensions) {
|
2018-12-17 15:10:39 +00:00
|
|
|
if (!isDirectory && fs.existsSync(resolvedPath + '.' + ext)) {
|
2018-10-17 21:28:25 +00:00
|
|
|
return resolvedPath + '.' + ext
|
|
|
|
}
|
2018-12-13 18:39:42 +00:00
|
|
|
|
2020-03-27 12:56:40 +00:00
|
|
|
const resolvedPathwithIndex = join(resolvedPath, 'index.' + ext)
|
|
|
|
if (isDirectory && fs.existsSync(resolvedPathwithIndex)) {
|
|
|
|
return resolvedPathwithIndex
|
2018-12-13 18:39:42 +00:00
|
|
|
}
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 15:10:39 +00:00
|
|
|
// If there's no index.[ext] we just return the directory path
|
|
|
|
if (isDirectory) {
|
|
|
|
return resolvedPath
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
// Give up
|
|
|
|
throw new Error(`Cannot resolve "${path}" from "${resolvedPath}"`)
|
|
|
|
}
|
|
|
|
|
2020-12-22 23:07:58 +00:00
|
|
|
requireModule (path, { alias, isAlias = alias, intropDefault, interopDefault = intropDefault, paths } = {}) {
|
2018-10-17 21:28:25 +00:00
|
|
|
let resolvedPath = path
|
|
|
|
let requiredModule
|
2019-01-06 08:02:28 +00:00
|
|
|
|
|
|
|
// TODO: Remove in Nuxt 3
|
2019-02-01 13:08:47 +00:00
|
|
|
if (intropDefault) {
|
|
|
|
consola.warn('Using intropDefault is deprecated and will be removed in Nuxt 3. Use `interopDefault` instead.')
|
|
|
|
}
|
2019-01-06 08:02:28 +00:00
|
|
|
if (alias) {
|
|
|
|
consola.warn('Using alias is deprecated and will be removed in Nuxt 3. Use `isAlias` instead.')
|
|
|
|
}
|
|
|
|
|
2019-01-04 19:30:28 +00:00
|
|
|
let lastError
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
// Try to resolve path
|
|
|
|
try {
|
2020-12-22 23:07:58 +00:00
|
|
|
resolvedPath = this.resolvePath(path, { isAlias, paths })
|
2018-10-17 21:28:25 +00:00
|
|
|
} catch (e) {
|
2019-01-04 19:30:28 +00:00
|
|
|
lastError = e
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:04:24 +00:00
|
|
|
const isExternal = isExternalDependency(resolvedPath)
|
|
|
|
|
|
|
|
// in dev mode make sure to clear the require cache so after
|
|
|
|
// a dev server restart any changed file is reloaded
|
|
|
|
if (this.options.dev && !isExternal) {
|
|
|
|
clearRequireCache(resolvedPath)
|
|
|
|
}
|
|
|
|
|
2018-10-17 21:28:25 +00:00
|
|
|
// Try to require
|
|
|
|
try {
|
2020-12-22 23:07:58 +00:00
|
|
|
requiredModule = this._require(resolvedPath)
|
2018-10-17 21:28:25 +00:00
|
|
|
} catch (e) {
|
2019-01-04 19:30:28 +00:00
|
|
|
lastError = e
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 13:08:47 +00:00
|
|
|
// Interop default
|
|
|
|
if (interopDefault !== false && requiredModule && requiredModule.default) {
|
2018-10-17 21:28:25 +00:00
|
|
|
requiredModule = requiredModule.default
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throw error if failed to require
|
2019-01-04 19:30:28 +00:00
|
|
|
if (requiredModule === undefined && lastError) {
|
|
|
|
throw lastError
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return requiredModule
|
|
|
|
}
|
|
|
|
}
|