File tree Expand file tree Collapse file tree 3 files changed +54
-6
lines changed
test/unit/lib/configuration Expand file tree Collapse file tree 3 files changed +54
-6
lines changed Original file line number Diff line number Diff line change @@ -56,12 +56,16 @@ const parseConfigurationFile = async (configurationPath) => {
56
56
case '.ts' :
57
57
case '.mts' : {
58
58
try {
59
- const { createJiti } = require ( 'jiti' ) ;
60
- const jiti = createJiti ( null , { interopDefault : true } ) ;
59
+ /**
60
+ * Jiti does not support `tsconfig.paths`, so we need to use tsx to load the configuration file.
61
+ * @see https://github.com/unjs/jiti/issues/373
62
+ * @see https://tsx.is/dev-api/tsx-require
63
+ */
64
+ // eslint-disable-next-line import/no-unresolved
65
+ const tsx = require ( 'tsx/cjs/api' ) ;
66
+ const content = tsx . require ( configurationPath , __filename ) ;
61
67
62
- const content = await jiti . import ( configurationPath , { default : true } ) ;
63
-
64
- return content ;
68
+ return content . default || content ;
65
69
} catch ( error ) {
66
70
throw new ServerlessError (
67
71
`Cannot parse "${ path . basename ( configurationPath ) } ": Initialization error: ${
Original file line number Diff line number Diff line change 53
53
"graceful-fs" : " ^4.2.11" ,
54
54
"https-proxy-agent" : " ^5.0.1" ,
55
55
"is-docker" : " ^2.2.1" ,
56
- "jiti" : " ^2.4.2" ,
57
56
"js-yaml" : " ^4.1.0" ,
58
57
"json-cycle" : " ^1.5.0" ,
59
58
"json-refs" : " ^3.0.15" ,
73
72
"strip-ansi" : " ^6.0.1" ,
74
73
"supports-color" : " ^8.1.1" ,
75
74
"timers-ext" : " ^0.1.7" ,
75
+ "tsx" : " ^4.20.3" ,
76
76
"type" : " ^2.7.2" ,
77
77
"untildify" : " ^4.0.0" ,
78
78
"uuid" : " ^9.0.0" ,
Original file line number Diff line number Diff line change @@ -117,6 +117,50 @@ describe('test/unit/lib/configuration/read.test.js', () => {
117
117
}
118
118
} ) ;
119
119
120
+ it ( 'should support tsconfig.paths' , async ( ) => {
121
+ await fse . ensureDir ( 'node_modules' ) ;
122
+ const tsconfigPath = 'tsconfig.json' ;
123
+ const servicePath = 'test.ts' ;
124
+
125
+ try {
126
+ await fse . writeFile (
127
+ tsconfigPath ,
128
+ JSON . stringify ( {
129
+ compilerOptions : {
130
+ paths : {
131
+ '@/test' : [ './test.ts' ] ,
132
+ } ,
133
+ } ,
134
+ include : [ '**/*.ts' ] ,
135
+ } )
136
+ ) ;
137
+
138
+ await fse . writeFile ( servicePath , "export const service = 'test-ts';" ) ;
139
+
140
+ configurationPath = 'serverless.ts' ;
141
+ const configuration = {
142
+ service : 'test-ts' ,
143
+ provider : { name : 'aws' } ,
144
+ } ;
145
+
146
+ await fsp . writeFile (
147
+ configurationPath ,
148
+ `import { service } from '@/test';
149
+
150
+ export default {
151
+ service: service,
152
+ provider: { name: 'aws' },
153
+ }`
154
+ ) ;
155
+ const result = await readConfiguration ( configurationPath ) ;
156
+ expect ( result ) . to . deep . equal ( configuration ) ;
157
+ } finally {
158
+ await fse . remove ( 'node_modules' ) ;
159
+ await fsp . unlink ( tsconfigPath ) ;
160
+ await fsp . unlink ( servicePath ) ;
161
+ }
162
+ } ) ;
163
+
120
164
it ( 'should support deferred configuration result' , async ( ) => {
121
165
// JS configurations are required (so immune to modules caching).
122
166
// In this tests we cannot use same JS configuration path twice for testing
You can’t perform that action at this time.
0 commit comments