/
babel.ts
50 lines (43 loc) · 1.3 KB
/
babel.ts
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
import { transformAsync } from '@babel/core';
import type { TransformOptions } from '@babel/core';
import type { Transformer, Options } from '../types';
const transformer: Transformer<Options.Babel> = async ({
content,
filename,
options,
map = undefined,
}) => {
const babelOptions = {
...options,
inputSourceMap:
typeof map === 'string' ? JSON.parse(map) : map ?? undefined,
sourceType: 'module',
// istanbul ignore next
sourceMaps: !!options?.sourceMaps,
filename,
minified: false,
ast: false,
code: true,
caller: {
name: 'svelte-preprocess',
supportsStaticESM: true,
supportsDynamicImport: true,
// this isn't supported by Svelte but let it error with a good error on this syntax untouched
supportsTopLevelAwait: true,
// todo: this can be enabled once all "peer deps" understand this
// this syntax is supported since rollup@1.26.0 and webpack@5.0.0-beta.21
// supportsExportNamespaceFrom: true,
...options?.caller,
},
} as TransformOptions;
const result = await transformAsync(content, babelOptions);
if (result == null) {
return { code: content };
}
const { code, map: sourcemap } = result;
return {
code: code as string,
map: sourcemap ?? undefined,
};
};
export { transformer };