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