-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
70 lines (60 loc) · 2.01 KB
/
build.js
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
import fs from "fs";
import replaceInFiles from "replace-in-files";
const transpiledPathPrefix = ".bos/transpiled/src/npm_package_name";
async function build() {
await replaceInFiles({
files: [`${transpiledPathPrefix}/**/*.jsx`],
from: /export\s+default\s+function[^(]*\((.*)/gms,
to: (_match, rest) =>
`function MainComponent(${rest}\nreturn MainComponent(props, context);`,
});
await replaceInFiles({
files: [`${transpiledPathPrefix}/**/*.jsx`],
from: /^export /,
// NOTE: Empty string is ignored, so we use a function workaround it
to: () => "",
});
// WARNING: Don't allow "imports" in includes as this may lead to undefined
// behavior as replacements are done in parallel and one file may be getting
// replacements saved while the other file needs to include it, which ends up
// with empty content includes.
await new Promise((resolve) => {
fs.rename(
`${transpiledPathPrefix}/includes`,
`${transpiledPathPrefix}/../includes`,
() => {
resolve();
}
);
});
await replaceInFiles({
files: [`${transpiledPathPrefix}/**/*.jsx`],
from: /import .* from "@\/includes\/([^"]*)";/gms,
to: (_match, importPath) => {
const importedFileContent = fs.readFileSync(
`${transpiledPathPrefix}/../includes/${importPath}.jsx`,
"utf8"
);
return `/* INCLUDE: "includes/${importPath}.jsx" */\n${importedFileContent}/* END_INCLUDE: "includes/${importPath}.jsx" */`;
},
});
const packageJson = JSON.parse(
fs.readFileSync(new URL("./package.json", import.meta.url))
);
await replaceInFiles({
files: [`${transpiledPathPrefix}/**/*.jsx`],
from: /^/m,
to: `/*\nLicense: ${packageJson.license}\nAuthor: ${packageJson.author}\nHomepage: ${packageJson.homepage}\n*/\n`,
});
await new Promise((resolve) => {
fs.rename(
transpiledPathPrefix,
`${transpiledPathPrefix}/../${packageJson.name}`,
() => {
resolve();
}
);
});
console.log("DONE");
}
build();