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
fix: Drop .ts extension from import & export paths in .d.ts files
eemeli committed Nov 29, 2024
commit ab240c17d35bb808a6df8e3039b9ddd6a2de7ac4
31 changes: 29 additions & 2 deletions config/rollup.node-config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import typescript from '@rollup/plugin-typescript'
import * as ts from 'typescript'

/**
* Drop .ts extension from import & export paths in .d.ts files
* to support older TS versions.
*
* @param {ts.TransformationContext} context
*/
function fixDeclarationImportPaths(context) {
/** @param {ts.SourceFile} source */
return function fixPaths(source) {
/** @param {ts.Node} node */
function visitor(node) {
if (ts.isStringLiteral(node) && /^\.+\/.*\.ts$/.test(node.text)) {
return ts.factory.createStringLiteral(node.text.slice(0, -3), true)
}
return ts.visitEachChild(node, visitor, context)
}
return ts.visitNode(source, visitor)
}
}

/**
* Strip out TS relative import path rewrite helper from dynamic import() calls
*
@@ -42,15 +62,22 @@ export default [
esModule: false,
preserveModules: true
},
plugins: [typescript()],
plugins: [
typescript({
transformers: { afterDeclarations: [fixDeclarationImportPaths] }
})
],
treeshake: { moduleSideEffects: false, propertyReadSideEffects: false }
},
{
input: 'src/cli.ts',
output: { file: 'dist/cli.mjs' },
external: () => true,
plugins: [
typescript({ transformers: { after: [fixDynamicImportRewrite] } })
typescript({
declaration: false,
transformers: { after: [fixDynamicImportRewrite] }
})
]
}
]