olum-compiler 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/bundle.js +50 -0
- package/package.json +1 -1
package/bin/bundle.js
CHANGED
|
@@ -18,6 +18,50 @@ function getLogs() {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
function resolveBase() {
|
|
22
|
+
let conf;
|
|
23
|
+
try {
|
|
24
|
+
const pkg = JSON.parse(
|
|
25
|
+
fs.readFileSync(path.join(root, "package.json"), "utf8"),
|
|
26
|
+
);
|
|
27
|
+
conf = pkg.olum && (pkg.olum.base || pkg.olum.BASE);
|
|
28
|
+
} catch (err) {}
|
|
29
|
+
if (typeof conf !== "string" || !conf.trim()) return "/";
|
|
30
|
+
|
|
31
|
+
const value = conf.trim();
|
|
32
|
+
if (/[\s"'<>`]/.test(value)) {
|
|
33
|
+
console.error(
|
|
34
|
+
colors(
|
|
35
|
+
"red",
|
|
36
|
+
'ignoring package.json "olum.base" — expected "./", "/" or a sub-path like "/app/"',
|
|
37
|
+
),
|
|
38
|
+
);
|
|
39
|
+
return "/";
|
|
40
|
+
}
|
|
41
|
+
if (value.startsWith(".")) return "./";
|
|
42
|
+
if (/^(https?:)?\/\//.test(value)) return value.replace(/\/*$/, "/");
|
|
43
|
+
return ("/" + value.replace(/^\/+/, "")).replace(/\/*$/, "/");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function staticUrlPlugin(base) {
|
|
47
|
+
const urlAttr = /((?:src|href|poster)=\\?["'])\/([^"'`\\?#\s]+\.[a-z0-9]+)/gi;
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
name: "olum:static-urls",
|
|
51
|
+
apply: "build",
|
|
52
|
+
enforce: "post",
|
|
53
|
+
renderChunk(code) {
|
|
54
|
+
let changed = false;
|
|
55
|
+
const out = code.replace(urlAttr, (match, head, url) => {
|
|
56
|
+
if (!fs.existsSync(path.join(public, url))) return match;
|
|
57
|
+
changed = true;
|
|
58
|
+
return head + base + url;
|
|
59
|
+
});
|
|
60
|
+
return changed ? { code: out, map: null } : null;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
21
65
|
function copyStatic(from, to, absorbed) {
|
|
22
66
|
for (const name of fs.readdirSync(from)) {
|
|
23
67
|
const src = path.join(from, name);
|
|
@@ -99,9 +143,15 @@ async function bundle(bootAt) {
|
|
|
99
143
|
(pkgName) => !fs.existsSync(path.join(root, "node_modules", pkgName)),
|
|
100
144
|
);
|
|
101
145
|
|
|
146
|
+
const base = resolveBase();
|
|
147
|
+
if (base !== "/")
|
|
148
|
+
console.log(colors("white", `building with base "${base}"`));
|
|
149
|
+
|
|
102
150
|
const buildResult = await build({
|
|
103
151
|
root: public,
|
|
152
|
+
base,
|
|
104
153
|
resolve: { alias },
|
|
154
|
+
plugins: base === "/" ? [] : [staticUrlPlugin(base)],
|
|
105
155
|
|
|
106
156
|
define: { "globalThis.__OLUM_DEV__": "false" },
|
|
107
157
|
build: {
|