forked from alloy-org/amplenote-embed-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
112 lines (95 loc) · 3.49 KB
/
build.mjs
File metadata and controls
112 lines (95 loc) · 3.49 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import serve, { error, log } from "create-serve";
import esbuild from "esbuild";
import JSZip from "jszip";
import fs from "node:fs";
import path from "node:path";
const IS_DEV = process.argv.includes("--dev");
const packageNotePlugin = {
name: "package-note-plugin",
setup(build) {
const options = build.initialOptions;
options.write = false;
build.onEnd(async ({ errors, outputFiles }) => {
if (errors.length > 0) {
console.error(errors);
} else {
let htmlContent = fs.readFileSync(path.join("assets", "embed.html"), "utf8");
for (const file of outputFiles) {
const { path: outputPath } = file;
if (outputPath.match(/\.js$/)) {
const base64JavascriptContent = Buffer.from(file.text).toString("base64");
htmlContent = htmlContent.replace("__BASE64JAVASCRIPTCONTENT__", base64JavascriptContent);
} else if (outputPath.match(/\.css$/)) {
const base64CssContent = Buffer.from(file.text).toString("base64");
htmlContent = htmlContent.replace("__BASE64CSSCONTENT__", base64CssContent);
}
}
const markdownContent = fs.readFileSync(path.join("assets", "note.md"), "utf8");
const zip = new JSZip();
zip.file("build.html.json", htmlContent);
zip.file("note.md", markdownContent);
const zipContent = await zip.generateAsync({ type: "nodebuffer" });
const outputDirectory = path.dirname(outputFiles[0].path);
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory);
}
const zipPath = path.join(outputDirectory, "plugin.zip");
fs.writeFileSync(zipPath, zipContent);
}
});
}
};
const serveBuildPlugin = {
name: "update-dev-plugin",
setup(build) {
const options = build.initialOptions;
options.write = false;
options.sourcemap = true;
build.onEnd(({ errors, outputFiles }) => {
if (errors.length > 0) {
error(`Build failed: ${ JSON.stringify(errors) }`);
} else {
outputFiles.forEach(file => {
const { path: outputPath } = file;
if (outputPath.match(/\.css$/)) {
const cssPath = path.join(path.dirname(outputPath), "index.css");
fs.writeFileSync(cssPath, file.text);
} else if (outputPath.match(/\.js$/)) {
const javascriptPath = path.join(path.dirname(outputPath), "index.js");
fs.writeFileSync(javascriptPath, file.text);
const htmlContent = fs.readFileSync(path.join("assets", "embed.dev.html"), "utf8");
const htmlPath = path.join(path.dirname(outputPath), "index.html");
fs.writeFileSync(htmlPath, htmlContent);
} else if (outputPath.match(/\.js.map$/)) {
const sourcemapPath = path.join(path.dirname(outputPath), "index.js.map");
fs.writeFileSync(sourcemapPath, file.text);
}
});
serve.update();
}
});
}
};
const buildOptions = {
bundle: true,
define: {
"process.env.NODE_ENV": IS_DEV ? '"development"' : '"production"',
},
entryPoints: [ "src/index.jsx" ],
minify: !IS_DEV,
outdir: "build",
sourceRoot: "src",
plugins: [ IS_DEV ? serveBuildPlugin : packageNotePlugin ],
target: [ "chrome58" , "firefox57", "safari11", "edge16" ],
};
if (IS_DEV) {
const context = await esbuild.context(buildOptions);
context.watch();
serve.start({
port: 5000,
root: "./build",
live: true,
});
} else {
await esbuild.build(buildOptions);
}