-
Notifications
You must be signed in to change notification settings - Fork 0
/
elementos.js
executable file
·122 lines (104 loc) · 2.55 KB
/
elementos.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
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
#!/usr/bin/env node
const spawnCallback = require("cross-spawn").spawn;
const terser = require("terser");
const elements = 'src/CustomElements/Reto.elm';
const fs = require("fs-extra");//.promises;
const path = require("path");
const cwd = process.cwd()
process.on("unhandledRejection", (error) => {
console.error(error);
process.exit(1);
});
// ok rolo
async function elmToEsm(elmPath) {
const elmEs3 = await fs.readFile(elmPath, "utf8");
return (
"\n" +
"const scope = {};\n" +
elmEs3.replace("}(this));", "}(scope));") +
"export const { Elm } = scope;\n" +
"\n"
);
}
async function compileElmCustomElements() {
const outputPath = `static/ce.js`;
await spawnElmMake(elements, outputPath);
const elmEsmContent = await elmToEsm(path.join(cwd, outputPath));
const elmFileOutput = await runTerserNew(elmEsmContent);
await fs.writeFile(path.join(process.cwd(), outputPath), elmFileOutput);
}
// ok rolo
function spawnElmMake(elmEntrypointPath, outputPath, cwd) {
return new Promise((resolve, reject) => {
const fullOutputPath = cwd ? path.join(cwd, outputPath) : outputPath;
if (fs.existsSync(fullOutputPath)) {
fs.rmSync(fullOutputPath, {
force: true /* ignore errors if file doesn't exist */,
});
}
const subprocess = runElm(elmEntrypointPath, outputPath, cwd)
subprocess.on("close", (code) => {
const fileOutputExists = fs.existsSync(fullOutputPath);
if (code == 0 && fileOutputExists) {
resolve();
} else {
reject();
process.exit(1);
}
});
});
}
// ok rolo
function runElm(elmEntrypointPath, outputPath, cwd) {
return spawnCallback(
`elm-optimize-level-2`,
[elmEntrypointPath, "--output", outputPath],
{
// ignore stdout
stdio: ["inherit", "ignore", "inherit"],
cwd: cwd,
}
);
}
// ok rolo
async function runTerserNew(fileContents) {
const minifiedElm = await terser.minify(fileContents, {
ecma: 5,
module: true,
compress: {
pure_funcs: [
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"A2",
"A3",
"A4",
"A5",
"A6",
"A7",
"A8",
"A9",
],
pure_getters: true,
keep_fargs: false,
unsafe_comps: true,
unsafe: true,
},
mangle: true,
});
const code = minifiedElm.code;
if (code) {
return code;
} else {
throw "Error running terser.";
}
}
async function run() {
compileElmCustomElements();
}
run();