@snap-rail/plugin-kit 0.0.0-stage → 0.2.0
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/LICENSE +202 -0
- package/lib/build.js +216 -0
- package/lib/index.js +43 -0
- package/lib/manifest.js +122 -0
- package/lib/pack.js +158 -0
- package/lib/recipe.js +51 -0
- package/lib/seeds.js +117 -0
- package/lib/sign.js +107 -0
- package/lib/types/build.d.ts +72 -0
- package/lib/types/index.d.ts +18 -0
- package/lib/types/manifest.d.ts +72 -0
- package/lib/types/pack.d.ts +79 -0
- package/lib/types/recipe.d.ts +53 -0
- package/lib/types/seeds.d.ts +78 -0
- package/lib/types/sign.d.ts +72 -0
- package/lib/types/slot-vocabulary.d.ts +33 -0
- package/package.json +56 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/lib/build.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { CLIENT_EXTERNALS } from "./seeds.js";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
5
|
+
import { defineConfig } from "tsdown";
|
|
6
|
+
//#region lib/types/build.js
|
|
7
|
+
/**
|
|
8
|
+
* tsdown config factories for plugin packages — the two faces a plugin
|
|
9
|
+
* ships. The host face is a plain ESM node bundle; the client face is the
|
|
10
|
+
** CJS factory bundle** the renderer's module loader consumes: the whole
|
|
11
|
+
* output wrapped in `window.__ModuleLoader__.load({ id, factory })`, with
|
|
12
|
+
* the seed table externalized (shared instances, zero duplication).
|
|
13
|
+
*
|
|
14
|
+
* Usage (a plugin package's `tsdown.config.ts`):
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { clientBundle, hostBundle } from '@snap-rail/plugin-kit/build'
|
|
18
|
+
* export default [
|
|
19
|
+
* hostBundle('packages/host/src/index.ts'),
|
|
20
|
+
* clientBundle('@scope/my-suite', 'packages/client/src/index.tsx'),
|
|
21
|
+
* ]
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @module @snap-rail/plugin-kit/build
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Contributed-seed ids this package declares it requires from other plugins
|
|
28
|
+
* (`snapRail.client.externalSeeds`), read from the package.json in the
|
|
29
|
+
* working directory (tsdown runs there). clientBundle externalizes exactly
|
|
30
|
+
* these ids on top of the seed whitelist, so a consumer's bundle resolves a
|
|
31
|
+
* third-party framework's primitives or an extension pack's components from
|
|
32
|
+
* the runtime seed table — single instances — instead of inlining private
|
|
33
|
+
* copies the loader could never resolve.
|
|
34
|
+
*/
|
|
35
|
+
function declaredExternalSeeds() {
|
|
36
|
+
try {
|
|
37
|
+
const seeds = JSON.parse(readFileSync(join(process.cwd(), "package.json"), "utf8")).snapRail?.client?.externalSeeds;
|
|
38
|
+
return Array.isArray(seeds) && seeds.every((seed) => typeof seed === "string" && seed !== "") ? seeds : [];
|
|
39
|
+
} catch {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Extensions the client preset ships as emitted assets beside the bundle. */
|
|
44
|
+
const ASSET_EXTENSIONS = /\.(png|jpe?g|webp|gif|svg)$/;
|
|
45
|
+
/**
|
|
46
|
+
* The seed interop guard: a client face must consume seed modules through
|
|
47
|
+
* **named or star imports only**. A default import compiles — under
|
|
48
|
+
* rolldown's CJS node-mode interop (`__toESM(require(id), 1)`) — into
|
|
49
|
+
* `.default` = the required namespace itself, never the module's own
|
|
50
|
+
* default export. A class-shaped seed (the timer service) then arrives as
|
|
51
|
+
* a plain object and cordis refuses it at mount: on the user's terminal,
|
|
52
|
+
* as a dead renderer, instead of here at build time. Star and named
|
|
53
|
+
* imports survive both interop modes; `import type` erases and is fine.
|
|
54
|
+
*
|
|
55
|
+
* Source-level scan (emit-independent), plugin sources only — inlined
|
|
56
|
+
* third-party dependencies keep their own import style.
|
|
57
|
+
*/
|
|
58
|
+
function seedImportGuard() {
|
|
59
|
+
const seedPattern = CLIENT_EXTERNALS.map((id) => id.replace(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`)).join("|");
|
|
60
|
+
const pattern = new RegExp(String.raw`import\s+(?![{*])(?!type\b)([\w$]+)\s*(?:,[^;]*?)?\s*from\s*['"](` + seedPattern + String.raw`)['"]`);
|
|
61
|
+
return {
|
|
62
|
+
name: "snap-rail-seed-import-guard",
|
|
63
|
+
transform(code, id) {
|
|
64
|
+
if (id.includes("node_modules")) return void 0;
|
|
65
|
+
const match = pattern.exec(code);
|
|
66
|
+
if (match === null) return void 0;
|
|
67
|
+
this.error(`种子模块 "${match[2]}" 被默认导入(import ${match[1]} from '${match[2]}')。种子只允许 named/star 导入:rolldown 的 CJS interop 会把 default 变成整个命名空间对象,运行时挂载即崩。请改为 import { … } from '` + match[2] + "' 或 import * as ns from '" + match[2] + "'。");
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** The pool directory name an installed package occupies (installer rule). */
|
|
72
|
+
function poolDirName(packageName) {
|
|
73
|
+
return packageName.replaceAll("/", "__");
|
|
74
|
+
}
|
|
75
|
+
/** A cheap stable id for deduplicating injected stylesheet tags. */
|
|
76
|
+
function styleId(source) {
|
|
77
|
+
let hash = 5381;
|
|
78
|
+
for (let index = 0; index < source.length; index += 1) hash = (hash << 5) + hash + source.charCodeAt(index) | 0;
|
|
79
|
+
return `snap-rail-css-${(hash >>> 0).toString(36)}`;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Turn `.css` imports into modules that inject a `<style>` tag on first
|
|
83
|
+
* evaluation. The module loader has no stylesheet story and the pool world
|
|
84
|
+
* has no app-level stylesheet: whoever needs tokens or utilities (the
|
|
85
|
+
* framework, above all) carries them inside the bundle, idempotently. The
|
|
86
|
+
* virtual-id redirect (`\0snap-rail-css:`) keeps rolldown's built-in CSS
|
|
87
|
+
* pipeline — which demands `@tsdown/css` and emits side files we have no
|
|
88
|
+
* loader for — out of the graph entirely.
|
|
89
|
+
*/
|
|
90
|
+
function cssModules() {
|
|
91
|
+
const VIRTUAL_PREFIX = "\0snap-rail-css:";
|
|
92
|
+
return {
|
|
93
|
+
name: "snap-rail-css-modules",
|
|
94
|
+
resolveId(source, importer) {
|
|
95
|
+
if (source.startsWith(VIRTUAL_PREFIX) || !source.endsWith(".css")) return void 0;
|
|
96
|
+
const file = isAbsolute(source) ? source : resolve(dirname(importer ?? process.cwd()), source);
|
|
97
|
+
return `${VIRTUAL_PREFIX}${file.replaceAll("\\", "/")}.js`;
|
|
98
|
+
},
|
|
99
|
+
async load(id) {
|
|
100
|
+
if (!id.startsWith(VIRTUAL_PREFIX)) return void 0;
|
|
101
|
+
const file = id.slice(15).replace(/\.js$/, "");
|
|
102
|
+
const css = await readFile(file, "utf8");
|
|
103
|
+
const tag = styleId(css);
|
|
104
|
+
return [
|
|
105
|
+
`const css = ${JSON.stringify(css)};`,
|
|
106
|
+
`const id = ${JSON.stringify(tag)};`,
|
|
107
|
+
"if (typeof document === \"undefined\") throw new Error(\"css module outside the renderer\");",
|
|
108
|
+
"if (document.getElementById(id) === null) {",
|
|
109
|
+
" const style = document.createElement(\"style\");",
|
|
110
|
+
" style.id = id;",
|
|
111
|
+
" style.textContent = css;",
|
|
112
|
+
" document.head.append(style);",
|
|
113
|
+
"}",
|
|
114
|
+
"export default {}"
|
|
115
|
+
].join("\n");
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Emit imported bitmap/svg assets beside the client bundle and rewrite their
|
|
121
|
+
* import values to absolute `snap-plugin://` URLs. The renderer has no
|
|
122
|
+
* filesystem and the module loader's `require` only knows the seed table, so
|
|
123
|
+
* a relative asset path could never resolve at runtime — the transport URL
|
|
124
|
+
* is the one address that works identically in dev and packaged builds.
|
|
125
|
+
* Emitted file names are stable (no content hash — a zip is immutable);
|
|
126
|
+
* same-named assets in different directories collide, last one wins.
|
|
127
|
+
*/
|
|
128
|
+
function assetUrls(packageName, baseDir) {
|
|
129
|
+
return {
|
|
130
|
+
name: "snap-rail-asset-urls",
|
|
131
|
+
async load(id) {
|
|
132
|
+
const file = id.replaceAll("\\", "/").split("?")[0].replace(/#.*$/, "");
|
|
133
|
+
if (!ASSET_EXTENSIONS.test(file)) return void 0;
|
|
134
|
+
const fileName = `assets/${file.split("/").at(-1)}`;
|
|
135
|
+
this.emitFile({
|
|
136
|
+
type: "asset",
|
|
137
|
+
fileName,
|
|
138
|
+
source: await readFile(file)
|
|
139
|
+
});
|
|
140
|
+
return `export default ${JSON.stringify(`snap-plugin://pool/${poolDirName(packageName)}/${baseDir}/${fileName}`)}`;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The host face: an ESM node bundle. The spine stays external by default
|
|
146
|
+
* (tsdown externalizes package.json dependencies); anything else the plugin
|
|
147
|
+
* needs ships inside.
|
|
148
|
+
*
|
|
149
|
+
* @param entry - the host entry source file.
|
|
150
|
+
*/
|
|
151
|
+
function hostBundle(entry) {
|
|
152
|
+
return defineConfig({
|
|
153
|
+
entry: { host: entry },
|
|
154
|
+
outDir: "lib-host",
|
|
155
|
+
format: ["esm"],
|
|
156
|
+
platform: "node",
|
|
157
|
+
target: "es2024",
|
|
158
|
+
dts: false,
|
|
159
|
+
clean: false
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The client face: a CJS browser bundle welded into the module-loader
|
|
164
|
+
* factory wrapper. Only the seed vocabulary ({@link CLIENT_EXTERNALS} —
|
|
165
|
+
* base plus contributed ids) stays external; everything else (icons,
|
|
166
|
+
* protocol libraries, bitmaps) inlines — bitmaps and SVGs excepted: they
|
|
167
|
+
* emit beside the bundle as `snap-plugin://`-addressed assets instead of
|
|
168
|
+
* base64 bulk, and `.css` imports become style-injection modules.
|
|
169
|
+
*
|
|
170
|
+
* A provider of contributed seeds (a framework, a contract owner) builds
|
|
171
|
+
* the same way: its entry default-exports `{ seeds: { … } }` mapping each
|
|
172
|
+
* subpath to its module namespace, and the shell seeds
|
|
173
|
+
* `<package>/<subpath>` ids from that map after requiring the face.
|
|
174
|
+
*
|
|
175
|
+
* @param packageName - the plugin's npm name (the module id it registers as).
|
|
176
|
+
* @param entry - the renderer entry source file.
|
|
177
|
+
* @param options - see {@link ClientBundleOptions}.
|
|
178
|
+
*/
|
|
179
|
+
function clientBundle(packageName, entry, options = {}) {
|
|
180
|
+
const entryFile = entry.replaceAll("\\", "/");
|
|
181
|
+
const entryPath = resolve(process.cwd(), entryFile).replaceAll("\\", "/");
|
|
182
|
+
const extraCss = (options.css ?? []).map((file) => resolve(process.cwd(), file).replaceAll("\\", "/"));
|
|
183
|
+
/** Prepend the extra-css imports to the entry so the styles ride the
|
|
184
|
+
* bundle through the same style-injection modules as source css. */
|
|
185
|
+
const injectExtraCss = {
|
|
186
|
+
name: "snap-rail-inject-extra-css",
|
|
187
|
+
transform(code, id) {
|
|
188
|
+
if (id.replaceAll("\\", "/").split("?")[0].replace(/#.*$/, "") !== entryPath || extraCss.length === 0) return void 0;
|
|
189
|
+
return `${extraCss.map((css) => `import ${JSON.stringify(css)};\n`).join("")}${code}`;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
return defineConfig({
|
|
193
|
+
entry: { client: entry },
|
|
194
|
+
outDir: "lib-client",
|
|
195
|
+
format: ["cjs"],
|
|
196
|
+
platform: "browser",
|
|
197
|
+
target: "es2024",
|
|
198
|
+
dts: false,
|
|
199
|
+
clean: false,
|
|
200
|
+
outExtensions: () => ({ js: ".js" }),
|
|
201
|
+
noExternal: [/.*/],
|
|
202
|
+
external: [...CLIENT_EXTERNALS, ...declaredExternalSeeds()],
|
|
203
|
+
plugins: [
|
|
204
|
+
injectExtraCss,
|
|
205
|
+
cssModules(),
|
|
206
|
+
assetUrls(packageName, "lib-client"),
|
|
207
|
+
seedImportGuard()
|
|
208
|
+
],
|
|
209
|
+
outputOptions: {
|
|
210
|
+
banner: `window.__ModuleLoader__.load({ id: ${JSON.stringify(packageName)}, factory: function (require, module, exports) {`,
|
|
211
|
+
footer: "return module.exports && module.exports.__esModule === true && \"default\" in module.exports ? module.exports.default : module.exports; } });"
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
//#endregion
|
|
216
|
+
export { clientBundle, hostBundle, seedImportGuard };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { CLIENT_EXTERNALS, CONTRIBUTED_SEED_MODULES, SEED_ABI_REVISION, SEED_MODULES, checkSeedAbi } from "./seeds.js";
|
|
2
|
+
import { clientBundle, hostBundle, seedImportGuard } from "./build.js";
|
|
3
|
+
import { PLUGIN_KINDS, clientFaceSchema, seedAbiSchema, snapRailManifestSchema } from "./manifest.js";
|
|
4
|
+
import { releaseFiles, releaseManifest, zipFileName } from "./pack.js";
|
|
5
|
+
import { packageNameOf, recipeDocumentSchema, recipePackageNames, recipeRowSchema } from "./recipe.js";
|
|
6
|
+
import { RELEASE_METADATA_PATH, RELEASE_SIGNATURE_PATH, canonicalJson, releaseMetadata, sha256, signReleaseFiles, signReleaseMetadata, verifyReleaseFiles } from "./sign.js";
|
|
7
|
+
//#region lib/types/slot-vocabulary.js
|
|
8
|
+
/**
|
|
9
|
+
* Authoring helper for UI framework packages: define the package's slot
|
|
10
|
+
* vocabulary in one place and get a runtime membership guard for it. A
|
|
11
|
+
* vocabulary is plain data — a `shell.menu`-style dotted id table plus the
|
|
12
|
+
* reserved `custom:<publisher>.<name>` extension namespace — and every
|
|
13
|
+
* consumer (components, layouts) checks membership through the returned
|
|
14
|
+
* guard instead of hand-rolled string tests. Ids are declared with an
|
|
15
|
+
* `as const` object so consumers get autocomplete and compile-time
|
|
16
|
+
* misspelling errors from the literal-union type (`SlotIdOf`).
|
|
17
|
+
*
|
|
18
|
+
* The shipped framework (@snap-rail/framework-shadcn) implements the same
|
|
19
|
+
* contract inline (`UI_SLOT_IDS`/`isValidUiSlotId`); new frameworks start
|
|
20
|
+
* here.
|
|
21
|
+
*
|
|
22
|
+
* @module @snap-rail/plugin-kit/slot-vocabulary
|
|
23
|
+
*/
|
|
24
|
+
/** Shape every slot id shares: dotted lowercase kebab (`shell.menu`). */
|
|
25
|
+
const SLOT_ID_PATTERN = /^[a-z][a-z0-9-]*(?:\.[a-z][a-z0-9-]+)+$/;
|
|
26
|
+
/** The reserved extension namespace: a custom slot is owned by a publisher. */
|
|
27
|
+
const CUSTOM_SLOT_PATTERN = /^custom:[a-z0-9][a-z0-9-]*\.[a-z0-9][a-z0-9-]*$/;
|
|
28
|
+
/** Define and validate a framework's slot vocabulary. Malformed ids throw at
|
|
29
|
+
* definition time — a vocabulary ships once and is consumed everywhere, so
|
|
30
|
+
* it fails loudly in the framework author's build, never in a component. */
|
|
31
|
+
function defineSlotVocabulary(ids, options) {
|
|
32
|
+
for (const [key, id] of Object.entries(ids)) if (!SLOT_ID_PATTERN.test(id)) throw new Error(`defineSlotVocabulary: 插槽 id 不合法(需为小写 kebab 点分,如 shell.menu):${key} = ${JSON.stringify(id)}`);
|
|
33
|
+
const known = new Set(Object.values(ids));
|
|
34
|
+
const customPattern = options?.customPattern ?? CUSTOM_SLOT_PATTERN;
|
|
35
|
+
return {
|
|
36
|
+
ids,
|
|
37
|
+
isSlotId(value) {
|
|
38
|
+
return known.has(value) || customPattern.test(value);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { CLIENT_EXTERNALS, CONTRIBUTED_SEED_MODULES, CUSTOM_SLOT_PATTERN, PLUGIN_KINDS, RELEASE_METADATA_PATH, RELEASE_SIGNATURE_PATH, SEED_ABI_REVISION, SEED_MODULES, canonicalJson, checkSeedAbi, clientBundle, clientFaceSchema, defineSlotVocabulary, hostBundle, packageNameOf, recipeDocumentSchema, recipePackageNames, recipeRowSchema, releaseFiles, releaseManifest, releaseMetadata, seedAbiSchema, seedImportGuard, sha256, signReleaseFiles, signReleaseMetadata, snapRailManifestSchema, verifyReleaseFiles, zipFileName };
|
package/lib/manifest.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { PLUGIN_LAYERS } from "@snap-rail/util/manifest";
|
|
3
|
+
//#region lib/types/manifest.js
|
|
4
|
+
/**
|
|
5
|
+
* The plugin author's manifest vocabulary: the `snapRail` field of a plugin
|
|
6
|
+
* package's `package.json`. One source of truth for the shape — the
|
|
7
|
+
* installer validates against it, the pool scan records it, and authors
|
|
8
|
+
* import the types.
|
|
9
|
+
*
|
|
10
|
+
* @module @snap-rail/plugin-kit/manifest
|
|
11
|
+
*/
|
|
12
|
+
/** Plugin package kinds. `suite` and `framework` packages are single-active. */
|
|
13
|
+
const PLUGIN_KINDS = [
|
|
14
|
+
"suite",
|
|
15
|
+
"driver",
|
|
16
|
+
"framework",
|
|
17
|
+
"plugin"
|
|
18
|
+
];
|
|
19
|
+
/** The seed-ABI stamp a pack writes into a client face's manifest (shape of
|
|
20
|
+
* `snapRail.client.seedAbi`). Computed by the packer from the built bundle
|
|
21
|
+
* and the build tree's resolved seed versions — authors never hand-write it. */
|
|
22
|
+
const seedAbiSchema = z.object({
|
|
23
|
+
/** The seed-contract revision the face was built against. */
|
|
24
|
+
revision: z.number().int().min(1),
|
|
25
|
+
/** Base-package versions of the seeds the bundle requires, by seed id. */
|
|
26
|
+
versions: z.record(z.string().min(1), z.string().min(1))
|
|
27
|
+
}).strict();
|
|
28
|
+
/** The renderer face declaration: where the client bundle lives. */
|
|
29
|
+
const clientFaceSchema = z.object({
|
|
30
|
+
/** Path of the CJS factory bundle inside the package (from its root). */
|
|
31
|
+
entry: z.string().min(1).default("lib/client.js"),
|
|
32
|
+
/**
|
|
33
|
+
* Subpaths this client face registers as contributed seeds (e.g. framework
|
|
34
|
+
* primitives like `./ui`): each becomes a `__ModuleLoader__` id other
|
|
35
|
+
* plugins' client bundles may `require` — same instance, one execution.
|
|
36
|
+
*/
|
|
37
|
+
seeds: z.array(z.string().min(1)).optional(),
|
|
38
|
+
/**
|
|
39
|
+
* Contributed seed ids (`<package>/<subpath>`) this client face requires
|
|
40
|
+
* from other installed plugins (a framework's primitives, an extension
|
|
41
|
+
* pack's components). Build-time plumbing: `clientBundle` externalizes
|
|
42
|
+
* exactly these ids so they resolve from the shell's seed table at
|
|
43
|
+
* runtime instead of being inlined as private copies; the enable precheck
|
|
44
|
+
* verifies each provider is installed and declares the id. It is not a
|
|
45
|
+
* compatibility declaration — package/version compatibility rides
|
|
46
|
+
* `requires`.
|
|
47
|
+
*/
|
|
48
|
+
externalSeeds: z.array(z.string().min(1)).optional(),
|
|
49
|
+
/**
|
|
50
|
+
* The seed-contract stamp the packer computed at pack time (see
|
|
51
|
+
* `@snap-rail/plugin-kit/seeds` for the comparison contract). Present in
|
|
52
|
+
* the release manifest only; the installer and the renderer boot both
|
|
53
|
+
* check it against the live table, absent declarations pass untouched.
|
|
54
|
+
*/
|
|
55
|
+
seedAbi: seedAbiSchema.optional()
|
|
56
|
+
}).strict();
|
|
57
|
+
/**
|
|
58
|
+
* Declared dependencies — the single compatibility declaration a plugin
|
|
59
|
+
* makes, checked before an enable/disable/uninstall lands. Three entry
|
|
60
|
+
* forms:
|
|
61
|
+
*
|
|
62
|
+
* - `name` — a package requirement (`@snap-rail/equipment`): satisfied when
|
|
63
|
+
* that package is installed and enabled.
|
|
64
|
+
* - `name@range` — the same, with a version range on the resolved versions
|
|
65
|
+
* (`@snap-rail/framework-shadcn@^0.1`): also requires the installed
|
|
66
|
+
* version to satisfy the range (`@snap-rail/util/semver` semantics).
|
|
67
|
+
* A renderer face's ecosystem binding IS this form: requiring the
|
|
68
|
+
* framework package is what binds a component to it — there is no
|
|
69
|
+
* separate framework field.
|
|
70
|
+
* - `service:id` — a live cordis service (`service:store`), checked
|
|
71
|
+
* against the running service table.
|
|
72
|
+
*
|
|
73
|
+
* An unmet requirement refuses the write instead of leaving the next boot
|
|
74
|
+
* to fail.
|
|
75
|
+
*/
|
|
76
|
+
const requiresSchema = z.array(z.string().min(1)).optional();
|
|
77
|
+
/** The `snapRail` manifest field's schema. */
|
|
78
|
+
const snapRailManifestSchema = z.object({
|
|
79
|
+
/** Role declaration; `suite` and `framework` packages are single-active. */
|
|
80
|
+
kind: z.enum(PLUGIN_KINDS).optional(),
|
|
81
|
+
/** The layer this package belongs to; gates the import direction. */
|
|
82
|
+
layer: z.enum(PLUGIN_LAYERS).optional(),
|
|
83
|
+
/**
|
|
84
|
+
* The human-facing display name shown wherever the plugin is listed (the
|
|
85
|
+
* management page, install confirmations). The package author names it
|
|
86
|
+
* freely (Chinese included); absent → surfaces fall back to the package
|
|
87
|
+
* name.
|
|
88
|
+
*/
|
|
89
|
+
title: z.string().min(1).max(64).optional(),
|
|
90
|
+
/**
|
|
91
|
+
* The classification marker shown in the management UI. Open vocabulary —
|
|
92
|
+
* the package author names it freely (Chinese included); the app groups
|
|
93
|
+
* rows by the raw marker and never validates it against an enum. Absent →
|
|
94
|
+
* the management page files the row under 其他.
|
|
95
|
+
*/
|
|
96
|
+
category: z.string().min(1).max(64).optional(),
|
|
97
|
+
/**
|
|
98
|
+
* Mutex group: enabling a package of group X deactivates every other
|
|
99
|
+
* enabled package of group X in the same batched write. Suites default
|
|
100
|
+
* to `'suite'`, frameworks to `'framework'`.
|
|
101
|
+
*/
|
|
102
|
+
exclusive: z.string().min(1).optional(),
|
|
103
|
+
requires: requiresSchema,
|
|
104
|
+
/**
|
|
105
|
+
* True for packages with no host face at all (frameworks, renderer
|
|
106
|
+
* stations): their rows are config-only — never mounted into the host
|
|
107
|
+
* tree, served to the renderer through `client-config.list`.
|
|
108
|
+
*/
|
|
109
|
+
clientOnly: z.boolean().optional(),
|
|
110
|
+
/** The renderer face; omit for host-only plugins. */
|
|
111
|
+
client: clientFaceSchema.optional(),
|
|
112
|
+
/** Permission declarations — shown at install time, enforced later. */
|
|
113
|
+
permissions: z.array(z.string()).optional(),
|
|
114
|
+
/** Host and renderer versions the built faces support. */
|
|
115
|
+
engines: z.object({
|
|
116
|
+
snapRail: z.string().min(1).optional(),
|
|
117
|
+
hostProtocol: z.string().min(1).optional(),
|
|
118
|
+
renderer: z.string().min(1).optional()
|
|
119
|
+
}).strict().optional()
|
|
120
|
+
});
|
|
121
|
+
//#endregion
|
|
122
|
+
export { PLUGIN_KINDS, PLUGIN_LAYERS, clientFaceSchema, seedAbiSchema, snapRailManifestSchema };
|