From 7b59b21511a821235faaec0a777a1d33d54e8d53 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 23 Dec 2020 00:07:58 +0100 Subject: [PATCH] refactor: rename requirePath to path more constency with require.resolve(id, { paths }) --- packages/core/src/module.js | 8 ++++---- packages/core/src/resolver.js | 18 +++++++----------- packages/core/test/module.test.js | 6 +++--- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/core/src/module.js b/packages/core/src/module.js index 206b0844df..466db6af5c 100644 --- a/packages/core/src/module.js +++ b/packages/core/src/module.js @@ -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 diff --git a/packages/core/src/resolver.js b/packages/core/src/resolver.js index c0ba676a66..b3c7ebcb70 100644 --- a/packages/core/src/resolver.js +++ b/packages/core/src/resolver.js @@ -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 } diff --git a/packages/core/test/module.test.js b/packages/core/test/module.test.js index 290c0f95fc..2efa2e9dfa 100644 --- a/packages/core/test/module.test.js +++ b/packages/core/test/module.test.js @@ -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),