-
Notifications
You must be signed in to change notification settings - Fork 6
/
Prepack.bs.mjs
155 lines (131 loc) · 3.7 KB
/
Prepack.bs.mjs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Fs from "fs";
import * as Execa from "execa";
import * as Rollup from "rollup";
import * as Nodefs from "node:fs";
import * as Nodepath from "node:path";
import * as Core__JSON from "@rescript/core/src/Core__JSON.bs.mjs";
import * as Core__List from "@rescript/core/src/Core__List.bs.mjs";
import * as Core__Option from "@rescript/core/src/Core__Option.bs.mjs";
import PluginReplace from "@rollup/plugin-replace";
var projectPath = ".";
var artifactsPath = Nodepath.join(projectPath, "packages/artifacts");
var sourePaths = [
"package.json",
"node_modules",
"src",
"rescript.json",
"README.md",
"RescriptSchema.gen.ts"
];
var jsInputPath = Nodepath.join(artifactsPath, "src/S.js");
function update(json, path, value) {
var dict = Core__JSON.Decode.object(json);
var dict$1 = dict !== undefined ? Object.assign({}, dict) : ({});
if (!path) {
return value;
}
var path$1 = path.tl;
var key = path.hd;
if (path$1) {
dict$1[key] = update(Core__Option.getOr(dict$1[key], {}), path$1, value);
return dict$1;
} else {
dict$1[key] = value;
return dict$1;
}
}
if (Nodefs.existsSync(artifactsPath)) {
Fs.rmSync(artifactsPath, {
recursive: true,
force: true
});
}
Nodefs.mkdirSync(artifactsPath);
sourePaths.forEach(function (path) {
Fs.cpSync(Nodepath.join(projectPath, path), Nodepath.join(artifactsPath, path), {
recursive: true
});
});
function updateJsonFile(src, path, value) {
var packageJsonData = Nodefs.readFileSync(src, {
encoding: "utf8"
});
var packageJson = JSON.parse(packageJsonData.toString());
var updatedPackageJson = JSON.stringify(update(packageJson, Core__List.fromArray(path), value), undefined, 2);
Nodefs.writeFileSync(src, Buffer.from(updatedPackageJson), {
encoding: "utf8"
});
}
Execa.execaSync("npm", [
"run",
"res:build"
], {
cwd: artifactsPath
});
var bundle = await Rollup.rollup({
input: jsInputPath,
external: [/S_Core\.bs\.mjs/]
});
var output = [
{
file: Nodepath.join(artifactsPath, "dist/S.js"),
format: "cjs",
exports: "named",
plugins: [PluginReplace({
values: Object.fromEntries([
[
"S_Core.bs.mjs",
"../src/S_Core.bs.js"
],
[
"rescript/lib/es6",
"rescript/lib/js"
]
])
})]
},
{
file: Nodepath.join(artifactsPath, "dist/S.mjs"),
format: "es",
exports: "named",
plugins: [PluginReplace({
values: Object.fromEntries([[
"S_Core.bs.mjs",
"../src/S_Core.bs.mjs"
]])
})]
}
];
for(var idx = 0 ,idx_finish = output.length; idx < idx_finish; ++idx){
var outpuOptions = output[idx];
await bundle.write(outpuOptions);
}
await bundle.close();
Fs.rmSync(Nodepath.join(artifactsPath, "lib"), {
recursive: true,
force: true
});
updateJsonFile(Nodepath.join(artifactsPath, "rescript.json"), [
"package-specs",
"module"
], "commonjs");
updateJsonFile(Nodepath.join(artifactsPath, "rescript.json"), ["suffix"], ".bs.js");
Execa.execaSync("npm", [
"run",
"res:build"
], {
cwd: artifactsPath
});
updateJsonFile(Nodepath.join(artifactsPath, "package.json"), ["type"], "commonjs");
Fs.rmSync(Nodepath.join(artifactsPath, "lib"), {
recursive: true,
force: true
});
Fs.rmSync(Nodepath.join(artifactsPath, "node_modules"), {
recursive: true,
force: true
});
export {
}
/* artifactsPath Not a pure module */