|
1 | | -import { register, RegisterOptions, Service } from './index'; |
| 1 | +import { register, RegisterOptions, Service, type TSError } from './index'; |
2 | 2 | import { parse as parseUrl, format as formatUrl, UrlWithStringQuery, fileURLToPath, pathToFileURL } from 'url'; |
3 | 3 | import { extname, resolve as pathResolve } from 'path'; |
4 | 4 | import * as assert from 'assert'; |
@@ -223,7 +223,7 @@ export function createEsmHooks(tsNodeService: Service) { |
223 | 223 | format: NodeLoaderHooksFormat; |
224 | 224 | source: string | Buffer | undefined; |
225 | 225 | }> { |
226 | | - return addShortCircuitFlag(async () => { |
| 226 | + return await addShortCircuitFlag(async () => { |
227 | 227 | // If we get a format hint from resolve() on the context then use it |
228 | 228 | // otherwise call the old getFormat() hook using node's old built-in defaultGetFormat() that ships with ts-node |
229 | 229 | const format = |
@@ -251,8 +251,23 @@ export function createEsmHooks(tsNodeService: Service) { |
251 | 251 | }); |
252 | 252 |
|
253 | 253 | // Call the old hook |
254 | | - const { source: transformedSource } = await transformSource(rawSource, { url, format }, defaultTransformSource); |
255 | | - source = transformedSource; |
| 254 | + try { |
| 255 | + const { source: transformedSource } = await transformSource( |
| 256 | + rawSource, |
| 257 | + { url, format }, |
| 258 | + defaultTransformSource |
| 259 | + ); |
| 260 | + source = transformedSource; |
| 261 | + } catch (er) { |
| 262 | + // throw an error that can make it through the loader thread |
| 263 | + // comms channel intact. |
| 264 | + const tsErr = er as TSError; |
| 265 | + const err = new Error(tsErr.message.trimEnd()); |
| 266 | + const { diagnosticCodes } = tsErr; |
| 267 | + Object.assign(err, { diagnosticCodes }); |
| 268 | + Error.captureStackTrace(err, load); |
| 269 | + throw err; |
| 270 | + } |
256 | 271 | } |
257 | 272 |
|
258 | 273 | return { format, source }; |
@@ -360,11 +375,12 @@ export function createEsmHooks(tsNodeService: Service) { |
360 | 375 | } |
361 | 376 |
|
362 | 377 | async function addShortCircuitFlag<T>(fn: () => Promise<T>) { |
363 | | - const ret = await fn(); |
364 | | - // Not sure if this is necessary; being lazy. Can revisit in the future. |
365 | | - if (ret == null) return ret; |
366 | | - return { |
367 | | - ...ret, |
368 | | - shortCircuit: true, |
369 | | - }; |
| 378 | + return fn().then((ret) => { |
| 379 | + // Not sure if this is necessary; being lazy. Can revisit in the future. |
| 380 | + if (ret == null) return ret; |
| 381 | + return { |
| 382 | + ...ret, |
| 383 | + shortCircuit: true, |
| 384 | + }; |
| 385 | + }); |
370 | 386 | } |
0 commit comments