This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.ts
89 lines (71 loc) · 2.14 KB
/
plugin.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
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
import type { Plugin } from "https://deno.land/x/aleph@v0.3.0-beta.19/types.d.ts";
import type { RequiredConfig } from "https://deno.land/x/aleph@v0.3.0-beta.19/server/config.ts";
import * as colors from "https://deno.land/std@0.110.0/fmt/colors.ts";
import * as path from "https://deno.land/std@0.110.0/path/mod.ts";
// Types
type Aleph = Parameters<Plugin["setup"]>[0] & { config: RequiredConfig };
// Constants
const baseDir = new URL(".", import.meta.url).pathname;
const nodeDir = path.resolve(baseDir, "./node/");
// State
const styles: { [key: string]: string } = {};
// Helper Functions
const log = (...messages: string[]) => {
console.log(colors.magenta("Emotion"), ...messages);
};
const webpack = (aleph: Aleph, options: {
watch: boolean;
}) => {
return Deno.run({
cmd: [
"npx",
"webpack",
"--config",
path.resolve(nodeDir, "./webpack.config.js"),
].concat(options.watch ? ["--watch"] : []),
env: {
_ALEPH_PLUGIN_EMOTION_BASE_DIRECTORY: baseDir,
_ALEPH_PLUGIN_EMOTION_MODE: aleph.mode,
_ALEPH_PLUGIN_EMOTION_SOURCE_DIRECTORY: path.resolve(
aleph.workingDir,
`.${aleph.config.srcDir}`,
),
_ALEPH_PLUGIN_EMOTION_WORKING_DIRECTORY: aleph.workingDir,
},
stderr: "null",
stdout: "null",
}).status();
};
// Main
export default <Plugin> {
name: "emotion",
async setup(
aleph: Aleph,
) {
await webpack(aleph, { watch: false });
// Events
aleph.onTransform(/\/pages\/.+.tsx$/, async ({ module: { specifier } }) => {
const css = new TextDecoder().decode(
await Deno.run({
cmd: [
"node",
path.resolve(nodeDir, "./output/", `./${specifier}.js`),
],
stdout: "piped",
}).output(),
);
styles[specifier] = css;
});
aleph.onRender(({ html, path }) => {
html.head.push(
`<style data-emotion>${Object.values(styles).join("")}</style>`,
);
log("render", path);
});
// In development mode
if (aleph.mode === "development") {
webpack(aleph, { watch: true });
log("start watching code changes...");
}
},
};