Skip to content

Commit 4e7c1c8

Browse files
authored
Merge pull request #68 from zirkelc/fix-60-tsconfig-paths
fix: resolve tsconfig paths with tsx
2 parents 7360437 + 309c51c commit 4e7c1c8

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

lib/configuration/read.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ const parseConfigurationFile = async (configurationPath) => {
5656
case '.ts':
5757
case '.mts': {
5858
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);
6167

62-
const content = await jiti.import(configurationPath, { default: true });
63-
64-
return content;
68+
return content.default || content;
6569
} catch (error) {
6670
throw new ServerlessError(
6771
`Cannot parse "${path.basename(configurationPath)}": Initialization error: ${

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
"graceful-fs": "^4.2.11",
5454
"https-proxy-agent": "^5.0.1",
5555
"is-docker": "^2.2.1",
56-
"jiti": "^2.4.2",
5756
"js-yaml": "^4.1.0",
5857
"json-cycle": "^1.5.0",
5958
"json-refs": "^3.0.15",
@@ -73,6 +72,7 @@
7372
"strip-ansi": "^6.0.1",
7473
"supports-color": "^8.1.1",
7574
"timers-ext": "^0.1.7",
75+
"tsx": "^4.20.3",
7676
"type": "^2.7.2",
7777
"untildify": "^4.0.0",
7878
"uuid": "^9.0.0",

test/unit/lib/configuration/read.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,50 @@ describe('test/unit/lib/configuration/read.test.js', () => {
117117
}
118118
});
119119

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+
120164
it('should support deferred configuration result', async () => {
121165
// JS configurations are required (so immune to modules caching).
122166
// In this tests we cannot use same JS configuration path twice for testing

0 commit comments

Comments
 (0)