forked from sveltejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
108 lines (96 loc) · 3.3 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import builtins from 'builtin-modules';
import fs from 'fs';
import path from 'path';
import { decode } from 'sourcemap-codec';
const DEV = !!process.env.ROLLUP_WATCH;
function repl() {
require('ts-node').register({
project: 'test/tsconfig.json',
transpileOnly: true
});
const OUTDIR = path.resolve(__dirname, 'repl', 'output');
const INPUT = path.resolve(__dirname, 'repl', 'index.svelte');
const OUTPUT = path.resolve(__dirname, 'repl', 'output', 'code.tsx');
const MAP = path.resolve(__dirname, 'repl', 'output', 'code.tsx.map');
const MAPPINGS = path.resolve(__dirname, 'repl', 'output', 'mappings.jsx');
return {
name: 'dev-repl',
buildStart() {
this.addWatchFile(INPUT);
},
writeBundle() {
try {
const BUILD = require.resolve('./index.js');
const BUILD_TEST = require.resolve('./test/build.ts');
delete require.cache[BUILD];
const { svelte2tsx } = require('./index.js');
delete require.cache[BUILD_TEST];
require.cache[BUILD_TEST] = require.cache[BUILD];
const { process_transformed_text } = require('./test/sourcemaps/process');
const input_content = fs.readFileSync(INPUT, 'utf-8');
const { code, map } = svelte2tsx(input_content);
map.file = 'code.tsx';
map.sources = ['index.svelte'];
map.sourcesContent = [input_content];
if (!fs.existsSync(OUTDIR)) {
fs.mkdirSync(OUTDIR);
}
fs.writeFileSync(OUTPUT, code);
fs.writeFileSync(MAP, map.toString());
try {
const mappings = process_transformed_text(
input_content,
code, // @ts-expect-error
decode(map.mappings)
).print_mappings();
fs.writeFileSync(MAPPINGS, mappings);
} catch (e) {
fs.writeFileSync(MAPPINGS, e.toString());
}
} catch (e) {
fs.writeFileSync(OUTPUT, e.toString());
fs.writeFileSync(MAPPINGS, e.toString());
}
}
};
}
export default [
{
input: 'src/index.ts',
output: [
{
exports: 'auto',
sourcemap: true,
format: 'commonjs',
file: 'index.js'
},
{
exports: 'auto',
file: 'index.mjs',
format: 'esm'
}
],
plugins: [
resolve({ browser: false, preferBuiltins: true }),
commonjs(),
json(),
typescript({ include: ['src/**/*'] }),
DEV && repl()
],
watch: {
clearScreen: false
},
external: [
...builtins,
'typescript',
'svelte',
'svelte/compiler',
'dedent-js',
'pascal-case'
]
}
];