-
Notifications
You must be signed in to change notification settings - Fork 6
/
Prepack.res
203 lines (178 loc) · 5.32 KB
/
Prepack.res
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
let projectPath = "."
let artifactsPath = NodeJs.Path.join2(projectPath, "packages/artifacts")
let sourePaths = [
"package.json",
"node_modules",
"src",
"rescript.json",
"README.md",
"RescriptSchema.gen.ts",
]
let jsInputPath = NodeJs.Path.join2(artifactsPath, "src/S.js")
module Stdlib = {
module Dict = {
@val
external copy: (@as(json`{}`) _, Dict.t<'a>) => Dict.t<'a> = "Object.assign"
}
module Json = {
let rec update = (json, path, value) => {
let dict = switch json->JSON.Decode.object {
| Some(dict) => dict->Dict.copy
| None => RescriptCore.Dict.make()
}
switch path {
| list{} => value
| list{key} => {
dict->RescriptCore.Dict.set(key, value)
dict->JSON.Encode.object
}
| list{key, ...path} => {
dict->RescriptCore.Dict.set(
key,
dict
->RescriptCore.Dict.get(key)
->Option.getOr(RescriptCore.Dict.make()->JSON.Encode.object)
->update(path, value),
)
dict->JSON.Encode.object
}
}
}
}
}
module Execa = {
type returnValue = {stdout: string}
type options = {env?: Dict.t<string>, cwd?: string}
@module("execa")
external sync: (string, array<string>, ~options: options=?, unit) => returnValue = "execaSync"
}
module FsX = {
type rmSyncOptions = {recursive?: bool, force?: bool}
@module("fs") external rmSync: (string, rmSyncOptions) => unit = "rmSync"
type cpSyncOptions = {recursive?: bool}
@module("fs") external cpSync: (~src: string, ~dest: string, cpSyncOptions) => unit = "cpSync"
}
module Rollup = {
type internalModuleFormat = [#amd | #cjs | #es | #iife | #system | #umd]
type moduleFormat = [internalModuleFormat | #commonjs | #esm | #"module" | #systemjs]
module Plugin = {
type t
}
module ReplacePlugin = {
type options = {values: Dict.t<string>}
@module("@rollup/plugin-replace") external make: options => Plugin.t = "default"
}
module InputOptions = {
type t = {
input?: string,
@as("external")
external_?: array<Re.t>,
}
}
module OutputOptions = {
type t = {
// only needed for Bundle.write
dir?: string,
// only needed for Bundle.write
file?: string,
format?: moduleFormat,
exports?: [#default | #named | #none | #auto],
plugins?: array<Plugin.t>,
}
}
module Output = {
type t
}
module Bundle = {
type t
@module("rollup")
external make: InputOptions.t => promise<t> = "rollup"
@send
external write: (t, OutputOptions.t) => promise<Output.t> = "write"
@send
external close: t => promise<unit> = "close"
}
}
if NodeJs.Fs.existsSync(artifactsPath) {
FsX.rmSync(artifactsPath, {recursive: true, force: true})
}
NodeJs.Fs.mkdirSync(artifactsPath)
sourePaths->Array.forEach(path => {
FsX.cpSync(
~src=NodeJs.Path.join2(projectPath, path),
~dest=NodeJs.Path.join2(artifactsPath, path),
{recursive: true},
)
})
let updateJsonFile = (~src, ~path, ~value) => {
let packageJsonData = NodeJs.Fs.readFileSyncWith(
src,
{
encoding: "utf8",
},
)
let packageJson = packageJsonData->NodeJs.Buffer.toString->JSON.parseExn
let updatedPackageJson =
packageJson->Stdlib.Json.update(path->List.fromArray, value)->JSON.stringify(~space=2)
NodeJs.Fs.writeFileSyncWith(
src,
updatedPackageJson->NodeJs.Buffer.fromString,
{
encoding: "utf8",
},
)
}
let _ = Execa.sync("npm", ["run", "res:build"], ~options={cwd: artifactsPath}, ())
let bundle = await Rollup.Bundle.make({input: jsInputPath, external_: [%re("/S_Core\.bs\.mjs/")]})
let output: array<Rollup.OutputOptions.t> = [
{
file: NodeJs.Path.join2(artifactsPath, "dist/S.js"),
format: #cjs,
exports: #named,
plugins: [
Rollup.ReplacePlugin.make({
values: Dict.fromArray([
(`S_Core.bs.mjs`, `../src/S_Core.bs.js`),
(`rescript/lib/es6`, `rescript/lib/js`),
]),
}),
],
},
{
file: NodeJs.Path.join2(artifactsPath, "dist/S.mjs"),
format: #es,
exports: #named,
plugins: [
Rollup.ReplacePlugin.make({
values: Dict.fromArray([(`S_Core.bs.mjs`, `../src/S_Core.bs.mjs`)]),
}),
],
},
]
for idx in 0 to output->Array.length - 1 {
let outpuOptions = output->Array.getUnsafe(idx)
let _ = await bundle->Rollup.Bundle.write(outpuOptions)
}
await bundle->Rollup.Bundle.close
// Clean up rescript artifacts so the compiled .bs.js files aren't removed on the .bs.mjs build
FsX.rmSync(NodeJs.Path.join2(artifactsPath, "lib"), {force: true, recursive: true})
updateJsonFile(
~src=NodeJs.Path.join2(artifactsPath, "rescript.json"),
~path=["package-specs", "module"],
~value=JSON.Encode.string("commonjs"),
)
updateJsonFile(
~src=NodeJs.Path.join2(artifactsPath, "rescript.json"),
~path=["suffix"],
~value=JSON.Encode.string(".bs.js"),
)
let _ = Execa.sync("npm", ["run", "res:build"], ~options={cwd: artifactsPath}, ())
updateJsonFile(
~src=NodeJs.Path.join2(artifactsPath, "package.json"),
~path=["type"],
~value=JSON.Encode.string("commonjs"),
)
// Clean up before uploading artifacts
FsX.rmSync(NodeJs.Path.join2(artifactsPath, "lib"), {force: true, recursive: true})
FsX.rmSync(NodeJs.Path.join2(artifactsPath, "node_modules"), {force: true, recursive: true})
// TODO: Generate RescriptSchema.gen.ts