forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatches.ts
More file actions
68 lines (61 loc) · 1.82 KB
/
patches.ts
File metadata and controls
68 lines (61 loc) · 1.82 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
64
65
66
67
68
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 { 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";
// @ts-ignore Workaround for https://github.com/denoland/deno/issues/30850
const { default: babelReact } = await import("@babel/preset-react");
export function patches(): Plugin {
let isDev = false;
return {
name: "fresh:patches",
sharedDuringBuild: true,
config(_, env) {
isDev = env.command === "serve";
},
applyToEnvironment() {
return true;
},
transform: {
filter: {
id: JS_REG,
},
handler(code, id) {
const presets = [];
if (this.environment.config.consumer === "client" && JSX_REG.test(id)) {
presets.push([babelReact, {
runtime: "automatic",
importSource: "preact",
development: isDev,
throwIfNamespace: false,
}]);
}
const env = isDev ? "development" : "production";
const plugins: babel.PluginItem[] = [
codeEvalPlugin(this.environment.config.consumer, env),
cjsPlugin,
removePolyfills,
jsxComments,
inlineEnvVarsPlugin(env, Deno.env.toObject()),
];
const res = babel.transformSync(code, {
filename: id,
babelrc: false,
compact: false,
plugins,
presets,
sourceMaps: "both",
});
if (res?.code) {
return {
code: res.code,
map: res.map,
};
}
},
},
};
}