mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-12 11:48:17 +00:00
fix: prevent ts-node
to register twice (#5699)
This commit is contained in:
parent
732be3288e
commit
0c7e500699
@ -2,7 +2,14 @@ import path from 'path'
|
|||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
import * as imports from '../imports'
|
import * as imports from '../imports'
|
||||||
|
|
||||||
|
let _guard = false
|
||||||
|
export const setGuard = (val) => { _guard = val }
|
||||||
|
|
||||||
async function registerTSNode({ tsConfigPath, options }) {
|
async function registerTSNode({ tsConfigPath, options }) {
|
||||||
|
if (_guard) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const { register } = await imports.tsNode()
|
const { register } = await imports.tsNode()
|
||||||
|
|
||||||
// https://github.com/TypeStrong/ts-node
|
// https://github.com/TypeStrong/ts-node
|
||||||
@ -13,6 +20,8 @@ async function registerTSNode({ tsConfigPath, options }) {
|
|||||||
},
|
},
|
||||||
...options
|
...options
|
||||||
})
|
})
|
||||||
|
|
||||||
|
_guard = true
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNuxtTypeScript() {
|
async function getNuxtTypeScript() {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import { mkdirp, writeFile, remove } from 'fs-extra'
|
import { mkdirp, writeFile, remove } from 'fs-extra'
|
||||||
import { register } from 'ts-node'
|
import { register } from 'ts-node'
|
||||||
import { detectTypeScript } from '../../src/utils/typescript'
|
import { detectTypeScript, setGuard } from '../../src/utils/typescript'
|
||||||
|
|
||||||
jest.mock('ts-node')
|
jest.mock('ts-node')
|
||||||
|
|
||||||
@ -16,8 +16,12 @@ describe('Typescript Support', () => {
|
|||||||
await writeFile(tsConfigPath, '{}', 'utf-8')
|
await writeFile(tsConfigPath, '{}', 'utf-8')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('detectTypeScript detects and registers runtime', async () => {
|
beforeEach(() => {
|
||||||
register.mockReset()
|
register.mockReset()
|
||||||
|
setGuard(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('detectTypeScript detects and registers runtime', async () => {
|
||||||
await detectTypeScript(rootDir)
|
await detectTypeScript(rootDir)
|
||||||
expect(register).toHaveBeenCalledTimes(1)
|
expect(register).toHaveBeenCalledTimes(1)
|
||||||
expect(register).toHaveBeenCalledWith({
|
expect(register).toHaveBeenCalledWith({
|
||||||
@ -28,8 +32,13 @@ describe('Typescript Support', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('multiple detectTypeScript calls registers runtime only once', async () => {
|
||||||
|
await detectTypeScript(rootDir)
|
||||||
|
await detectTypeScript(rootDir)
|
||||||
|
expect(register).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
test('detectTypeScript skips rootDir without tsconfig.json', async () => {
|
test('detectTypeScript skips rootDir without tsconfig.json', async () => {
|
||||||
register.mockReset()
|
|
||||||
await detectTypeScript(rootDir2)
|
await detectTypeScript(rootDir2)
|
||||||
expect(register).toHaveBeenCalledTimes(0)
|
expect(register).toHaveBeenCalledTimes(0)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user