-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprebuild.js
94 lines (75 loc) · 2.16 KB
/
prebuild.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const babel = require("@babel/core");
const path = require("path");
const fs = require("fs").promises;
const UglifyJS = require("uglify-js");
const { loadEnvConfig } = require("@next/env");
loadEnvConfig(process.cwd());
async function compile() {
console.log("compiling extension script");
let extensionDirectory = path.join(process.cwd(), "extension");
const presets = [
[
"@babel/preset-env",
{
useBuiltIns: "entry",
corejs: "3.22",
},
],
];
const plugins = [
"@babel/plugin-transform-async-to-generator",
"@babel/plugin-transform-template-literals",
];
let unlockScript = await fs.readFile(
extensionDirectory + "/js/unlock.js",
"utf8"
);
({ code: unlockScript } = babel.transform(unlockScript, {
presets,
plugins,
}));
let paywallApp = await fs.readFile(
extensionDirectory + "/htmls/partials/paywall_head.htm",
"utf8"
);
paywallApp = paywallApp.replaceAll(":unlock_script:", unlockScript);
let paywallSectionHTML = await fs.readFile(
extensionDirectory + "/htmls/paywall_section.htm",
"utf8"
);
paywallSectionHTML = paywallApp.replaceAll(
":paywall_html:",
paywallSectionHTML
);
paywallSectionHTML = paywallSectionHTML.replaceAll(
":unlock_script:",
unlockScript
);
let paywallPageHTML = await fs.readFile(
extensionDirectory + "/htmls/paywall_page.htm",
"utf8"
);
paywallPageHTML = paywallApp.replaceAll(":paywall_html:", paywallPageHTML);
let fileContents = await fs.readFile(
extensionDirectory + "/extension.js",
"utf8"
);
fileContents = fileContents.replace(":paywallelem:", paywallSectionHTML);
fileContents = fileContents.replace(":paywallpage:", paywallPageHTML);
fileContents = fileContents.replaceAll(":API_URL:", process.env.API_URL);
const { code: extension } = babel.transform(fileContents, {
presets,
plugins,
});
let extensionMinify = UglifyJS.minify(extension);
fs.writeFile(
extensionDirectory + "/compiled/paywall.js",
extensionMinify.code,
function (err) {
if (err) throw err;
console.log("Saved!");
}
);
return "built";
}
compile().then(console.log);