-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.ts
36 lines (30 loc) · 1.29 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
import { merge } from "lume/core/utils.ts";
import { toFileUrl } from "lume/deps/path.ts";
import { prepareAsset } from "lume/plugins/source_maps.ts";
import type { Site } from "lume/core.ts";
import type { Options } from "./babel/options.ts";
import * as babel from "./babel/deps.ts";
import defaultOptions from "./babel/options.ts";
export default function (userOptions?: Partial<Options>) {
let { extensions, ...babelOptions } = merge(defaultOptions, userOptions);
if (userOptions?.plugins) {
babelOptions.plugins!.unshift(...defaultOptions.plugins!);
}
babelOptions = babel.loadOptions(babelOptions)!;
return (site: Site) => {
site.loadAssets(extensions);
site.process(extensions, async (page) => {
const script = prepareAsset(site, page);
// WARNING: Passing cached plugin instances is not supported in babel.loadPartialConfig()
// const config = await babel.loadPartialConfigAsync(babelOptions!);
// babelOptions = config!.options;
if (script.enableSourceMap) {
babelOptions.sourceMaps ??= true;
babelOptions.sourceFileName = toFileUrl(script.filename).href;
}
const result = await babel.transformAsync(script.content, babelOptions)!;
page.content = result!.code!;
page.updateDest({ ext: ".js" });
});
};
}