mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
refactor(core): use require.resolve
instead of Module
internals (#5796)
This commit is contained in:
parent
7e50fe744f
commit
5f72ad5358
@ -1,4 +1,3 @@
|
||||
import Module from 'module'
|
||||
import { resolve, join } from 'path'
|
||||
import fs from 'fs-extra'
|
||||
import consola from 'consola'
|
||||
@ -19,17 +18,21 @@ export default class Resolver {
|
||||
|
||||
// ESM Loader
|
||||
this.esm = esm(module)
|
||||
|
||||
this._resolve = require.resolve
|
||||
}
|
||||
|
||||
resolveModule(path) {
|
||||
try {
|
||||
return Module._resolveFilename(path, {
|
||||
return this._resolve(path, {
|
||||
paths: this.options.modulesDir
|
||||
})
|
||||
} catch (error) {
|
||||
if (error.code === 'MODULE_NOT_FOUND') {
|
||||
return undefined
|
||||
} else {
|
||||
if (error.code !== 'MODULE_NOT_FOUND') {
|
||||
// TODO: remove after https://github.com/facebook/jest/pull/8487 released
|
||||
if (process.env.NODE_ENV === 'test' && error.message.startsWith('Cannot resolve module')) {
|
||||
return
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
import Module from 'module'
|
||||
import path from 'path'
|
||||
import esm from 'esm'
|
||||
import fs from 'fs-extra'
|
||||
@ -7,7 +6,6 @@ import { startsWithRootAlias, startsWithSrcAlias } from '@nuxt/utils'
|
||||
|
||||
import Resolver from '../src/resolver'
|
||||
|
||||
jest.mock('module')
|
||||
jest.mock('path')
|
||||
jest.mock('esm', () => jest.fn(() => jest.fn()))
|
||||
jest.mock('fs-extra')
|
||||
@ -33,24 +31,24 @@ describe('core: resolver', () => {
|
||||
expect(esm).toBeCalledTimes(1)
|
||||
})
|
||||
|
||||
test('should call _resolveFilename in resolveModule', () => {
|
||||
test('should call require.resolve in resolveModule', () => {
|
||||
const resolver = new Resolver({
|
||||
options: { modulesDir: '/var/nuxt/node_modules' }
|
||||
})
|
||||
Module._resolveFilename = jest.fn(() => '/var/nuxt/resolver/module')
|
||||
const resolve = resolver._resolve = jest.fn(() => '/var/nuxt/resolver/module')
|
||||
|
||||
const modulePath = resolver.resolveModule('/var/nuxt/resolver')
|
||||
|
||||
expect(modulePath).toEqual('/var/nuxt/resolver/module')
|
||||
expect(Module._resolveFilename).toBeCalledTimes(1)
|
||||
expect(Module._resolveFilename).toBeCalledWith('/var/nuxt/resolver', { paths: '/var/nuxt/node_modules' })
|
||||
expect(resolve).toBeCalledTimes(1)
|
||||
expect(resolve).toBeCalledWith('/var/nuxt/resolver', { paths: '/var/nuxt/node_modules' })
|
||||
})
|
||||
|
||||
test('should return undefined when module is not found', () => {
|
||||
const resolver = new Resolver({
|
||||
options: { modulesDir: '/var/nuxt/node_modules' }
|
||||
})
|
||||
Module._resolveFilename = jest.fn(() => {
|
||||
const resolve = resolver._resolve = jest.fn(() => {
|
||||
const err = new Error()
|
||||
err.code = 'MODULE_NOT_FOUND'
|
||||
throw err
|
||||
@ -59,16 +57,14 @@ describe('core: resolver', () => {
|
||||
const modulePath = resolver.resolveModule('/var/nuxt/resolver')
|
||||
|
||||
expect(modulePath).toBeUndefined()
|
||||
expect(Module._resolveFilename).toBeCalledTimes(1)
|
||||
expect(resolve).toBeCalledTimes(1)
|
||||
})
|
||||
|
||||
test('should throw error when _resolveFilename failed', () => {
|
||||
test('should throw error when require.resolve failed', () => {
|
||||
const resolver = new Resolver({
|
||||
options: { modulesDir: '/var/nuxt/node_modules' }
|
||||
})
|
||||
Module._resolveFilename = jest.fn(() => {
|
||||
throw new Error('resolve failed')
|
||||
})
|
||||
resolver._resolve = jest.fn(() => { throw new Error('resolve failed') })
|
||||
|
||||
expect(() => resolver.resolveModule('/var/nuxt/resolver')).toThrow('resolve failed')
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user