11import typescript from '@rollup/plugin-typescript'
2+ import * as ts from 'typescript'
3+
4+ /**
5+ * Drop .ts extension from import & export paths in .d.ts files
6+ * to support older TS versions.
7+ *
8+ * @param {ts.TransformationContext } context
9+ */
10+ function fixDeclarationImportPaths ( context ) {
11+ /** @param {ts.SourceFile } source */
12+ return function fixPaths ( source ) {
13+ /** @param {ts.Node } node */
14+ function visitor ( node ) {
15+ if ( ts . isStringLiteral ( node ) && / ^ \. + \/ .* \. t s $ / . test ( node . text ) ) {
16+ return ts . factory . createStringLiteral ( node . text . slice ( 0 , - 3 ) , true )
17+ }
18+ return ts . visitEachChild ( node , visitor , context )
19+ }
20+ return ts . visitNode ( source , visitor )
21+ }
22+ }
23+
24+ /**
25+ * Strip out TS relative import path rewrite helper from dynamic import() calls
26+ *
27+ * Required due to
28+ * https://github.com/rollup/plugins/issues/1820
29+ *
30+ * @param {ts.TransformationContext } context
31+ */
32+ function fixDynamicImportRewrite ( context ) {
33+ /** @param {ts.SourceFile } source */
34+ return function fixDynamicImport ( source ) {
35+ /** @param {ts.Node } node */
36+ function visitor ( node ) {
37+ if (
38+ ts . isCallExpression ( node ) &&
39+ ts . isIdentifier ( node . expression ) &&
40+ String ( node . expression . escapedText ) ===
41+ '___rewriteRelativeImportExtension' &&
42+ node . arguments . length === 1
43+ ) {
44+ return node . arguments [ 0 ]
45+ }
46+ return ts . visitEachChild ( node , visitor , context )
47+ }
48+ return ts . visitNode ( source , visitor )
49+ }
50+ }
251
352export default [
453 {
@@ -13,13 +62,22 @@ export default [
1362 esModule : false ,
1463 preserveModules : true
1564 } ,
16- plugins : [ typescript ( ) ] ,
65+ plugins : [
66+ typescript ( {
67+ transformers : { afterDeclarations : [ fixDeclarationImportPaths ] }
68+ } )
69+ ] ,
1770 treeshake : { moduleSideEffects : false , propertyReadSideEffects : false }
1871 } ,
1972 {
2073 input : 'src/cli.ts' ,
2174 output : { file : 'dist/cli.mjs' } ,
2275 external : ( ) => true ,
23- plugins : [ typescript ( ) ]
76+ plugins : [
77+ typescript ( {
78+ declaration : false ,
79+ transformers : { after : [ fixDynamicImportRewrite ] }
80+ } )
81+ ]
2482 }
2583]
0 commit comments