mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
perf(core): skip esm for node_modules and non .js files (#5220)
This commit is contained in:
parent
adf423a57f
commit
aabb1f69e7
@ -143,7 +143,7 @@ export default class ModuleContainer {
|
||||
|
||||
// Resolve handler
|
||||
if (!handler) {
|
||||
handler = this.nuxt.resolver.requireModule(src)
|
||||
handler = this.nuxt.resolver.requireModule(src, { useESM: true })
|
||||
}
|
||||
|
||||
// Validate handler
|
||||
@ -152,7 +152,10 @@ export default class ModuleContainer {
|
||||
}
|
||||
|
||||
// Resolve module meta
|
||||
const key = (handler.meta && handler.meta.name) || handler.name || src
|
||||
let key = (handler.meta && handler.meta.name) || handler.name
|
||||
if (!key || key === 'default') {
|
||||
key = src
|
||||
}
|
||||
|
||||
// Update requiredModules
|
||||
if (typeof key === 'string') {
|
||||
|
@ -135,17 +135,17 @@ export default class Resolver {
|
||||
lastError = e
|
||||
}
|
||||
|
||||
// Disable esm for ts files by default
|
||||
if (useESM === undefined && /.ts$/.test(resolvedPath)) {
|
||||
useESM = false
|
||||
// By default use esm only for js,mjs files outside of node_modules
|
||||
if (useESM === undefined) {
|
||||
useESM = /.(js|mjs)$/.test(resolvedPath) && !/node_modules/.test(resolvedPath)
|
||||
}
|
||||
|
||||
// Try to require
|
||||
try {
|
||||
if (useESM === false) {
|
||||
requiredModule = require(resolvedPath)
|
||||
} else {
|
||||
if (useESM) {
|
||||
requiredModule = this.esm(resolvedPath)
|
||||
} else {
|
||||
requiredModule = require(resolvedPath)
|
||||
}
|
||||
} catch (e) {
|
||||
lastError = e
|
||||
|
@ -330,7 +330,7 @@ describe('core: module', () => {
|
||||
const result = await module.addModule('moduleTest')
|
||||
|
||||
expect(requireModule).toBeCalledTimes(1)
|
||||
expect(requireModule).toBeCalledWith('moduleTest')
|
||||
expect(requireModule).toBeCalledWith('moduleTest', { useESM: true })
|
||||
expect(module.requiredModules).toEqual({
|
||||
moduleTest: {
|
||||
handler: expect.any(Function),
|
||||
@ -379,7 +379,7 @@ describe('core: module', () => {
|
||||
const result = await module.addModule(['moduleTest', { test: true }])
|
||||
|
||||
expect(requireModule).toBeCalledTimes(1)
|
||||
expect(requireModule).toBeCalledWith('moduleTest')
|
||||
expect(requireModule).toBeCalledWith('moduleTest', { useESM: true })
|
||||
expect(module.requiredModules).toEqual({
|
||||
moduleTest: {
|
||||
handler: expect.any(Function),
|
||||
|
@ -352,7 +352,7 @@ describe('core: resolver', () => {
|
||||
})
|
||||
fs.existsSync = jest.fn(() => true)
|
||||
|
||||
resolver.resolvePath('/var/nuxt/resolver/file', { module: true })
|
||||
resolver.resolvePath('/var/nuxt/resolver/file.js', { module: true })
|
||||
const warnMsg = 'Using module is deprecated and will be removed in Nuxt 3. Use `isModule` instead.'
|
||||
expect(consola.warn).toBeCalledTimes(1)
|
||||
expect(consola.warn).toBeCalledWith(warnMsg)
|
||||
@ -364,10 +364,10 @@ describe('core: resolver', () => {
|
||||
const resolver = new Resolver({
|
||||
options: {}
|
||||
})
|
||||
resolver.resolvePath = jest.fn()
|
||||
resolver.resolvePath = x => x
|
||||
resolver.esm = jest.fn(() => ({ default: 'resolved module' }))
|
||||
|
||||
const resolvedModule = resolver.requireModule('/var/nuxt/resolver/module')
|
||||
const resolvedModule = resolver.requireModule('/var/nuxt/resolver/module.js')
|
||||
|
||||
expect(resolvedModule).toEqual('resolved module')
|
||||
})
|
||||
@ -376,10 +376,10 @@ describe('core: resolver', () => {
|
||||
const resolver = new Resolver({
|
||||
options: {}
|
||||
})
|
||||
resolver.resolvePath = jest.fn()
|
||||
resolver.resolvePath = x => x
|
||||
resolver.esm = jest.fn(() => 'resolved module')
|
||||
|
||||
const resolvedModule = resolver.requireModule('/var/nuxt/resolver/module')
|
||||
const resolvedModule = resolver.requireModule('/var/nuxt/resolver/module.js')
|
||||
|
||||
expect(resolvedModule).toEqual('resolved module')
|
||||
})
|
||||
@ -388,10 +388,10 @@ describe('core: resolver', () => {
|
||||
const resolver = new Resolver({
|
||||
options: {}
|
||||
})
|
||||
resolver.resolvePath = jest.fn()
|
||||
resolver.resolvePath = x => x
|
||||
resolver.esm = jest.fn(() => ({ default: 'resolved module' }))
|
||||
|
||||
const resolvedModule = resolver.requireModule('/var/nuxt/resolver/module', { interopDefault: false })
|
||||
const resolvedModule = resolver.requireModule('/var/nuxt/resolver/module.js', { interopDefault: false })
|
||||
|
||||
expect(resolvedModule).toEqual({ default: 'resolved module' })
|
||||
})
|
||||
@ -403,7 +403,7 @@ describe('core: resolver', () => {
|
||||
resolver.resolvePath = jest.fn(() => 'path')
|
||||
resolver.esm = jest.fn(() => ({ default: 'resolved module' }))
|
||||
|
||||
const resolvedModule = resolver.requireModule('path', { esm: false })
|
||||
const resolvedModule = resolver.requireModule('path', { useESM: false })
|
||||
|
||||
expect(resolvedModule).toBe(path)
|
||||
})
|
||||
@ -427,7 +427,7 @@ describe('core: resolver', () => {
|
||||
resolver.resolvePath = jest.fn(() => { throw new Error('resolve failed') })
|
||||
resolver.esm = jest.fn(() => undefined)
|
||||
|
||||
expect(() => resolver.requireModule('/var/nuxt/resolver/module')).toThrow('resolve failed')
|
||||
expect(() => resolver.requireModule('/var/nuxt/resolver/module.js')).toThrow('resolve failed')
|
||||
})
|
||||
|
||||
test('should throw last error', () => {
|
||||
@ -437,17 +437,17 @@ describe('core: resolver', () => {
|
||||
resolver.resolvePath = jest.fn(() => { throw new Error('resolve failed') })
|
||||
resolver.esm = jest.fn(() => { throw new Error('resolve esm failed') })
|
||||
|
||||
expect(() => resolver.requireModule('/var/nuxt/resolver/module')).toThrow('resolve esm failed')
|
||||
expect(() => resolver.requireModule('/var/nuxt/resolver/module.js')).toThrow('resolve esm failed')
|
||||
})
|
||||
|
||||
test('should display deprecated alias options', () => {
|
||||
const resolver = new Resolver({
|
||||
options: {}
|
||||
})
|
||||
resolver.resolvePath = jest.fn()
|
||||
resolver.resolvePath = x => x
|
||||
resolver.esm = jest.fn()
|
||||
|
||||
resolver.requireModule('/var/nuxt/resolver/file', { alias: true })
|
||||
resolver.requireModule('/var/nuxt/resolver/file.js', { alias: true })
|
||||
const warnMsg = 'Using alias is deprecated and will be removed in Nuxt 3. Use `isAlias` instead.'
|
||||
expect(consola.warn).toBeCalledTimes(1)
|
||||
expect(consola.warn).toBeCalledWith(warnMsg)
|
||||
@ -460,7 +460,7 @@ describe('core: resolver', () => {
|
||||
resolver.resolvePath = jest.fn()
|
||||
resolver.esm = jest.fn()
|
||||
|
||||
resolver.requireModule('/var/nuxt/resolver/file', { esm: true })
|
||||
resolver.requireModule('/var/nuxt/resolver/file.js', { esm: true })
|
||||
const warnMsg = 'Using esm is deprecated and will be removed in Nuxt 3. Use `useESM` instead.'
|
||||
expect(consola.warn).toBeCalledTimes(1)
|
||||
expect(consola.warn).toBeCalledWith(warnMsg)
|
||||
|
4
test/fixtures/typescript/modules/module.ts
vendored
4
test/fixtures/typescript/modules/module.ts
vendored
@ -1 +1,3 @@
|
||||
export default () => {}
|
||||
export default function testTSModule() {
|
||||
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ describe('typescript', () => {
|
||||
})
|
||||
|
||||
test('TS module successfully required', () => {
|
||||
expect(nuxt.moduleContainer.requiredModules).toHaveProperty('~/modules/module')
|
||||
expect(nuxt.moduleContainer.requiredModules.testTSModule).toBeDefined()
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
|
Loading…
Reference in New Issue
Block a user