Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use .ts extension in relative source imports #591

Merged
merged 8 commits into from
Dec 30, 2024
Prev Previous commit
Next Next commit
chore: Enable typescript --rewriteRelativeImportExtensions
eemeli committed Nov 29, 2024
commit 18e611279076efdcd9f4984766d852b4e1f7db99
33 changes: 32 additions & 1 deletion config/rollup.node-config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import typescript from '@rollup/plugin-typescript'
import * as ts from 'typescript'

/**
* Strip out TS relative import path rewrite helper from dynamic import() calls
*
* Required due to
* https://github.com/rollup/plugins/issues/1820
*
* @param {ts.TransformationContext} context
*/
function fixDynamicImportRewrite(context) {
/** @param {ts.SourceFile} source */
return function fixDynamicImport(source) {
/** @param {ts.Node} node */
function visitor(node) {
if (
ts.isCallExpression(node) &&
ts.isIdentifier(node.expression) &&
String(node.expression.escapedText) ===
'___rewriteRelativeImportExtension' &&
node.arguments.length === 1
) {
return node.arguments[0]
}
return ts.visitEachChild(node, visitor, context)
}
return ts.visitNode(source, visitor)
}
}

export default [
{
@@ -20,6 +49,8 @@ export default [
input: 'src/cli.ts',
output: { file: 'dist/cli.mjs' },
external: () => true,
plugins: [typescript()]
plugins: [
typescript({ transformers: { after: [fixDynamicImportRewrite] } })
]
}
]
11 changes: 11 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -38,6 +38,17 @@ export default [
'no-control-regex': 'off',
'no-fallthrough': ['error', { commentPattern: 'fallthrough' }],
'no-implicit-globals': 'error',
'no-restricted-imports': [
'error',
{
patterns: [
{
regex: '^\\..*(?<!\\.ts)$',
message: 'Relative imports must use .ts extension.'
}
]
}
],
'no-template-curly-in-string': 'warn',
'no-var': 'error',
'prefer-const': ['warn', { destructuring: 'all' }],
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist",
"rewriteRelativeImportExtensions": true,
"rootDir": "src",
"strict": true,
"target": "ES2020",