1
1
import 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
+ }
2
51
3
52
export default [
4
53
{
@@ -13,13 +62,22 @@ export default [
13
62
esModule : false ,
14
63
preserveModules : true
15
64
} ,
16
- plugins : [ typescript ( ) ] ,
65
+ plugins : [
66
+ typescript ( {
67
+ transformers : { afterDeclarations : [ fixDeclarationImportPaths ] }
68
+ } )
69
+ ] ,
17
70
treeshake : { moduleSideEffects : false , propertyReadSideEffects : false }
18
71
} ,
19
72
{
20
73
input : 'src/cli.ts' ,
21
74
output : { file : 'dist/cli.mjs' } ,
22
75
external : ( ) => true ,
23
- plugins : [ typescript ( ) ]
76
+ plugins : [
77
+ typescript ( {
78
+ declaration : false ,
79
+ transformers : { after : [ fixDynamicImportRewrite ] }
80
+ } )
81
+ ]
24
82
}
25
83
]
0 commit comments