@gjsify/cli 0.3.14 → 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 +6 -2
- package/lib/actions/build.js +37 -8
- 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 +54 -2
- package/lib/index.js +3 -1
- package/lib/types/config-data.d.ts +145 -4
- package/lib/utils/install-global.d.ts +54 -0
- package/lib/utils/install-global.js +153 -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 +10 -10
- package/src/actions/build.ts +41 -8
- 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 +58 -2
- package/src/index.ts +4 -0
- package/src/types/config-data.ts +142 -4
- package/src/utils/install-global.ts +182 -0
- package/src/utils/resolve-plugin-by-name.ts +106 -0
package/src/types/config-data.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
|
-
import type { RolldownOptions, OutputOptions } from 'rolldown';
|
|
1
|
+
import type { RolldownOptions, OutputOptions, RolldownPluginOption } from 'rolldown';
|
|
2
2
|
import type { ConfigDataLibrary, ConfigDataTypescript } from './index.js';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Plugin entry resolvable by package name from the project's `node_modules`.
|
|
6
|
+
* Lets users describe the plugin chain in `package.json#gjsify` without
|
|
7
|
+
* dropping to a JS-form config file. The CLI imports the named module,
|
|
8
|
+
* picks the chosen export (defaults to `default`), and calls it with
|
|
9
|
+
* `options`.
|
|
10
|
+
*
|
|
11
|
+
* Example:
|
|
12
|
+
* ```jsonc
|
|
13
|
+
* { "name": "@gjsify/vite-plugin-blueprint", "options": { "minify": true } }
|
|
14
|
+
* { "name": "@gjsify/vite-plugin-gettext", "export": "msgfmtPlugin", "options": { ... } }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export interface BundlerPluginByName {
|
|
18
|
+
name: string;
|
|
19
|
+
export?: string;
|
|
20
|
+
options?: unknown;
|
|
21
|
+
}
|
|
22
|
+
|
|
4
23
|
/**
|
|
5
24
|
* Subset of `RolldownOptions` accepted in `.gjsifyrc.js`. Mirrors the legacy
|
|
6
25
|
* `esbuild?: BuildOptions` field — a thin pass-through. The orchestrator
|
|
@@ -10,9 +29,14 @@ import type { ConfigDataLibrary, ConfigDataTypescript } from './index.js';
|
|
|
10
29
|
* `output` is constrained to a single `OutputOptions` object (Rolldown also
|
|
11
30
|
* accepts an array for multi-output builds, but the CLI surface targets the
|
|
12
31
|
* single-output use case).
|
|
32
|
+
*
|
|
33
|
+
* `plugins` is widened to also accept `BundlerPluginByName` entries — these
|
|
34
|
+
* are resolved by the CLI from the project's `node_modules` before the
|
|
35
|
+
* Rolldown call.
|
|
13
36
|
*/
|
|
14
|
-
export type BundlerOptions = Omit<RolldownOptions, 'output'> & {
|
|
37
|
+
export type BundlerOptions = Omit<RolldownOptions, 'output' | 'plugins'> & {
|
|
15
38
|
output?: OutputOptions;
|
|
39
|
+
plugins?: Array<RolldownPluginOption | BundlerPluginByName>;
|
|
16
40
|
};
|
|
17
41
|
|
|
18
42
|
/**
|
|
@@ -69,9 +93,21 @@ export interface ConfigData {
|
|
|
69
93
|
*/
|
|
70
94
|
globals?: string;
|
|
71
95
|
/**
|
|
72
|
-
* Prepend
|
|
96
|
+
* Prepend a shebang to the output bundle and mark it executable.
|
|
97
|
+
*
|
|
98
|
+
* `true` → use the default `#!/usr/bin/env -S gjs -m` line
|
|
99
|
+
* `false` → no shebang (default)
|
|
100
|
+
* `"…"` → custom line. Supports `${env:NAME}` and `${env:NAME:-default}`
|
|
101
|
+
* placeholders against `process.env`. The leading `#!` is
|
|
102
|
+
* added automatically if omitted. Useful when an outer
|
|
103
|
+
* build tool (Meson, Flatpak) exports the GJS interpreter
|
|
104
|
+
* path as `GJS_CONSOLE` (e.g. `/usr/bin/gjs-console`).
|
|
105
|
+
*
|
|
106
|
+
* Example: `"shebang": "${env:GJS_CONSOLE:-/usr/bin/env -S gjs} -m"`
|
|
107
|
+
*
|
|
108
|
+
* See also `CliBuildOptions.shebang`.
|
|
73
109
|
*/
|
|
74
|
-
shebang?: boolean;
|
|
110
|
+
shebang?: boolean | string;
|
|
75
111
|
/**
|
|
76
112
|
* Extra module aliases layered on top of the built-in alias map.
|
|
77
113
|
* Comes from `gjsify build --alias FROM=TO`.
|
|
@@ -84,4 +120,106 @@ export interface ConfigData {
|
|
|
84
120
|
* Example: `["fetch", "XMLHttpRequest"]` excludes the HTTP polyfill stack.
|
|
85
121
|
*/
|
|
86
122
|
excludeGlobals?: string[];
|
|
123
|
+
/**
|
|
124
|
+
* Compile-time defines populated from `package.json` fields. Each entry
|
|
125
|
+
* maps a JS identifier (the define key) to a dotted package.json path.
|
|
126
|
+
* Values are JSON-stringified before merging into `bundler.transform.define`.
|
|
127
|
+
*
|
|
128
|
+
* Example:
|
|
129
|
+
* ```jsonc
|
|
130
|
+
* "defineFromPackageJson": {
|
|
131
|
+
* "__PACKAGE_VERSION__": { "field": "version" },
|
|
132
|
+
* "__PACKAGE_NAME__": { "field": "name" }
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* Replaces the wrapper-script pattern (`spawnSync('gjsify', ['build',
|
|
137
|
+
* '--define', '__VERSION__=' + JSON.stringify(pkg.version)])`) used by
|
|
138
|
+
* `@ts-for-gir/cli` before this option existed.
|
|
139
|
+
*/
|
|
140
|
+
defineFromPackageJson?: Record<string, { field: string }>;
|
|
141
|
+
/**
|
|
142
|
+
* Compile-time defines populated from `process.env` at config-load time.
|
|
143
|
+
* Each entry maps a JS identifier to an environment variable name with an
|
|
144
|
+
* optional default. Values are JSON-stringified before merging into
|
|
145
|
+
* `bundler.transform.define`. When the variable is unset and no default
|
|
146
|
+
* is provided, the identifier is replaced with the literal `undefined`
|
|
147
|
+
* so consumer code can safely guard with `typeof X === 'undefined'` or
|
|
148
|
+
* `X ?? fallback`.
|
|
149
|
+
*
|
|
150
|
+
* Example:
|
|
151
|
+
* ```jsonc
|
|
152
|
+
* "defineFromEnv": {
|
|
153
|
+
* "__APPLICATION_ID__": { "env": "APPLICATION_ID", "default": "org.example.App" },
|
|
154
|
+
* "__PREFIX__": { "env": "PREFIX" }
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* Designed for projects whose build is driven by an outer tool (Meson,
|
|
159
|
+
* Make, CI) that exports environment variables — avoids a wrapper script
|
|
160
|
+
* just to thread them through to the bundler.
|
|
161
|
+
*/
|
|
162
|
+
defineFromEnv?: Record<string, { env: string; default?: string }>;
|
|
163
|
+
/**
|
|
164
|
+
* Extension → loader-kind map for files Rolldown does not classify
|
|
165
|
+
* natively. Currently only `'text'` is implemented — the file's content
|
|
166
|
+
* becomes the JS string default export (`export default "<content>"`).
|
|
167
|
+
* Replaces the legacy esbuild `loader: { '.ui': 'text' }` pattern.
|
|
168
|
+
*
|
|
169
|
+
* Example:
|
|
170
|
+
* ```jsonc
|
|
171
|
+
* "loaders": { ".ui": "text", ".asm": "text" }
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* Lives at the top level (not under `bundler`) so it doesn't leak into
|
|
175
|
+
* Rolldown's options on pass-through; the CLI converts it into a
|
|
176
|
+
* `text-loader` plugin prepended to the bundler's plugin chain.
|
|
177
|
+
*/
|
|
178
|
+
loaders?: Record<string, 'text'>;
|
|
179
|
+
/**
|
|
180
|
+
* Flatpak-related configuration consumed by `gjsify flatpak <sub>`.
|
|
181
|
+
* Lives in its own top-level namespace so the bundler config doesn't
|
|
182
|
+
* accumulate concerns and `flatpak init` / `flatpak ci` can read defaults
|
|
183
|
+
* declaratively. CLI flags override these values.
|
|
184
|
+
*/
|
|
185
|
+
flatpak?: ConfigDataFlatpak;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Flatpak-toolchain config consumed by the `gjsify flatpak` subcommand
|
|
190
|
+
* group. All fields optional — sensible defaults apply when missing.
|
|
191
|
+
*/
|
|
192
|
+
export interface ConfigDataFlatpak {
|
|
193
|
+
/** Reverse-DNS app id, e.g. `eu.jumplink.Learn6502`. Defaults to `package.json#name` if it looks like a reverse-DNS id. */
|
|
194
|
+
appId?: string;
|
|
195
|
+
/**
|
|
196
|
+
* Runtime family. Default `'gnome'` — needed at runtime by GJS bundles
|
|
197
|
+
* for GLib/GObject/GIO. `'freedesktop'` is only suitable for non-gjsify
|
|
198
|
+
* CLI tools (no GJS interpreter ships in the Freedesktop runtime).
|
|
199
|
+
*/
|
|
200
|
+
runtime?: 'gnome' | 'freedesktop';
|
|
201
|
+
/** Runtime/SDK version, e.g. `'50'` for GNOME or `'24.08'` for Freedesktop. */
|
|
202
|
+
runtimeVersion?: string;
|
|
203
|
+
/** Extra SDK extensions, e.g. `['org.freedesktop.Sdk.Extension.node24']` for build-time `yarn install`. */
|
|
204
|
+
sdkExtensions?: string[];
|
|
205
|
+
/** Path components prepended to PATH inside the build sandbox. */
|
|
206
|
+
appendPath?: string[];
|
|
207
|
+
/** The binary name to run (`/app/bin/<command>`). Defaults to `appId`. */
|
|
208
|
+
command?: string;
|
|
209
|
+
/** Finish-args (capabilities). Default depends on `runtime` + `--cli-only`. */
|
|
210
|
+
finishArgs?: string[];
|
|
211
|
+
/** Extra Flatpak modules prepended before the app's own meson/simple module (e.g. `blueprint-compiler` build). */
|
|
212
|
+
extraModules?: unknown[];
|
|
213
|
+
/** Cleanup glob patterns applied to the final manifest, e.g. `['/include', '/lib/pkgconfig']`. */
|
|
214
|
+
cleanup?: string[];
|
|
215
|
+
/** Source-of-truth lockfile for `gjsify flatpak deps` — `yarn.lock` or `package-lock.json`. */
|
|
216
|
+
lockfile?: string;
|
|
217
|
+
/**
|
|
218
|
+
* GitHub-Actions container image override for `gjsify flatpak ci`.
|
|
219
|
+
* Default derived from runtime + runtimeVersion:
|
|
220
|
+
* gnome+50 → `ghcr.io/flathub-infra/flatpak-github-actions:gnome-50`
|
|
221
|
+
*/
|
|
222
|
+
ciContainer?: string;
|
|
223
|
+
/** Branches the generated workflow triggers on. Default `['main']`. */
|
|
224
|
+
ciBranches?: string[];
|
|
87
225
|
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// Global-install helpers for `gjsify install -g <pkg>`.
|
|
2
|
+
//
|
|
3
|
+
// Layout (XDG-compliant, ~ = $HOME):
|
|
4
|
+
//
|
|
5
|
+
// ~/.local/share/gjsify/global/
|
|
6
|
+
// node_modules/<pkg>/ ← extracted package contents
|
|
7
|
+
// package.json
|
|
8
|
+
// bin/<pkg> ← original npm bin
|
|
9
|
+
// bin/<pkg>-gjs ← GJS bundle (declared via `gjsify.bin`)
|
|
10
|
+
// <pkg-data-files> ← e.g. `dist-templates/` for ts-for-gir
|
|
11
|
+
// ~/.local/bin/<bin-name> ← symlink to the matching bin under node_modules
|
|
12
|
+
//
|
|
13
|
+
// Unlike the project install path (`gjsify install <pkg>` without -g), this
|
|
14
|
+
// mode installs the requested top-level packages plus their runtime deps into
|
|
15
|
+
// a user-owned XDG location, never mutates a project package.json, and links
|
|
16
|
+
// bins into the user's PATH so the package is invocable as `<bin-name>` from
|
|
17
|
+
// anywhere — the closest GJS equivalent of `npm i -g`.
|
|
18
|
+
//
|
|
19
|
+
// `gjsify.bin` (when declared) wins over the standard npm `bin` field on this
|
|
20
|
+
// path because the user explicitly asked for the GJS-runnable artifact: e.g.
|
|
21
|
+
// `ts-for-gir` (npm bin = bin/ts-for-gir Node script) becomes a symlink to
|
|
22
|
+
// `bin/ts-for-gir-gjs` (the self-contained GJS bundle) instead.
|
|
23
|
+
|
|
24
|
+
import * as fs from 'node:fs';
|
|
25
|
+
import * as os from 'node:os';
|
|
26
|
+
import * as path from 'node:path';
|
|
27
|
+
|
|
28
|
+
export interface GlobalLayout {
|
|
29
|
+
/** Where extracted package trees live: `<prefix>/node_modules/<pkg>/`. */
|
|
30
|
+
prefix: string;
|
|
31
|
+
/** Where bin-name symlinks land. Typically `~/.local/bin`. */
|
|
32
|
+
binDir: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Compute the canonical global install layout for the current user. Honours
|
|
37
|
+
* `XDG_DATA_HOME` (per the XDG Base Directory Spec) plus
|
|
38
|
+
* `GJSIFY_GLOBAL_PREFIX` and `GJSIFY_GLOBAL_BIN_DIR` escape hatches for tests.
|
|
39
|
+
*/
|
|
40
|
+
export function defaultGlobalLayout(): GlobalLayout {
|
|
41
|
+
const prefixOverride = process.env.GJSIFY_GLOBAL_PREFIX;
|
|
42
|
+
const binOverride = process.env.GJSIFY_GLOBAL_BIN_DIR;
|
|
43
|
+
const home = os.homedir();
|
|
44
|
+
const xdgData = process.env.XDG_DATA_HOME ?? path.join(home, '.local', 'share');
|
|
45
|
+
return {
|
|
46
|
+
prefix: prefixOverride ?? path.join(xdgData, 'gjsify', 'global'),
|
|
47
|
+
binDir: binOverride ?? path.join(home, '.local', 'bin'),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface LinkedBin {
|
|
52
|
+
/** The bin-name (key from `bin` / `gjsify.bin`). */
|
|
53
|
+
name: string;
|
|
54
|
+
/** Absolute path to the file the symlink points at. */
|
|
55
|
+
target: string;
|
|
56
|
+
/** Absolute path to the symlink (under `binDir`). */
|
|
57
|
+
link: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Install each top-level package's bin entries into `binDir` as small POSIX
|
|
62
|
+
* `sh` launchers that exec the real bin. Reads each package's installed
|
|
63
|
+
* `package.json` to discover bins; prefers `gjsify.bin` (GJS-bundled bins)
|
|
64
|
+
* over the npm `bin` field, falling back to npm `bin` when no GJS map is
|
|
65
|
+
* declared. Stale launchers are replaced (latest install wins).
|
|
66
|
+
*
|
|
67
|
+
* Why launchers instead of symlinks:
|
|
68
|
+
*
|
|
69
|
+
* When a GJS bundle is invoked via a symlink in $PATH, the kernel follows
|
|
70
|
+
* the symlink to find the executable but passes the original (symlink)
|
|
71
|
+
* path to it. The bundle's shebang then runs as `gjs -m <symlink-path>`,
|
|
72
|
+
* which makes `import.meta.url` resolve to the symlink directory — so any
|
|
73
|
+
* path computation relative to `import.meta.url` (e.g. ts-for-gir's
|
|
74
|
+
* `findTemplatesRoot()`, version-discovery `readFileSync`s, gjsify's own
|
|
75
|
+
* `import.meta.url` rewrites) looks for assets in the wrong place.
|
|
76
|
+
*
|
|
77
|
+
* A `sh` launcher invokes the real path explicitly, so the bundle sees its
|
|
78
|
+
* real install location in `import.meta.url` and every relative read works.
|
|
79
|
+
* This costs ~50 bytes per bin and one extra `exec` per launch — both
|
|
80
|
+
* negligible compared to `gjs -m` cold-start time.
|
|
81
|
+
*
|
|
82
|
+
* Plain Node bins are unaffected by either approach (Node defaults to
|
|
83
|
+
* resolving symlinks in ESM module URLs); launchers are uniform for
|
|
84
|
+
* simplicity and so we don't need to discriminate by runtime here.
|
|
85
|
+
*/
|
|
86
|
+
export function linkGlobalBins(packageNames: string[], layout: GlobalLayout): LinkedBin[] {
|
|
87
|
+
fs.mkdirSync(layout.binDir, { recursive: true });
|
|
88
|
+
const created: LinkedBin[] = [];
|
|
89
|
+
|
|
90
|
+
for (const pkgName of packageNames) {
|
|
91
|
+
const pkgDir = path.join(layout.prefix, 'node_modules', pkgName);
|
|
92
|
+
const pkgJsonPath = path.join(pkgDir, 'package.json');
|
|
93
|
+
if (!fs.existsSync(pkgJsonPath)) continue;
|
|
94
|
+
|
|
95
|
+
const pkgJson = readJson(pkgJsonPath);
|
|
96
|
+
const binMap = pickBinMap(pkgName, pkgJson);
|
|
97
|
+
if (!binMap || binMap.size === 0) continue;
|
|
98
|
+
|
|
99
|
+
for (const [binName, binTarget] of binMap) {
|
|
100
|
+
const targetAbs = path.join(pkgDir, binTarget);
|
|
101
|
+
if (!fs.existsSync(targetAbs)) continue;
|
|
102
|
+
try {
|
|
103
|
+
fs.chmodSync(targetAbs, 0o755);
|
|
104
|
+
} catch {
|
|
105
|
+
/* best effort */
|
|
106
|
+
}
|
|
107
|
+
const linkPath = path.join(layout.binDir, binName);
|
|
108
|
+
fs.rmSync(linkPath, { force: true });
|
|
109
|
+
// Inline `${target}` directly — this file is rewritten on every
|
|
110
|
+
// install, paths are user-owned, and POSIX `sh` quoting via
|
|
111
|
+
// single-quotes plus `'\''` for embedded quotes is well-defined.
|
|
112
|
+
const launcher = `#!/bin/sh\nexec ${shQuote(targetAbs)} "$@"\n`;
|
|
113
|
+
fs.writeFileSync(linkPath, launcher);
|
|
114
|
+
fs.chmodSync(linkPath, 0o755);
|
|
115
|
+
created.push({ name: binName, target: targetAbs, link: linkPath });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return created;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function shQuote(s: string): string {
|
|
123
|
+
return `'${s.replace(/'/g, `'\\''`)}'`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function pickBinMap(
|
|
127
|
+
pkgName: string,
|
|
128
|
+
pkgJson: Record<string, unknown>,
|
|
129
|
+
): Map<string, string> | null {
|
|
130
|
+
const gjsifyEntry = pkgJson.gjsify as { bin?: string | Record<string, string> } | undefined;
|
|
131
|
+
if (gjsifyEntry?.bin !== undefined) {
|
|
132
|
+
return normalizeBin(pkgName, gjsifyEntry.bin);
|
|
133
|
+
}
|
|
134
|
+
const npmBin = pkgJson.bin as string | Record<string, string> | undefined;
|
|
135
|
+
if (npmBin !== undefined) {
|
|
136
|
+
return normalizeBin(pkgName, npmBin);
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function normalizeBin(
|
|
142
|
+
pkgName: string,
|
|
143
|
+
bin: string | Record<string, string>,
|
|
144
|
+
): Map<string, string> {
|
|
145
|
+
const out = new Map<string, string>();
|
|
146
|
+
if (typeof bin === 'string') {
|
|
147
|
+
const baseName = pkgName.startsWith('@')
|
|
148
|
+
? pkgName.slice(pkgName.indexOf('/') + 1)
|
|
149
|
+
: pkgName;
|
|
150
|
+
out.set(baseName, bin);
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
for (const [k, v] of Object.entries(bin)) out.set(k, v);
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function readJson(file: string): Record<string, unknown> {
|
|
158
|
+
return JSON.parse(fs.readFileSync(file, 'utf-8')) as Record<string, unknown>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Returns `true` if `binDir` is on the user's PATH. */
|
|
162
|
+
export function binDirOnPath(binDir: string): boolean {
|
|
163
|
+
const PATH = process.env.PATH ?? '';
|
|
164
|
+
const sep = process.platform === 'win32' ? ';' : ':';
|
|
165
|
+
const want = path.resolve(binDir);
|
|
166
|
+
return PATH.split(sep).some((entry) => entry && path.resolve(entry) === want);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Extracts the package name from a spec like `name@1.2`, `@scope/name`,
|
|
171
|
+
* or `@scope/name@latest`. Returns the unchanged string for plain names.
|
|
172
|
+
*/
|
|
173
|
+
export function specToPackageName(spec: string): string {
|
|
174
|
+
if (spec.startsWith('@')) {
|
|
175
|
+
const slash = spec.indexOf('/');
|
|
176
|
+
if (slash < 0) return spec;
|
|
177
|
+
const at = spec.indexOf('@', slash);
|
|
178
|
+
return at < 0 ? spec : spec.slice(0, at);
|
|
179
|
+
}
|
|
180
|
+
const at = spec.indexOf('@');
|
|
181
|
+
return at < 0 ? spec : spec.slice(0, at);
|
|
182
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Resolve `bundler.plugins` entries that are specified by package name in
|
|
2
|
+
// the user's gjsify config, e.g.:
|
|
3
|
+
//
|
|
4
|
+
// "bundler": {
|
|
5
|
+
// "plugins": [
|
|
6
|
+
// { "name": "@gjsify/vite-plugin-blueprint", "options": { "minify": true } },
|
|
7
|
+
// { "name": "@gjsify/vite-plugin-gettext", "export": "msgfmtPlugin", "options": { ... } }
|
|
8
|
+
// ]
|
|
9
|
+
// }
|
|
10
|
+
//
|
|
11
|
+
// Lets `package.json#gjsify` describe the full plugin chain without dropping
|
|
12
|
+
// to a JS-form config file (`gjsify.config.mjs`). Resolution is anchored at
|
|
13
|
+
// the project root (where the config lives) so the project's own
|
|
14
|
+
// `node_modules` wins over the CLI's own dependencies.
|
|
15
|
+
|
|
16
|
+
import { createRequire } from 'node:module';
|
|
17
|
+
import { join } from 'node:path';
|
|
18
|
+
import { pathToFileURL } from 'node:url';
|
|
19
|
+
import type { RolldownPluginOption } from 'rolldown';
|
|
20
|
+
|
|
21
|
+
/** User-supplied entry: a package name + optional named export and options. */
|
|
22
|
+
export interface PluginByName {
|
|
23
|
+
name: string;
|
|
24
|
+
/** Named export to invoke. Defaults to the module's default export. */
|
|
25
|
+
export?: string;
|
|
26
|
+
/** Options forwarded to the plugin factory. */
|
|
27
|
+
options?: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Type-guard: a `PluginByName` shape rather than a Rolldown plugin object. */
|
|
31
|
+
export function isPluginByName(value: unknown): value is PluginByName {
|
|
32
|
+
return (
|
|
33
|
+
typeof value === 'object' &&
|
|
34
|
+
value !== null &&
|
|
35
|
+
typeof (value as { name?: unknown }).name === 'string' &&
|
|
36
|
+
// RolldownPluginOption can be `false | null | undefined | Plugin | Promise<Plugin>`.
|
|
37
|
+
// A real plugin always has a function-shape behavior; `name` alone is shared
|
|
38
|
+
// with our shape, so we additionally require absence of plugin-shape fields.
|
|
39
|
+
!('apply' in value) &&
|
|
40
|
+
!('resolveId' in value) &&
|
|
41
|
+
!('load' in value) &&
|
|
42
|
+
!('transform' in value) &&
|
|
43
|
+
!('renderChunk' in value) &&
|
|
44
|
+
!('generateBundle' in value)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Resolve a list of mixed user plugins. Entries that are already plugin
|
|
50
|
+
* objects pass through unchanged; entries shaped like `PluginByName` get
|
|
51
|
+
* dynamically imported, instantiated with their `options`, and returned in
|
|
52
|
+
* the same position. Resolution is anchored at `projectDir`.
|
|
53
|
+
*
|
|
54
|
+
* Throws when a name fails to resolve, when the chosen export is not a
|
|
55
|
+
* function, or when the factory returns nothing.
|
|
56
|
+
*/
|
|
57
|
+
export async function resolveUserPlugins(
|
|
58
|
+
plugins: ReadonlyArray<RolldownPluginOption | PluginByName>,
|
|
59
|
+
projectDir: string,
|
|
60
|
+
): Promise<RolldownPluginOption[]> {
|
|
61
|
+
const requireFromProject = createRequire(join(projectDir, 'package.json'));
|
|
62
|
+
const out: RolldownPluginOption[] = [];
|
|
63
|
+
|
|
64
|
+
for (const entry of plugins) {
|
|
65
|
+
if (!isPluginByName(entry)) {
|
|
66
|
+
out.push(entry as RolldownPluginOption);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let resolvedPath: string;
|
|
71
|
+
try {
|
|
72
|
+
resolvedPath = requireFromProject.resolve(entry.name);
|
|
73
|
+
} catch (err) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`gjsify config: failed to resolve plugin "${entry.name}" from ${projectDir}. ` +
|
|
76
|
+
`Add it to your project's dependencies, or pass a Plugin object directly. ` +
|
|
77
|
+
`(${(err as Error).message})`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const mod = await import(pathToFileURL(resolvedPath).href);
|
|
82
|
+
const exportName = entry.export ?? 'default';
|
|
83
|
+
const factory = (mod as Record<string, unknown>)[exportName];
|
|
84
|
+
|
|
85
|
+
if (typeof factory !== 'function') {
|
|
86
|
+
const available = Object.keys(mod).filter(
|
|
87
|
+
(k) => typeof (mod as Record<string, unknown>)[k] === 'function',
|
|
88
|
+
);
|
|
89
|
+
throw new Error(
|
|
90
|
+
`gjsify config: plugin "${entry.name}" has no function export "${exportName}". ` +
|
|
91
|
+
`Available function exports: ${available.length ? available.join(', ') : '(none)'}.`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const plugin = await (factory as (opts?: unknown) => unknown)(entry.options);
|
|
96
|
+
if (plugin === undefined || plugin === null) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`gjsify config: plugin "${entry.name}" factory returned ${plugin}. ` +
|
|
99
|
+
`Check the plugin's signature — it should return a Rolldown/Vite plugin object.`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
out.push(plugin as RolldownPluginOption);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return out;
|
|
106
|
+
}
|