Skip to content

Commit

Permalink
fix: Drop .ts extension from import & export paths in .d.ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Nov 29, 2024
1 parent c4c49f9 commit ab240c1
Showing 1 changed file with 29 additions and 2 deletions.
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
*
Expand Down Expand Up @@ -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] }
})
]
}
]

0 comments on commit ab240c1

Please sign in to comment.