File tree 3 files changed +28
-3
lines changed
3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -2,20 +2,34 @@ import type { Loader } from "cosmiconfig";
2
2
import type { RegisterOptions , Service } from "ts-node" ;
3
3
4
4
import { TypeScriptCompileError } from "./typescript-compile-error" ;
5
+ import { instanceOfNodeError } from "./util" ;
5
6
6
7
export function TypeScriptLoader ( options ?: RegisterOptions ) : Loader {
7
8
let tsNodeInstance : Service ;
8
9
9
10
return ( path : string , content : string ) => {
10
- try {
11
- if ( ! tsNodeInstance ) {
11
+ if ( ! tsNodeInstance ) {
12
+ try {
12
13
const { register } = require ( "ts-node" ) as typeof import ( "ts-node" ) ;
13
14
tsNodeInstance = register ( {
14
15
...options ,
15
16
compilerOptions : { module : "commonjs" } ,
16
17
} ) ;
18
+ } catch ( error ) {
19
+ if (
20
+ instanceOfNodeError ( error ) &&
21
+ error . code === "ERR_MODULE_NOT_FOUND"
22
+ ) {
23
+ throw new Error (
24
+ "cosmiconfig-typescript-loader: 'ts-node' is required for loading TypeScript cosmiconfig configuration files." +
25
+ `Make sure it is installed\nError: ${ error . message } `
26
+ ) ;
27
+ }
28
+ throw error ;
17
29
}
30
+ }
18
31
32
+ try {
19
33
// cosmiconfig requires the transpiled configuration to be CJS
20
34
tsNodeInstance . compile ( content , path ) ;
21
35
const result = require ( path ) ;
Original file line number Diff line number Diff line change
1
+ export function instanceOfNodeError (
2
+ error : unknown
3
+ ) : error is NodeJS . ErrnoException {
4
+ return (
5
+ error instanceof Error &&
6
+ // https://github.com/DefinitelyTyped/DefinitelyTyped/commit/cddd0b7aab18761214d26a0c7012cf45de5285a9
7
+ ( error as Record < string , any > ) . code !== undefined
8
+ ) ;
9
+ }
Original file line number Diff line number Diff line change 36
36
"peerDependencies" : {
37
37
"@types/node" : " *" ,
38
38
"cosmiconfig" : " >=7" ,
39
- "ts-node" : " >=10" ,
40
39
"typescript" : " >=4"
41
40
},
41
+ "optionalDependencies" : {
42
+ "ts-node" : " >=10"
43
+ },
42
44
"devDependencies" : {
43
45
"@swc/core" : " ^1.3.44" ,
44
46
"@swc/jest" : " ^0.2.24" ,
You can’t perform that action at this time.
0 commit comments