Skip to content

Commit 8ae2ffd

Browse files
committed
Build widget separately from the web app.
1 parent 0a3fcfd commit 8ae2ffd

8 files changed

+107
-45
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ node_modules
2222
*.sw?
2323
.vercel
2424

25-
public/d/
25+
/dist

api/widget.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { readFile } from "fs/promises";
22
import { resolve, join } from "path";
33

4-
const staticDir = "./public";
4+
const staticDir = "./dist";
55

66
export default async function handler(request, response) {
77
const manifestPath = resolve(join(staticDir, "d", "manifest.json"));
88
const manifest = JSON.parse(await readFile(manifestPath, "utf-8"));
99

10-
const { file: widgetEntry } = manifest["widget/widget.js"];
10+
const { file: widgetEntry } = manifest["widget.js"];
1111

1212
response.setHeader("Content-Type", "application/javascript");
1313
response.setHeader("Cache-Control", "max-age=0, s-maxage=604800");

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"private": true,
88
"license": "MIT",
99
"scripts": {
10-
"build": "vite build"
10+
"build": "vite build web && vite build widget"
1111
},
1212
"devDependencies": {
1313
"vite": "^4.2.0",

vercel.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
{
2-
"outputDirectory": "public",
32
"rewrites": [{ "source": "/widget.js", "destination": "/api/widget" }],
43

54
"headers": [
5+
{
6+
"source": "/assets/(.*)",
7+
"headers": [
8+
{
9+
"key": "Cache-Control",
10+
"value": "public, max-age=604800, immutable"
11+
}
12+
]
13+
},
614
{
715
"source": "/d/(.*)",
816
"headers": [

vite.config.js

-40
This file was deleted.

web/index.html

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
</head>
7+
<body>
8+
<div id="root"></div>
9+
<script type="module">
10+
import { createPoll } from "/widget.js";
11+
12+
const config = {
13+
question: "What is the best album of 2016?",
14+
url: "ficus.io",
15+
answers: [
16+
{
17+
id: "a1",
18+
color: "rgba(61, 118, 230, 1.00)",
19+
label: "Chance The Rapper – Coloring Book",
20+
},
21+
{ id: "a2", color: "rgba(241, 196, 15, 1.00)", label: "Beyoncé – Lemonade" },
22+
{
23+
id: "a3",
24+
color: "rgba(230, 126, 34, 1.00)",
25+
label: "Frank Ocean – Blonde",
26+
},
27+
{ id: "a4", color: "rgba(231, 76, 60, 1.00)", label: "David Bowie – Star" },
28+
{
29+
id: "a5",
30+
color: "rgba(142, 68, 173, 1.00)",
31+
label: "Kanye West – The Life Of Pablo",
32+
},
33+
],
34+
};
35+
36+
const [update] = createPoll(document.getElementById("root"), { config, type: "bar" });
37+
38+
update({
39+
votes: [
40+
{ id: "p1", answers: ["a1", "a2"] },
41+
{ id: "p2", answer: "a2" },
42+
{ id: "p3", answers: ["a2"] },
43+
],
44+
});
45+
</script>
46+
</body>
47+
</html>

web/vite.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// vite.config.js
2+
import { resolve } from "path";
3+
4+
export default {
5+
publicDir: resolve(__dirname, "../dist"),
6+
build: {
7+
target: "modules",
8+
emptyOutDir: true, // clean dist (must be called before building the widget)
9+
outDir: resolve(__dirname, "../dist"),
10+
rollupOptions: {
11+
external: ["/widget.js"],
12+
},
13+
},
14+
};

widget/vite.config.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// vite.config.js
2+
import { resolve } from "path";
3+
import cssInjectedByJSPlugin from "vite-plugin-css-injected-by-js";
4+
5+
export default {
6+
publicDir: false, // no static assets here, just a single js file
7+
8+
plugins: [cssInjectedByJSPlugin({ styleId: "ficus-io" })],
9+
10+
css: {
11+
modules: {
12+
localsConvention: "dashesOnly", // .my-component => styles.myComponent
13+
},
14+
},
15+
16+
esbuild: {
17+
jsxFactory: "h", // preact
18+
jsxFragment: "Fragment",
19+
},
20+
21+
build: {
22+
target: "modules",
23+
manifest: true,
24+
sourcemap: true,
25+
emptyOutDir: true, // clean dist/d dir before build
26+
outDir: resolve(__dirname, "../dist/d"), // "d" stands for "dist"
27+
lib: {
28+
entry: resolve(__dirname, "widget.js"),
29+
formats: ["es"],
30+
fileName: "widget-[hash:8]",
31+
},
32+
},
33+
};

0 commit comments

Comments
 (0)