Skip to content

Commit 299df21

Browse files
authored
feat: skip ts-node register for ESM plugins (#778)
* feat: skip ts-node register for ESM plugins * chore: update debug message
1 parent 660ec2c commit 299df21

2 files changed

Lines changed: 30 additions & 43 deletions

File tree

src/config/ts-node.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {TSConfig, Plugin} from '../interfaces'
66
import {settings} from '../settings'
77
import {isProd} from '../util'
88
import {Debug} from './util'
9-
import {Config} from './config'
109
import {memoizedWarn} from '../errors'
1110
// eslint-disable-next-line new-cap
1211
const debug = Debug('ts-node')
@@ -110,7 +109,6 @@ function registerTSNode(root: string): TSConfig | undefined {
110109
*/
111110
export function tsPath(root: string, orig: string, plugin: Plugin): string
112111
export function tsPath(root: string, orig: string | undefined, plugin?: Plugin): string | undefined
113-
// eslint-disable-next-line complexity
114112
export function tsPath(root: string, orig: string | undefined, plugin?: Plugin): string | undefined {
115113
if (!orig) return orig
116114
orig = orig.startsWith(root) ? orig : path.join(root, orig)
@@ -122,32 +120,21 @@ export function tsPath(root: string, orig: string | undefined, plugin?: Plugin):
122120
return orig
123121
}
124122

125-
// Skip ts-node registration if plugin is an ESM plugin executing from a CJS plugin
126-
if (plugin?.moduleType === 'module' && Config.rootPlugin?.moduleType === 'commonjs') {
127-
debug(`Skipping ts-node registration for ${root} because it's an ESM module but the root plugin is CommonJS`)
123+
// Skip ts-node registration for ESM plugins.
124+
// The node ecosystem is not mature enough to support auto-transpiling ESM modules at this time.
125+
// See the following:
126+
// - https://github.com/TypeStrong/ts-node/issues/1791#issuecomment-1149754228
127+
// - https://github.com/nodejs/node/issues/49432
128+
// - https://github.com/nodejs/node/pull/49407
129+
// - https://github.com/nodejs/node/issues/34049
130+
if (plugin?.moduleType === 'module') {
131+
debug(`Skipping ts-node registration for ${root} because it's an ESM module`)
128132
if (plugin.type === 'link')
129-
memoizedWarn(`${plugin.name} is a linked ESM module and cannot be auto-compiled from a CommonJS root plugin. Existing compiled source will be used instead.`)
133+
memoizedWarn(`${plugin.name} is a linked ESM module and cannot be auto-transpiled. Existing compiled source will be used instead.`)
130134

131135
return orig
132136
}
133137

134-
// If plugin is an ESM plugin being executed from an ESM root plugin, check to see if ts-node/esm loader has been set
135-
// either in the NODE_OPTIONS env var or from the exec args. If the ts-node/esm loader has NOT been loaded then we want
136-
// to skip ts-node registration so that it falls back on the compiled source.
137-
if (plugin?.moduleType === 'module') {
138-
const tsNodeEsmLoaderInExecArgv = process.execArgv.includes('--loader') && process.execArgv.includes('ts-node/esm')
139-
const tsNodeEsmLoaderInNodeOptions = process.env.NODE_OPTIONS?.includes('--loader=ts-node/esm') ?? false
140-
if (!tsNodeEsmLoaderInExecArgv && !tsNodeEsmLoaderInNodeOptions) {
141-
debug(`Skipping ts-node registration for ${root} because it's an ESM module but the ts-node/esm loader hasn't been run`)
142-
debug('try setting NODE_OPTIONS="--loader ts-node/esm" in your environment.')
143-
if (plugin.type === 'link') {
144-
memoizedWarn(`${plugin.name} is a linked ESM module and cannot be auto-compiled without setting NODE_OPTIONS="--loader=ts-node/esm" in the environment. Existing compiled source will be used instead.`)
145-
}
146-
147-
return orig
148-
}
149-
}
150-
151138
if (settings.tsnodeEnabled === undefined && isProd() && plugin?.type !== 'link') {
152139
debug(`Skipping ts-node registration for ${root} because NODE_ENV is NOT "test" or "development"`)
153140
return orig

test/integration/esm-cjs.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,34 +365,34 @@ type CleanUpOptions = {
365365
await test('Link ESM plugin to ESM root plugin', async () => {
366366
const plugin = PLUGINS.esm2
367367

368-
const linkedPlugin = await linkPlugin({executor: esmExecutor, plugin, script: 'run'})
368+
await linkPlugin({executor: esmExecutor, plugin, script: 'run'})
369369
// test bin/run
370-
// NOTE: this also tests that the compiled source is used when ts-node/esm loader is not specified
371370
await runCommand({
372371
executor: esmExecutor,
373372
plugin,
374373
script: 'run',
375374
expectStrings: [plugin.commandText, plugin.hookText],
376375
})
377-
// test un-compiled changes with bin/run
378-
await modifyCommand({executor: linkedPlugin, plugin, from: 'hello', to: 'howdy'})
379-
await runCommand({
380-
executor: esmExecutor,
381-
plugin,
382-
script: 'run',
383-
expectStrings: ['howdy', plugin.hookText],
384-
env: {NODE_OPTIONS: '--loader=ts-node/esm'},
385-
})
386376

387-
// test un-compiled changes with bin/dev
388-
await modifyCommand({executor: linkedPlugin, plugin, from: 'howdy', to: 'cheers'})
389-
await runCommand({
390-
executor: esmExecutor,
391-
plugin,
392-
script: 'dev',
393-
expectStrings: ['cheers', plugin.hookText],
394-
env: {NODE_OPTIONS: '--loader=ts-node/esm'},
395-
})
377+
// Skipping these because we decided to not support auto-transpiling ESM plugins at this time.
378+
// // test un-compiled changes with bin/run
379+
// await modifyCommand({executor: linkedPlugin, plugin, from: 'hello', to: 'howdy'})
380+
// await runCommand({
381+
// executor: esmExecutor,
382+
// plugin,
383+
// script: 'run',
384+
// expectStrings: ['howdy', plugin.hookText],
385+
// env: {NODE_OPTIONS: '--loader=ts-node/esm'},
386+
// })
387+
// // test un-compiled changes with bin/dev
388+
// await modifyCommand({executor: linkedPlugin, plugin, from: 'howdy', to: 'cheers'})
389+
// await runCommand({
390+
// executor: esmExecutor,
391+
// plugin,
392+
// script: 'dev',
393+
// expectStrings: ['cheers', plugin.hookText],
394+
// env: {NODE_OPTIONS: '--loader=ts-node/esm'},
395+
// })
396396

397397
await cleanUp({executor: esmExecutor, plugin, script: 'run'})
398398
})

0 commit comments

Comments
 (0)