mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
refactor: rename requirePath to path
more constency with require.resolve(id, { paths })
This commit is contained in:
parent
ed6e8cd307
commit
7b59b21511
@ -121,13 +121,13 @@ export default class ModuleContainer {
|
||||
)
|
||||
}
|
||||
|
||||
requireModule (moduleOpts, { requirePath } = {}) {
|
||||
return this.addModule(moduleOpts, undefined, { requirePath })
|
||||
requireModule (moduleOpts, { paths } = {}) {
|
||||
return this.addModule(moduleOpts, undefined, { paths })
|
||||
}
|
||||
|
||||
async addModule (moduleOpts, arg2, arg3) {
|
||||
// Arg2 was previously used for requireOnce which is ignored now
|
||||
const { requirePath } = { ...arg2, ...arg3 }
|
||||
const { paths } = { ...arg2, ...arg3 }
|
||||
let src
|
||||
let options
|
||||
let handler
|
||||
@ -156,7 +156,7 @@ export default class ModuleContainer {
|
||||
// Resolve handler
|
||||
if (!handler) {
|
||||
try {
|
||||
handler = this.nuxt.resolver.requireModule(src, { requirePath })
|
||||
handler = this.nuxt.resolver.requireModule(src, { paths })
|
||||
} catch (error) {
|
||||
if (error.code !== 'MODULE_NOT_FOUND') {
|
||||
throw error
|
||||
|
@ -25,13 +25,10 @@ export default class Resolver {
|
||||
this._require = this._createRequire(__filename)
|
||||
}
|
||||
|
||||
resolveModule (path, { requirePath } = {}) {
|
||||
resolveModule (path, { paths } = {}) {
|
||||
try {
|
||||
return this._require.resolve(path, {
|
||||
paths: [
|
||||
requirePath || __dirname,
|
||||
...[].concat(this.options.modulesDir)
|
||||
]
|
||||
paths: [].concat(paths || [], this.options.modulesDir)
|
||||
})
|
||||
} catch (error) {
|
||||
if (error.code !== 'MODULE_NOT_FOUND') {
|
||||
@ -52,7 +49,7 @@ export default class Resolver {
|
||||
return resolve(this.options.srcDir, path)
|
||||
}
|
||||
|
||||
resolvePath (path, { alias, isAlias = alias, module, isModule = module, isStyle, requirePath } = {}) {
|
||||
resolvePath (path, { alias, isAlias = alias, module, isModule = module, isStyle, paths } = {}) {
|
||||
// TODO: Remove in Nuxt 3
|
||||
if (alias) {
|
||||
consola.warn('Using alias is deprecated and will be removed in Nuxt 3. Use `isAlias` instead.')
|
||||
@ -70,7 +67,7 @@ export default class Resolver {
|
||||
|
||||
// Try to resolve it as a regular module
|
||||
if (isModule !== false) {
|
||||
resolvedPath = this.resolveModule(path, { requirePath })
|
||||
resolvedPath = this.resolveModule(path, { paths })
|
||||
}
|
||||
|
||||
// Try to resolve alias
|
||||
@ -117,7 +114,7 @@ export default class Resolver {
|
||||
throw new Error(`Cannot resolve "${path}" from "${resolvedPath}"`)
|
||||
}
|
||||
|
||||
requireModule (path, { alias, isAlias = alias, intropDefault, interopDefault = intropDefault, requirePath } = {}) {
|
||||
requireModule (path, { alias, isAlias = alias, intropDefault, interopDefault = intropDefault, paths } = {}) {
|
||||
let resolvedPath = path
|
||||
let requiredModule
|
||||
|
||||
@ -133,7 +130,7 @@ export default class Resolver {
|
||||
|
||||
// Try to resolve path
|
||||
try {
|
||||
resolvedPath = this.resolvePath(path, { isAlias, requirePath })
|
||||
resolvedPath = this.resolvePath(path, { isAlias, paths })
|
||||
} catch (e) {
|
||||
lastError = e
|
||||
}
|
||||
@ -148,8 +145,7 @@ export default class Resolver {
|
||||
|
||||
// Try to require
|
||||
try {
|
||||
const _require = requirePath ? this._createRequire(requirePath) : this._require
|
||||
requiredModule = _require(resolvedPath)
|
||||
requiredModule = this._require(resolvedPath)
|
||||
} catch (e) {
|
||||
lastError = e
|
||||
}
|
||||
|
@ -318,7 +318,7 @@ describe('core: module', () => {
|
||||
module.requireModule(moduleOpts)
|
||||
|
||||
expect(module.addModule).toBeCalledTimes(1)
|
||||
expect(module.addModule).toBeCalledWith(moduleOpts, undefined, { requirePath: undefined })
|
||||
expect(module.addModule).toBeCalledWith(moduleOpts, undefined, { paths: undefined })
|
||||
})
|
||||
|
||||
test('should add string module', async () => {
|
||||
@ -332,7 +332,7 @@ describe('core: module', () => {
|
||||
const result = await module.addModule('moduleTest')
|
||||
|
||||
expect(requireModule).toBeCalledTimes(1)
|
||||
expect(requireModule).toBeCalledWith('moduleTest', { requirePath: undefined })
|
||||
expect(requireModule).toBeCalledWith('moduleTest', { paths: undefined })
|
||||
expect(module.requiredModules).toEqual({
|
||||
moduleTest: {
|
||||
handler: expect.any(Function),
|
||||
@ -381,7 +381,7 @@ describe('core: module', () => {
|
||||
const result = await module.addModule(['moduleTest', { test: true }])
|
||||
|
||||
expect(requireModule).toBeCalledTimes(1)
|
||||
expect(requireModule).toBeCalledWith('moduleTest', { requirePath: undefined })
|
||||
expect(requireModule).toBeCalledWith('moduleTest', { paths: undefined })
|
||||
expect(module.requiredModules).toEqual({
|
||||
moduleTest: {
|
||||
handler: expect.any(Function),
|
||||
|
Loading…
Reference in New Issue
Block a user