@@ -5,26 +5,66 @@ import typescript from '@rollup/plugin-typescript';
5
5
import builtins from 'builtin-modules' ;
6
6
import fs from 'fs' ;
7
7
import path from 'path' ;
8
+ import { decode } from 'sourcemap-codec' ;
9
+
10
+ const DEV = ! ! process . env . ROLLUP_WATCH ;
8
11
9
12
function repl ( ) {
13
+ require ( 'ts-node' ) . register ( {
14
+ project : 'test/tsconfig.json' ,
15
+ transpileOnly : true
16
+ } ) ;
17
+ const OUTDIR = path . resolve ( __dirname , 'repl' , 'output' ) ;
18
+
19
+ const INPUT = path . resolve ( __dirname , 'repl' , 'index.svelte' ) ;
20
+ const OUTPUT = path . resolve ( __dirname , 'repl' , 'output' , 'code.tsx' ) ;
21
+ const MAP = path . resolve ( __dirname , 'repl' , 'output' , 'code.tsx.map' ) ;
22
+ const MAPPINGS = path . resolve ( __dirname , 'repl' , 'output' , 'mappings.jsx' ) ;
23
+
10
24
return {
11
25
name : 'dev-repl' ,
12
26
buildStart ( ) {
13
- this . addWatchFile ( './repl/index.svelte' ) ;
27
+ this . addWatchFile ( INPUT ) ;
14
28
} ,
15
29
writeBundle ( ) {
16
- if ( ! this . meta . watchMode ) return ;
30
+ const BUILD = require . resolve ( './index.js' ) ;
31
+ const BUILD_TEST = require . resolve ( './test/build.ts' ) ;
17
32
18
- const repl = `${ __dirname } /repl/` ;
19
- const output = `${ __dirname } /repl/output/` ;
20
-
21
- delete require . cache [ path . resolve ( __dirname , 'index.js' ) ] ;
33
+ delete require . cache [ BUILD ] ;
22
34
const svelte2tsx = require ( './index.js' ) ;
23
35
24
- const tsx = svelte2tsx ( fs . readFileSync ( `${ repl } /index.svelte` , 'utf-8' ) ) ;
36
+ delete require . cache [ BUILD_TEST ] ;
37
+ require . cache [ BUILD_TEST ] = require . cache [ BUILD ] ;
38
+ const { process_transformed_text } = require ( './test/sourcemaps/process' ) ;
39
+
40
+ const input_content = fs . readFileSync ( INPUT , 'utf-8' ) ;
25
41
26
- if ( ! fs . existsSync ( output ) ) fs . mkdirSync ( output ) ;
27
- fs . writeFileSync ( `${ repl } /output/code.tsx` , tsx . code ) ;
42
+ try {
43
+ const { code, map } = svelte2tsx ( input_content ) ;
44
+
45
+ map . file = 'code.tsx' ;
46
+ map . sources = [ 'index.svelte' ] ;
47
+ map . sourcesContent = [ input_content ] ;
48
+
49
+ if ( ! fs . existsSync ( OUTDIR ) ) fs . mkdirSync ( OUTDIR ) ;
50
+ fs . writeFileSync ( OUTPUT , code ) ;
51
+ fs . writeFileSync ( MAP , map . toString ( ) ) ;
52
+
53
+ try {
54
+ const mappings = process_transformed_text (
55
+ input_content ,
56
+ code , // @ts -expect-error
57
+ decode ( map . mappings )
58
+ ) . print_mappings ( ) ;
59
+
60
+ fs . writeFileSync ( MAPPINGS , mappings ) ;
61
+ } catch ( e ) {
62
+ fs . writeFileSync ( MAPPINGS , e . toString ( ) ) ;
63
+ }
64
+ } catch ( e ) {
65
+ fs . writeFileSync ( OUTPUT , e . toString ( ) ) ;
66
+ fs . writeFileSync ( MAPPINGS , e . toString ( ) ) ;
67
+ }
28
68
}
29
69
} ;
30
70
}
@@ -33,11 +73,13 @@ export default [
33
73
input : 'src/index.ts' ,
34
74
output : [
35
75
{
76
+ exports : 'auto' ,
36
77
sourcemap : true ,
37
78
format : 'commonjs' ,
38
79
file : 'index.js'
39
80
} ,
40
81
{
82
+ exports : 'auto' ,
41
83
file : 'index.mjs' ,
42
84
format : 'esm'
43
85
}
@@ -47,7 +89,7 @@ export default [
47
89
commonjs ( ) ,
48
90
json ( ) ,
49
91
typescript ( { include : [ 'src/**/*' ] } ) ,
50
- repl ( )
92
+ DEV && repl ( )
51
93
] ,
52
94
watch : {
53
95
clearScreen : false
0 commit comments