Skip to content

Commit 18e6112

Browse files
committed
chore: Enable typescript --rewriteRelativeImportExtensions
1 parent 4cbf694 commit 18e6112

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

config/rollup.node-config.mjs

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
import typescript from '@rollup/plugin-typescript'
2+
import * as ts from 'typescript'
3+
4+
/**
5+
* Strip out TS relative import path rewrite helper from dynamic import() calls
6+
*
7+
* Required due to
8+
* https://github.com/rollup/plugins/issues/1820
9+
*
10+
* @param {ts.TransformationContext} context
11+
*/
12+
function fixDynamicImportRewrite(context) {
13+
/** @param {ts.SourceFile} source */
14+
return function fixDynamicImport(source) {
15+
/** @param {ts.Node} node */
16+
function visitor(node) {
17+
if (
18+
ts.isCallExpression(node) &&
19+
ts.isIdentifier(node.expression) &&
20+
String(node.expression.escapedText) ===
21+
'___rewriteRelativeImportExtension' &&
22+
node.arguments.length === 1
23+
) {
24+
return node.arguments[0]
25+
}
26+
return ts.visitEachChild(node, visitor, context)
27+
}
28+
return ts.visitNode(source, visitor)
29+
}
30+
}
231

332
export default [
433
{
@@ -20,6 +49,8 @@ export default [
2049
input: 'src/cli.ts',
2150
output: { file: 'dist/cli.mjs' },
2251
external: () => true,
23-
plugins: [typescript()]
52+
plugins: [
53+
typescript({ transformers: { after: [fixDynamicImportRewrite] } })
54+
]
2455
}
2556
]

eslint.config.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ export default [
3838
'no-control-regex': 'off',
3939
'no-fallthrough': ['error', { commentPattern: 'fallthrough' }],
4040
'no-implicit-globals': 'error',
41+
'no-restricted-imports': [
42+
'error',
43+
{
44+
patterns: [
45+
{
46+
regex: '^\\..*(?<!\\.ts)$',
47+
message: 'Relative imports must use .ts extension.'
48+
}
49+
]
50+
}
51+
],
4152
'no-template-curly-in-string': 'warn',
4253
'no-var': 'error',
4354
'prefer-const': ['warn', { destructuring: 'all' }],

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"noUnusedLocals": true,
77
"noUnusedParameters": true,
88
"outDir": "dist",
9+
"rewriteRelativeImportExtensions": true,
910
"rootDir": "src",
1011
"strict": true,
1112
"target": "ES2020",

0 commit comments

Comments
 (0)