-
Notifications
You must be signed in to change notification settings - Fork 754
Expand file tree
/
Copy pathpatches.ts
More file actions
63 lines (57 loc) · 1.61 KB
/
Copy pathpatches.ts
File metadata and controls
63 lines (57 loc) · 1.61 KB
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
import type { Plugin } from "vite";
import * as babel from "@babel/core";
import { cjsPlugin } from "./patches/commonjs.ts";
import { jsxComments } from "./patches/jsx_comment.ts";
import babelReact from "@babel/preset-react";
import { inlineEnvVarsPlugin } from "./patches/inline_env_vars.ts";
import { removePolyfills } from "./patches/remove_polyfills.ts";
import { JS_REG, JSX_REG } from "../utils.ts";
import { codeEvalPlugin } from "./patches/code_eval.ts";
export function patches(): Plugin {
let isDev = false;
return {
name: "fresh:patches",
config(_, env) {
isDev = env.command === "serve";
},
applyToEnvironment() {
return true;
},
transform: {
filter: {
id: JS_REG,
},
handler(code, id, options) {
const presets = [];
if (!options?.ssr && JSX_REG.test(id)) {
presets.push([babelReact, {
runtime: "automatic",
importSource: "preact",
development: isDev,
}]);
}
const env = isDev ? "development" : "production";
const plugins: babel.PluginItem[] = [
codeEvalPlugin(options?.ssr ? "ssr" : "client", env),
cjsPlugin,
removePolyfills,
jsxComments,
inlineEnvVarsPlugin(env, Deno.env.toObject()),
];
const res = babel.transformSync(code, {
filename: id,
babelrc: false,
compact: true,
plugins,
presets,
});
if (res?.code) {
return {
code: res.code,
map: res.map,
};
}
},
},
};
}