Skip to content

Commit

Permalink
chore: Enable typescript --rewriteRelativeImportExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Nov 29, 2024
1 parent 4cbf694 commit 18e6112
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
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 [
{
Expand All @@ -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
Expand Up @@ -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' }],
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist",
"rewriteRelativeImportExtensions": true,
"rootDir": "src",
"strict": true,
"target": "ES2020",
Expand Down

0 comments on commit 18e6112

Please sign in to comment.