@gjsify/cli 0.3.13 → 0.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/actions/build.d.ts +14 -93
- package/lib/actions/build.js +182 -154
- package/lib/commands/flatpak/build.d.ts +16 -0
- package/lib/commands/flatpak/build.js +187 -0
- package/lib/commands/flatpak/ci.d.ts +13 -0
- package/lib/commands/flatpak/ci.js +133 -0
- package/lib/commands/flatpak/deps.d.ts +12 -0
- package/lib/commands/flatpak/deps.js +96 -0
- package/lib/commands/flatpak/index.d.ts +7 -0
- package/lib/commands/flatpak/index.js +23 -0
- package/lib/commands/flatpak/init.d.ts +15 -0
- package/lib/commands/flatpak/init.js +154 -0
- package/lib/commands/flatpak/utils.d.ts +32 -0
- package/lib/commands/flatpak/utils.js +63 -0
- package/lib/commands/gsettings.d.ts +9 -0
- package/lib/commands/gsettings.js +72 -0
- package/lib/commands/index.d.ts +2 -0
- package/lib/commands/index.js +2 -0
- package/lib/commands/install.d.ts +1 -0
- package/lib/commands/install.js +66 -11
- package/lib/config.js +103 -11
- package/lib/index.js +3 -1
- package/lib/types/cli-build-options.d.ts +1 -1
- package/lib/types/config-data.d.ts +194 -4
- package/lib/utils/install-global.d.ts +54 -0
- package/lib/utils/install-global.js +153 -0
- package/lib/utils/normalize-bundler-options.d.ts +17 -0
- package/lib/utils/normalize-bundler-options.js +123 -0
- package/lib/utils/resolve-plugin-by-name.d.ts +21 -0
- package/lib/utils/resolve-plugin-by-name.js +75 -0
- package/package.json +11 -11
- package/src/actions/build.ts +406 -352
- package/src/commands/flatpak/build.ts +225 -0
- package/src/commands/flatpak/ci.ts +173 -0
- package/src/commands/flatpak/deps.ts +120 -0
- package/src/commands/flatpak/index.ts +53 -0
- package/src/commands/flatpak/init.ts +191 -0
- package/src/commands/flatpak/utils.ts +76 -0
- package/src/commands/gsettings.ts +87 -0
- package/src/commands/index.ts +2 -0
- package/src/commands/install.ts +90 -11
- package/src/config.ts +103 -11
- package/src/index.ts +4 -0
- package/src/types/cli-build-options.ts +1 -1
- package/src/types/config-data.ts +191 -4
- package/src/utils/install-global.ts +182 -0
- package/src/utils/normalize-bundler-options.ts +129 -0
- package/src/utils/resolve-plugin-by-name.ts +106 -0
package/src/actions/build.ts
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import type { ConfigData } from "../types/index.js";
|
|
2
|
-
import type { App } from "@gjsify/
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import type { ConfigData, BundlerOptions } from "../types/index.js";
|
|
2
|
+
import type { App, PluginOptions } from "@gjsify/rolldown-plugin-gjsify";
|
|
3
|
+
import type { RolldownOutput, RolldownPluginOption } from "rolldown";
|
|
4
|
+
import { rolldown } from "rolldown";
|
|
5
|
+
import { gjsifyPlugin, textLoaderPlugin, resolveShebangLine } from "@gjsify/rolldown-plugin-gjsify";
|
|
6
|
+
import { resolveUserPlugins } from "../utils/resolve-plugin-by-name.js";
|
|
5
7
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "@gjsify/
|
|
10
|
-
import {
|
|
11
|
-
import { getPnpPlugin } from "@gjsify/resolve-npm/pnp-relay";
|
|
8
|
+
resolveGlobalsList,
|
|
9
|
+
writeRegisterInjectFile,
|
|
10
|
+
detectAutoGlobals,
|
|
11
|
+
} from "@gjsify/rolldown-plugin-gjsify/globals";
|
|
12
|
+
import { pnpPlugin } from "@gjsify/rolldown-plugin-pnp";
|
|
12
13
|
import { dirname, extname } from "node:path";
|
|
13
14
|
import { chmod, readFile, writeFile } from "node:fs/promises";
|
|
15
|
+
import { normalizeBundlerOptions, mergeBundlerOptions } from "../utils/normalize-bundler-options.js";
|
|
14
16
|
|
|
15
|
-
const
|
|
17
|
+
const DEFAULT_GJS_SHEBANG = "#!/usr/bin/env -S gjs -m";
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* `true` when `path` points at a location that's unsafe to use as a build
|
|
@@ -21,10 +23,10 @@ const GJS_SHEBANG = "#!/usr/bin/env -S gjs -m\n";
|
|
|
21
23
|
* - paths that live under a `src/` segment (relative or absolute)
|
|
22
24
|
*/
|
|
23
25
|
function isUnsafeDefaultOutput(path: string): boolean {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
if (/\.[cm]?tsx?$/i.test(path)) return true;
|
|
27
|
+
const norm = path.replace(/\\/g, "/");
|
|
28
|
+
if (/(?:^|\/)src\//.test(norm)) return true;
|
|
29
|
+
return false;
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/**
|
|
@@ -33,345 +35,397 @@ function isUnsafeDefaultOutput(path: string): boolean {
|
|
|
33
35
|
* @gjsify/{node,web}-polyfills) are resolvable for external consumers without
|
|
34
36
|
* each one having to be a direct devDep.
|
|
35
37
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* inside the pnp plugin's onLoad rather than as a separate registration.
|
|
38
|
+
* The path rewriter (`__filename`/`__dirname` + `import.meta.url` injection
|
|
39
|
+
* for node_modules code) is registered separately by the orchestrator —
|
|
40
|
+
* Rolldown's transform hooks all run sequentially, no shared `onLoad` race.
|
|
40
41
|
*/
|
|
41
|
-
async function buildPnpPlugin(): Promise<
|
|
42
|
-
|
|
43
|
-
issuerUrl: import.meta.url,
|
|
44
|
-
transformContentsFactory: (build) => {
|
|
45
|
-
const bundleDir = getBundleDir(build);
|
|
46
|
-
return (args, contents) => rewriteContents(args, contents, bundleDir);
|
|
47
|
-
},
|
|
48
|
-
});
|
|
42
|
+
async function buildPnpPlugin(): Promise<RolldownPluginOption | null> {
|
|
43
|
+
return pnpPlugin({ issuerUrl: import.meta.url });
|
|
49
44
|
}
|
|
50
45
|
|
|
51
46
|
export class BuildAction {
|
|
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
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
47
|
+
constructor(readonly configData: ConfigData = {}) {}
|
|
48
|
+
|
|
49
|
+
/** Library mode */
|
|
50
|
+
async buildLibrary(): Promise<RolldownOutput[]> {
|
|
51
|
+
const { verbose, library, typescript, exclude, aliases } = this.configData;
|
|
52
|
+
const lib = library ?? {};
|
|
53
|
+
const userBundler = normalizeBundlerOptions(this.configData);
|
|
54
|
+
|
|
55
|
+
const moduleOutdir = lib.module ? dirname(lib.module) : undefined;
|
|
56
|
+
const mainOutdir = lib.main ? dirname(lib.main) : undefined;
|
|
57
|
+
|
|
58
|
+
const moduleOutExt = lib.module ? extname(lib.module) : ".js";
|
|
59
|
+
const mainOutExt = lib.main ? extname(lib.main) : ".js";
|
|
60
|
+
|
|
61
|
+
const multipleBuilds =
|
|
62
|
+
moduleOutdir && mainOutdir && moduleOutdir !== mainOutdir;
|
|
63
|
+
|
|
64
|
+
const pnp = await buildPnpPlugin();
|
|
65
|
+
const pnpPlugins: RolldownPluginOption[] = pnp ? [pnp] : [];
|
|
66
|
+
|
|
67
|
+
const results: RolldownOutput[] = [];
|
|
68
|
+
|
|
69
|
+
if (multipleBuilds) {
|
|
70
|
+
const moduleFormat: "esm" | "cjs" =
|
|
71
|
+
moduleOutdir.includes("/cjs") || moduleOutExt === ".cjs"
|
|
72
|
+
? "cjs"
|
|
73
|
+
: "esm";
|
|
74
|
+
results.push(
|
|
75
|
+
await runOneLibraryBuild({
|
|
76
|
+
pluginOpts: {
|
|
77
|
+
debug: verbose,
|
|
78
|
+
library: moduleFormat,
|
|
79
|
+
exclude,
|
|
80
|
+
reflection: typescript?.reflection,
|
|
81
|
+
jsExtension: moduleOutExt,
|
|
82
|
+
},
|
|
83
|
+
userBundler,
|
|
84
|
+
output: { dir: moduleOutdir },
|
|
85
|
+
userAliases: aliases,
|
|
86
|
+
pnpPlugins,
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const mainFormat: "esm" | "cjs" =
|
|
91
|
+
mainOutdir.includes("/cjs") || mainOutExt === ".cjs" ? "cjs" : "esm";
|
|
92
|
+
results.push(
|
|
93
|
+
await runOneLibraryBuild({
|
|
94
|
+
pluginOpts: {
|
|
95
|
+
debug: verbose,
|
|
96
|
+
library: mainFormat,
|
|
97
|
+
exclude,
|
|
98
|
+
reflection: typescript?.reflection,
|
|
99
|
+
jsExtension: mainOutExt,
|
|
100
|
+
},
|
|
101
|
+
userBundler,
|
|
102
|
+
output: { dir: mainOutdir },
|
|
103
|
+
userAliases: aliases,
|
|
104
|
+
pnpPlugins,
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
} else {
|
|
108
|
+
const outfilePath =
|
|
109
|
+
userBundler.output?.file ?? lib.module ?? lib.main;
|
|
110
|
+
const outExt = outfilePath ? extname(outfilePath) : ".js";
|
|
111
|
+
const outdir =
|
|
112
|
+
userBundler.output?.dir ?? (outfilePath ? dirname(outfilePath) : undefined);
|
|
113
|
+
const format: "esm" | "cjs" =
|
|
114
|
+
(userBundler.output?.format as "esm" | "cjs" | undefined) ??
|
|
115
|
+
(outdir?.includes("/cjs") || outExt === ".cjs" ? "cjs" : "esm");
|
|
116
|
+
results.push(
|
|
117
|
+
await runOneLibraryBuild({
|
|
118
|
+
pluginOpts: {
|
|
119
|
+
debug: verbose,
|
|
120
|
+
library: format,
|
|
121
|
+
exclude,
|
|
122
|
+
reflection: typescript?.reflection,
|
|
123
|
+
jsExtension: outExt,
|
|
124
|
+
},
|
|
125
|
+
userBundler,
|
|
126
|
+
output: { dir: outdir },
|
|
127
|
+
userAliases: aliases,
|
|
128
|
+
pnpPlugins,
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return results;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Parse the `--globals` value into { autoMode, extras }.
|
|
137
|
+
* - `auto` → { autoMode: true, extras: '' }
|
|
138
|
+
* - `auto,dom` → { autoMode: true, extras: 'dom' }
|
|
139
|
+
* - `auto,dom,fetch` → { autoMode: true, extras: 'dom,fetch' }
|
|
140
|
+
* - `dom,fetch` → { autoMode: false, extras: 'dom,fetch' }
|
|
141
|
+
* - `none` / `` → { autoMode: false, extras: '' }
|
|
142
|
+
* - `undefined` → { autoMode: true, extras: '' } (default)
|
|
143
|
+
*/
|
|
144
|
+
private parseGlobalsValue(value: string | undefined): {
|
|
145
|
+
autoMode: boolean;
|
|
146
|
+
extras: string;
|
|
147
|
+
} {
|
|
148
|
+
if (value === undefined) return { autoMode: true, extras: "" };
|
|
149
|
+
if (value === "none" || value === "")
|
|
150
|
+
return { autoMode: false, extras: "" };
|
|
151
|
+
|
|
152
|
+
const tokens = value
|
|
153
|
+
.split(",")
|
|
154
|
+
.map((t) => t.trim())
|
|
155
|
+
.filter(Boolean);
|
|
156
|
+
const hasAuto = tokens.includes("auto");
|
|
157
|
+
const extras = tokens.filter((t) => t !== "auto").join(",");
|
|
158
|
+
|
|
159
|
+
return { autoMode: hasAuto, extras };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Resolve the `--globals` CLI list into a pre-computed inject stub path
|
|
164
|
+
* that the orchestrator appends to its input list. Only runs for
|
|
165
|
+
* `--app gjs` — Node and browser builds rely on native globals.
|
|
166
|
+
*
|
|
167
|
+
* Used only for the explicit-only path (no `auto` token in the value).
|
|
168
|
+
* The auto path is handled in `buildApp` via the iterative multi-pass build.
|
|
169
|
+
*/
|
|
170
|
+
private async resolveGlobalsInject(
|
|
171
|
+
app: App,
|
|
172
|
+
globals: string,
|
|
173
|
+
verbose: boolean | undefined,
|
|
174
|
+
): Promise<string | undefined> {
|
|
175
|
+
if (app !== "gjs") return undefined;
|
|
176
|
+
if (!globals) return undefined;
|
|
177
|
+
|
|
178
|
+
const registerPaths = resolveGlobalsList(globals);
|
|
179
|
+
if (registerPaths.size === 0) return undefined;
|
|
180
|
+
|
|
181
|
+
const injectPath = await writeRegisterInjectFile(
|
|
182
|
+
registerPaths,
|
|
183
|
+
process.cwd(),
|
|
184
|
+
);
|
|
185
|
+
if (verbose && injectPath) {
|
|
186
|
+
console.debug(
|
|
187
|
+
`[gjsify] globals: injected ${registerPaths.size} register module(s) from --globals ${globals}`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
return injectPath ?? undefined;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Post-processing: prepend the resolved shebang line and mark the
|
|
195
|
+
* output executable. Only runs for GJS app builds with a single outfile.
|
|
196
|
+
* The shebang plugin in `@gjsify/rolldown-plugin-gjsify` already injects
|
|
197
|
+
* during bundling — this hook is the safety net for anything that
|
|
198
|
+
* bypassed the plugin (e.g. user-supplied banners that out-ordered it),
|
|
199
|
+
* plus the chmod.
|
|
200
|
+
*/
|
|
201
|
+
private async applyShebang(
|
|
202
|
+
outfile: string | undefined,
|
|
203
|
+
verbose: boolean | undefined,
|
|
204
|
+
): Promise<void> {
|
|
205
|
+
if (!outfile) {
|
|
206
|
+
if (verbose)
|
|
207
|
+
console.warn(
|
|
208
|
+
"[gjsify] --shebang skipped: no single outfile (use --outfile for GJS executables)",
|
|
209
|
+
);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const line = resolveShebangLine(this.configData.shebang) ?? DEFAULT_GJS_SHEBANG;
|
|
214
|
+
|
|
215
|
+
const content = await readFile(outfile, "utf-8");
|
|
216
|
+
if (content.startsWith("#!")) {
|
|
217
|
+
if (verbose)
|
|
218
|
+
console.debug(
|
|
219
|
+
`[gjsify] --shebang skipped: ${outfile} already starts with a shebang`,
|
|
220
|
+
);
|
|
221
|
+
} else {
|
|
222
|
+
await writeFile(outfile, line + "\n" + content);
|
|
223
|
+
}
|
|
224
|
+
await chmod(outfile, 0o755);
|
|
225
|
+
if (verbose)
|
|
226
|
+
console.debug(
|
|
227
|
+
`[gjsify] --shebang: wrote ${line} + chmod 0o755 to ${outfile}`,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Application mode */
|
|
232
|
+
async buildApp(app: App = "gjs"): Promise<RolldownOutput[]> {
|
|
233
|
+
const {
|
|
234
|
+
verbose,
|
|
235
|
+
typescript,
|
|
236
|
+
exclude,
|
|
237
|
+
library: pkg,
|
|
238
|
+
aliases,
|
|
239
|
+
excludeGlobals,
|
|
240
|
+
} = this.configData;
|
|
241
|
+
|
|
242
|
+
const userBundler = normalizeBundlerOptions(this.configData);
|
|
243
|
+
|
|
244
|
+
const formatRaw =
|
|
245
|
+
(userBundler.output?.format as "esm" | "cjs" | "iife" | undefined) ??
|
|
246
|
+
(userBundler.output?.file?.endsWith(".cjs") ? "cjs" : "esm");
|
|
247
|
+
// The orchestrator only handles esm/cjs (iife is not a GJS / Node /
|
|
248
|
+
// browser-bundle target we support). Coerce.
|
|
249
|
+
const format: "esm" | "cjs" = formatRaw === "iife" ? "esm" : formatRaw;
|
|
250
|
+
|
|
251
|
+
// Set default outfile if no outdir is set
|
|
252
|
+
let outfile = userBundler.output?.file;
|
|
253
|
+
let outdir = userBundler.output?.dir;
|
|
254
|
+
if (!outfile && !outdir && (pkg?.main || pkg?.module)) {
|
|
255
|
+
const candidate =
|
|
256
|
+
format === "cjs"
|
|
257
|
+
? pkg.main ?? pkg.module
|
|
258
|
+
: pkg.module ?? pkg.main;
|
|
259
|
+
if (candidate && isUnsafeDefaultOutput(candidate)) {
|
|
260
|
+
throw new Error(
|
|
261
|
+
`gjsify build: refusing to default --outfile to ${candidate} ` +
|
|
262
|
+
`(would overwrite a TypeScript source file). Pass --outfile/--outdir ` +
|
|
263
|
+
`explicitly, or set "gjsify.bundler.output.file" in package.json.`,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
outfile = candidate;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const { consoleShim, globals } = this.configData;
|
|
270
|
+
|
|
271
|
+
const userExternal = Array.isArray(userBundler.external)
|
|
272
|
+
? (userBundler.external as string[])
|
|
273
|
+
: undefined;
|
|
274
|
+
const userBanner = typeof userBundler.output?.banner === "string"
|
|
275
|
+
? (userBundler.output.banner as string)
|
|
276
|
+
: undefined;
|
|
277
|
+
|
|
278
|
+
const pluginOpts: PluginOptions = {
|
|
279
|
+
debug: verbose,
|
|
280
|
+
app,
|
|
281
|
+
format,
|
|
282
|
+
exclude,
|
|
283
|
+
reflection: typescript?.reflection,
|
|
284
|
+
consoleShim,
|
|
285
|
+
...(aliases ? { aliases } : {}),
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const { autoMode, extras } = this.parseGlobalsValue(globals);
|
|
289
|
+
|
|
290
|
+
const pnp = await buildPnpPlugin();
|
|
291
|
+
const pnpPlugins: RolldownPluginOption[] = pnp ? [pnp] : [];
|
|
292
|
+
|
|
293
|
+
// User-supplied text loaders need to be available during BOTH the
|
|
294
|
+
// auto-globals pre-build (`detectAutoGlobals`) and the final build —
|
|
295
|
+
// otherwise Rolldown's parser hits unknown extensions like `.ui` /
|
|
296
|
+
// `.asm` during the pre-build, fails to parse them as JS/JSX, and
|
|
297
|
+
// the auto-globals iteration aborts before the final plugin chain is
|
|
298
|
+
// ever assembled. Build the user-plugin chain once, up front, and
|
|
299
|
+
// pass it into both passes.
|
|
300
|
+
const userTextLoader = textLoaderPlugin({ loaders: this.configData.loaders });
|
|
301
|
+
const userPlugins: RolldownPluginOption[] = userTextLoader ? [userTextLoader] : [];
|
|
302
|
+
|
|
303
|
+
// User-supplied bundler.plugins (mix of plugin objects + by-name
|
|
304
|
+
// entries) — resolved from the project's node_modules. Same
|
|
305
|
+
// ordering rationale as the text loader: must be present during
|
|
306
|
+
// auto-globals pre-build to avoid claiming the same files via
|
|
307
|
+
// Rolldown's default classifier.
|
|
308
|
+
if (userBundler.plugins?.length) {
|
|
309
|
+
const resolved = await resolveUserPlugins(userBundler.plugins, process.cwd());
|
|
310
|
+
userPlugins.push(...resolved);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// --- Auto mode (with optional extras): iterative multi-pass build ---
|
|
314
|
+
if (app === "gjs" && autoMode) {
|
|
315
|
+
const gjsifyPluginFactory = async (opts: PluginOptions) => {
|
|
316
|
+
const cfg = await gjsifyPlugin(
|
|
317
|
+
{
|
|
318
|
+
input: userBundler.input,
|
|
319
|
+
output: { file: outfile, dir: outdir },
|
|
320
|
+
userExternal,
|
|
321
|
+
userBanner,
|
|
322
|
+
userAliases: aliases,
|
|
323
|
+
shebang: this.configData.shebang,
|
|
324
|
+
},
|
|
325
|
+
opts,
|
|
326
|
+
);
|
|
327
|
+
return cfg.plugins;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const { injectPath } = await detectAutoGlobals(
|
|
331
|
+
{
|
|
332
|
+
input: userBundler.input,
|
|
333
|
+
plugins: [...pnpPlugins, ...userPlugins],
|
|
334
|
+
external: userBundler.external,
|
|
335
|
+
transform: userBundler.transform,
|
|
336
|
+
format,
|
|
337
|
+
},
|
|
338
|
+
pluginOpts,
|
|
339
|
+
gjsifyPluginFactory,
|
|
340
|
+
verbose,
|
|
341
|
+
{ extraGlobalsList: extras, excludeGlobals },
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
pluginOpts.autoGlobalsInject = injectPath;
|
|
345
|
+
} else if (extras) {
|
|
346
|
+
pluginOpts.autoGlobalsInject = await this.resolveGlobalsInject(
|
|
347
|
+
app,
|
|
348
|
+
extras,
|
|
349
|
+
verbose,
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Final build: orchestrator → rolldown → write
|
|
354
|
+
const cfg = await gjsifyPlugin(
|
|
355
|
+
{
|
|
356
|
+
input: userBundler.input,
|
|
357
|
+
output: { file: outfile, dir: outdir },
|
|
358
|
+
userExternal,
|
|
359
|
+
userBanner,
|
|
360
|
+
userAliases: aliases,
|
|
361
|
+
shebang: this.configData.shebang,
|
|
362
|
+
},
|
|
363
|
+
pluginOpts,
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
const merged = mergeBundlerOptions(cfg.options as BundlerOptions, userBundler);
|
|
367
|
+
|
|
368
|
+
const finalOpts: BundlerOptions = {
|
|
369
|
+
...merged,
|
|
370
|
+
// Drop user-config plugins from `merged` — they survived
|
|
371
|
+
// mergeBundlerOptions via spread but have already been resolved
|
|
372
|
+
// and appended into `userPlugins` above. Re-emitting the raw
|
|
373
|
+
// entries (which may include `BundlerPluginByName` shapes
|
|
374
|
+
// Rolldown doesn't understand) would crash the build.
|
|
375
|
+
plugins: [...pnpPlugins, ...userPlugins, ...cfg.plugins],
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const build = await rolldown(finalOpts);
|
|
379
|
+
let writeResult: RolldownOutput;
|
|
380
|
+
try {
|
|
381
|
+
writeResult = await build.write(finalOpts.output ?? {});
|
|
382
|
+
} finally {
|
|
383
|
+
await build.close();
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (app === "gjs" && this.configData.shebang) {
|
|
387
|
+
await this.applyShebang(outfile, verbose);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return [writeResult];
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async start(buildType: { library?: boolean; app?: App } = { app: "gjs" }) {
|
|
394
|
+
if (buildType.library) {
|
|
395
|
+
return await this.buildLibrary();
|
|
396
|
+
}
|
|
397
|
+
return await this.buildApp(buildType.app);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
interface OneLibraryBuildArgs {
|
|
402
|
+
pluginOpts: PluginOptions;
|
|
403
|
+
userBundler: BundlerOptions;
|
|
404
|
+
output: { file?: string; dir?: string };
|
|
405
|
+
userAliases?: Record<string, string>;
|
|
406
|
+
pnpPlugins: RolldownPluginOption[];
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async function runOneLibraryBuild(args: OneLibraryBuildArgs): Promise<RolldownOutput> {
|
|
410
|
+
const cfg = await gjsifyPlugin(
|
|
411
|
+
{
|
|
412
|
+
input: args.userBundler.input,
|
|
413
|
+
output: args.output,
|
|
414
|
+
userAliases: args.userAliases,
|
|
415
|
+
},
|
|
416
|
+
args.pluginOpts,
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
const merged = mergeBundlerOptions(cfg.options as BundlerOptions, args.userBundler);
|
|
420
|
+
const finalOpts: BundlerOptions = {
|
|
421
|
+
...merged,
|
|
422
|
+
plugins: [...args.pnpPlugins, ...cfg.plugins],
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
const build = await rolldown(finalOpts);
|
|
426
|
+
try {
|
|
427
|
+
return await build.write(finalOpts.output ?? {});
|
|
428
|
+
} finally {
|
|
429
|
+
await build.close();
|
|
430
|
+
}
|
|
377
431
|
}
|