-
Notifications
You must be signed in to change notification settings - Fork 7
/
publish.mts
126 lines (112 loc) · 3.11 KB
/
publish.mts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import fs from "fs";
import path from "path";
import glob from "fast-glob";
const HOOK = process.argv.at(-1) === "pre" ? "pre" : "post";
/**
* Updates the `marko.json` fields between src & dist.
*/
await updateText("marko.json", async (src) =>
HOOK === "pre"
? src.replace(/\/src\//g, "/dist/")
: src.replace(/\/dist\//g, "/src/")
);
/**
* Generates the `exports` field in the `package.json`.
* This includes auto adding conditions to load `.mjs` vs `.js` and
* supports the `browser` condition by looking for `-browser.ts` files.
*/
await updateJSON("package.json", async (data) => {
if (HOOK === "post") {
return {
...data,
exports: undefined,
};
}
const exportMap: any = {
"./marko.json": "./marko.json",
};
const files = glob.stream([
"dist/**",
"!*.d.ts",
"!**/__tests__",
"!**/*.tsbuildinfo",
]) as AsyncIterable<string>;
for await (const file of files) {
const relativeFile = `./${file}`;
const ext = path.extname(file);
switch (ext) {
case ".js": {
const base = relativeFile.slice(0, -ext.length);
const jsFile = `${base}.js`;
const conditions = {
import: `${base}.mjs`,
default: jsFile,
};
const prev = exportMap[base];
exportMap[base] = exportMap[jsFile] = prev
? Object.assign(prev, conditions)
: conditions;
if (prev) {
exportMap[base] = exportMap[jsFile] = Object.assign(prev, conditions);
continue;
}
exportMap[base] = exportMap[jsFile] = conditions;
switch (path.basename(file, ext)) {
case "index":
exportMap[base.slice(0, -"/index".length)] = conditions;
break;
case "index-browser": {
const parentBase = base.slice(0, -"-browser".length);
const parentJSFile = `${parentBase}.js`;
const parentDir = parentBase.slice(0, -"/index".length);
const parentPrev = exportMap[parentBase];
if (parentPrev) {
exportMap[parentBase] =
exportMap[parentJSFile] =
exportMap[parentDir] =
{
browser: conditions,
...(parentPrev as object),
};
} else {
exportMap[parentBase] =
exportMap[parentJSFile] =
exportMap[parentDir] =
{
browser: conditions,
};
}
break;
}
}
break;
}
case ".mjs":
break;
default:
exportMap[relativeFile] = relativeFile;
break;
}
}
return {
...data,
exports: exportMap,
};
});
async function updateJSON(
file: string,
update: (val: object) => Promise<unknown>
) {
await updateText(file, async (src) =>
JSON.stringify(await update(JSON.parse(src)), null, 2)
);
}
async function updateText(
file: string,
update: (val: string) => Promise<string>
) {
await fs.promises.writeFile(
file,
await update(await fs.promises.readFile(file, "utf-8"))
);
}