@zeus-js/output-wc 0.1.0-beta.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/dist/output-wc.cjs.js +318 -0
- package/dist/output-wc.cjs.prod.js +318 -0
- package/dist/output-wc.d.ts +63 -0
- package/dist/output-wc.esm-bundler.js +291 -0
- package/index.js +4 -0
- package/package.json +59 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* output-wc v0.1.0-beta.0
|
|
3
|
+
* (c) 2026 baicie
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
**/
|
|
6
|
+
Object.defineProperties(exports, {
|
|
7
|
+
__esModule: { value: true },
|
|
8
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
9
|
+
});
|
|
10
|
+
//#region \0rolldown/runtime.js
|
|
11
|
+
var __create = Object.create;
|
|
12
|
+
var __defProp = Object.defineProperty;
|
|
13
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
14
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
15
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
19
|
+
key = keys[i];
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
21
|
+
get: ((k) => from[k]).bind(null, key),
|
|
22
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
//#endregion
|
|
32
|
+
let node_path = require("node:path");
|
|
33
|
+
node_path = __toESM(node_path, 1);
|
|
34
|
+
let _zeus_js_bundler_plugin = require("@zeus-js/bundler-plugin");
|
|
35
|
+
let _zeus_js_component_dts = require("@zeus-js/component-dts");
|
|
36
|
+
//#region packages/web-c/output-wc/src/generateCustomElementsJson.ts
|
|
37
|
+
function generateCustomElementsJson(options) {
|
|
38
|
+
const { manifest, getModulePath } = options;
|
|
39
|
+
const result = {
|
|
40
|
+
schemaVersion: "1.0.0",
|
|
41
|
+
modules: manifest.components.map((component) => {
|
|
42
|
+
const modulePath = normalizeModulePath(getModulePath(component));
|
|
43
|
+
return {
|
|
44
|
+
kind: "javascript-module",
|
|
45
|
+
path: modulePath,
|
|
46
|
+
declarations: [{
|
|
47
|
+
kind: "class",
|
|
48
|
+
name: `${component.name}Element`,
|
|
49
|
+
tagName: component.tag,
|
|
50
|
+
customElement: true,
|
|
51
|
+
description: component.description,
|
|
52
|
+
attributes: generateAttributes(component),
|
|
53
|
+
members: generateMembers(component),
|
|
54
|
+
events: generateEvents(component),
|
|
55
|
+
slots: generateSlots(component),
|
|
56
|
+
cssParts: component.cssParts.map((name) => ({ name })),
|
|
57
|
+
cssProperties: component.cssVars.map((name) => ({ name }))
|
|
58
|
+
}],
|
|
59
|
+
exports: [{
|
|
60
|
+
kind: "js",
|
|
61
|
+
name: component.exportName,
|
|
62
|
+
declaration: {
|
|
63
|
+
name: `${component.name}Element`,
|
|
64
|
+
module: modulePath
|
|
65
|
+
}
|
|
66
|
+
}]
|
|
67
|
+
};
|
|
68
|
+
})
|
|
69
|
+
};
|
|
70
|
+
return `${JSON.stringify(result, null, 2)}\n`;
|
|
71
|
+
}
|
|
72
|
+
function generateAttributes(component) {
|
|
73
|
+
const result = [];
|
|
74
|
+
for (const [name, prop] of Object.entries(component.props)) {
|
|
75
|
+
const attrName = getAttributeName(name, prop);
|
|
76
|
+
if (attrName === false) continue;
|
|
77
|
+
result.push({
|
|
78
|
+
name: attrName,
|
|
79
|
+
description: prop.description,
|
|
80
|
+
type: { text: formatPropType(prop) },
|
|
81
|
+
default: prop.default === void 0 ? void 0 : JSON.stringify(prop.default)
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
function generateMembers(component) {
|
|
87
|
+
return Object.entries(component.props).map(([name, prop]) => {
|
|
88
|
+
return {
|
|
89
|
+
kind: "field",
|
|
90
|
+
name,
|
|
91
|
+
description: prop.description,
|
|
92
|
+
type: { text: formatPropType(prop) },
|
|
93
|
+
default: prop.default === void 0 ? void 0 : JSON.stringify(prop.default)
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function generateEvents(component) {
|
|
98
|
+
return Object.entries(component.events).map(([name, event]) => {
|
|
99
|
+
return {
|
|
100
|
+
name,
|
|
101
|
+
description: event.description,
|
|
102
|
+
type: { text: event.detail ? `CustomEvent<${formatDetailType(event.detail)}>` : "CustomEvent" }
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function generateSlots(component) {
|
|
107
|
+
return Object.entries(component.slots).map(([name, slot]) => {
|
|
108
|
+
return {
|
|
109
|
+
name: name === "default" ? "" : name,
|
|
110
|
+
description: slot.description
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function getAttributeName(propName, prop) {
|
|
115
|
+
if (prop.attr === false) return false;
|
|
116
|
+
if (typeof prop.attr === "string") return prop.attr;
|
|
117
|
+
return propName.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
118
|
+
}
|
|
119
|
+
function formatPropType(prop) {
|
|
120
|
+
var _prop$values;
|
|
121
|
+
if ((_prop$values = prop.values) === null || _prop$values === void 0 ? void 0 : _prop$values.length) return prop.values.map((value) => JSON.stringify(value)).join(" | ");
|
|
122
|
+
switch (prop.type) {
|
|
123
|
+
case "string": return "string";
|
|
124
|
+
case "number": return "number";
|
|
125
|
+
case "boolean": return "boolean";
|
|
126
|
+
case "array": return "unknown[]";
|
|
127
|
+
case "object": return "Record<string, unknown>";
|
|
128
|
+
default: return "unknown";
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function formatDetailType(detail) {
|
|
132
|
+
return `{ ${Object.entries(detail).map(([name, type]) => `${name}: ${type}`).join("; ")} }`;
|
|
133
|
+
}
|
|
134
|
+
function normalizeModulePath(value) {
|
|
135
|
+
return value.replace(/\\/g, "/");
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region packages/web-c/output-wc/src/imports.ts
|
|
139
|
+
function toAbsoluteImportPath(root, source) {
|
|
140
|
+
const normalizedRoot = normalizePath(root);
|
|
141
|
+
const normalizedSource = normalizePath(source);
|
|
142
|
+
if (normalizedRoot.startsWith("/")) return `${normalizedRoot.replace(/\/$/, "")}/${normalizedSource.replace(/^\//, "")}`;
|
|
143
|
+
return normalizePath(node_path.default.resolve(normalizedRoot, normalizedSource));
|
|
144
|
+
}
|
|
145
|
+
function normalizePath(value) {
|
|
146
|
+
return value.replace(/\\/g, "/");
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region packages/web-c/output-wc/src/generateEntry.ts
|
|
150
|
+
function generateWCEntry(options) {
|
|
151
|
+
const { root, component } = options;
|
|
152
|
+
const source = toAbsoluteImportPath(root, component.source);
|
|
153
|
+
return [
|
|
154
|
+
`import { ${component.exportName} } from ${JSON.stringify(source)};`,
|
|
155
|
+
"",
|
|
156
|
+
`export { ${component.exportName} };`,
|
|
157
|
+
""
|
|
158
|
+
].join("\n");
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region packages/web-c/output-wc/src/generateIndex.ts
|
|
162
|
+
function generateWCIndex(options) {
|
|
163
|
+
const { components, getFileName } = options;
|
|
164
|
+
const lines = [];
|
|
165
|
+
for (const component of components) {
|
|
166
|
+
const fileName = getFileName(component.tag);
|
|
167
|
+
lines.push(`export * from ${JSON.stringify("./" + fileName)};`);
|
|
168
|
+
}
|
|
169
|
+
lines.push("");
|
|
170
|
+
return lines.join("\n");
|
|
171
|
+
}
|
|
172
|
+
function getVirtualComponentId(component) {
|
|
173
|
+
return `zeus:wc:${component.tag}`;
|
|
174
|
+
}
|
|
175
|
+
function getVirtualIndexId() {
|
|
176
|
+
return "zeus:wc:index";
|
|
177
|
+
}
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region packages/web-c/output-wc/src/generateManifest.ts
|
|
180
|
+
function generateZeusComponentsManifest(manifest) {
|
|
181
|
+
return `${JSON.stringify(manifest, null, 2)}\n`;
|
|
182
|
+
}
|
|
183
|
+
//#endregion
|
|
184
|
+
//#region packages/web-c/output-wc/src/index.ts
|
|
185
|
+
function wc(options = {}) {
|
|
186
|
+
var _options$outDir, _options$stripPrefix, _options$manifestFile, _options$customElemen, _options$dts, _options$jsxDts, _options$index, _options$warnOnFileNa;
|
|
187
|
+
const normalized = {
|
|
188
|
+
outDir: (_options$outDir = options.outDir) !== null && _options$outDir !== void 0 ? _options$outDir : "wc",
|
|
189
|
+
stripPrefix: (_options$stripPrefix = options.stripPrefix) !== null && _options$stripPrefix !== void 0 ? _options$stripPrefix : false,
|
|
190
|
+
fileName: options.fileName,
|
|
191
|
+
manifestFile: (_options$manifestFile = options.manifestFile) !== null && _options$manifestFile !== void 0 ? _options$manifestFile : "zeus.components.json",
|
|
192
|
+
customElementsFile: (_options$customElemen = options.customElementsFile) !== null && _options$customElemen !== void 0 ? _options$customElemen : "custom-elements.json",
|
|
193
|
+
dts: (_options$dts = options.dts) !== null && _options$dts !== void 0 ? _options$dts : "auto",
|
|
194
|
+
jsxDts: (_options$jsxDts = options.jsxDts) !== null && _options$jsxDts !== void 0 ? _options$jsxDts : "auto",
|
|
195
|
+
index: (_options$index = options.index) !== null && _options$index !== void 0 ? _options$index : true,
|
|
196
|
+
warnOnFileNameCollision: (_options$warnOnFileNa = options.warnOnFileNameCollision) !== null && _options$warnOnFileNa !== void 0 ? _options$warnOnFileNa : true
|
|
197
|
+
};
|
|
198
|
+
let _outDir = normalized.outDir;
|
|
199
|
+
return {
|
|
200
|
+
name: "zeus-output-wc",
|
|
201
|
+
setup(ctx) {
|
|
202
|
+
_outDir = normalized.outDir;
|
|
203
|
+
ctx.outputs.register("wc", {
|
|
204
|
+
outDir: normalized.outDir,
|
|
205
|
+
stripPrefix: normalized.stripPrefix,
|
|
206
|
+
fileName: normalized.fileName ? (tag) => normalized.fileName(tag) : void 0
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
buildStart(ctx) {
|
|
210
|
+
if (!ctx.manifest) return;
|
|
211
|
+
checkFileNameCollisions(ctx.manifest.components, normalized, { warn: ctx.warn });
|
|
212
|
+
},
|
|
213
|
+
virtualModules(ctx) {
|
|
214
|
+
if (!ctx.manifest) return [];
|
|
215
|
+
const modules = [];
|
|
216
|
+
const hasOutputs = ctx.outputs.has("wc");
|
|
217
|
+
const getFileName = (tag) => {
|
|
218
|
+
if (hasOutputs) return ctx.outputs.getFileName("wc", tag);
|
|
219
|
+
return normalizeFileName(tag, normalized.stripPrefix, normalized.fileName);
|
|
220
|
+
};
|
|
221
|
+
const joinPath = (fileName) => {
|
|
222
|
+
if (hasOutputs) return ctx.outputs.join("wc", fileName);
|
|
223
|
+
return node_path.default.posix.join(_outDir, fileName);
|
|
224
|
+
};
|
|
225
|
+
for (const component of ctx.manifest.components) {
|
|
226
|
+
const fileName = joinPath(getFileName(component.tag));
|
|
227
|
+
modules.push({
|
|
228
|
+
id: getVirtualComponentId(component),
|
|
229
|
+
fileName,
|
|
230
|
+
code: generateWCEntry({
|
|
231
|
+
root: ctx.root,
|
|
232
|
+
component
|
|
233
|
+
})
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
if (normalized.index) modules.push({
|
|
237
|
+
id: getVirtualIndexId(),
|
|
238
|
+
fileName: joinPath("index.js"),
|
|
239
|
+
code: generateWCIndex({
|
|
240
|
+
components: ctx.manifest.components,
|
|
241
|
+
getFileName
|
|
242
|
+
})
|
|
243
|
+
});
|
|
244
|
+
return modules;
|
|
245
|
+
},
|
|
246
|
+
generateBundle(ctx) {
|
|
247
|
+
if (!ctx.manifest) return [];
|
|
248
|
+
const files = [];
|
|
249
|
+
if (normalized.manifestFile) files.push({
|
|
250
|
+
type: "asset",
|
|
251
|
+
fileName: normalized.manifestFile,
|
|
252
|
+
source: generateZeusComponentsManifest(ctx.manifest)
|
|
253
|
+
});
|
|
254
|
+
const hasOutputs = ctx.outputs.has("wc");
|
|
255
|
+
const getFileName = (tag) => {
|
|
256
|
+
if (hasOutputs) return ctx.outputs.getFileName("wc", tag);
|
|
257
|
+
return normalizeFileName(tag, normalized.stripPrefix, normalized.fileName);
|
|
258
|
+
};
|
|
259
|
+
const joinPath = (fileName) => {
|
|
260
|
+
if (hasOutputs) return ctx.outputs.join("wc", fileName);
|
|
261
|
+
return node_path.default.posix.join(_outDir, fileName);
|
|
262
|
+
};
|
|
263
|
+
if (normalized.customElementsFile) files.push({
|
|
264
|
+
type: "asset",
|
|
265
|
+
fileName: normalized.customElementsFile,
|
|
266
|
+
source: generateCustomElementsJson({
|
|
267
|
+
manifest: ctx.manifest,
|
|
268
|
+
getModulePath: (component) => joinPath(getFileName(component.tag))
|
|
269
|
+
})
|
|
270
|
+
});
|
|
271
|
+
const dts = (0, _zeus_js_bundler_plugin.resolvePluginDts)(normalized.dts, ctx);
|
|
272
|
+
const jsxDts = (0, _zeus_js_bundler_plugin.resolvePluginDts)(normalized.jsxDts, ctx);
|
|
273
|
+
if (dts || jsxDts) {
|
|
274
|
+
const dtsFiles = (0, _zeus_js_component_dts.generateWCDtsFiles)(ctx.manifest, {
|
|
275
|
+
outDir: _outDir,
|
|
276
|
+
stripPrefix: normalized.stripPrefix,
|
|
277
|
+
fileName: (tag) => getFileName(tag).replace(/\.js$/, ""),
|
|
278
|
+
perComponent: true,
|
|
279
|
+
index: dts,
|
|
280
|
+
jsx: jsxDts
|
|
281
|
+
});
|
|
282
|
+
for (const file of dtsFiles) files.push({
|
|
283
|
+
type: "asset",
|
|
284
|
+
fileName: file.fileName,
|
|
285
|
+
source: file.source
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return files;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function normalizeFileName(tag, stripPrefix, fileName) {
|
|
293
|
+
let name;
|
|
294
|
+
if (fileName) name = fileName(tag);
|
|
295
|
+
else {
|
|
296
|
+
name = tag;
|
|
297
|
+
if (stripPrefix && name.startsWith(stripPrefix)) name = name.slice(stripPrefix.length);
|
|
298
|
+
}
|
|
299
|
+
if (!name.endsWith(".js")) name = `${name}.js`;
|
|
300
|
+
return name;
|
|
301
|
+
}
|
|
302
|
+
function checkFileNameCollisions(components, options, reporter) {
|
|
303
|
+
if (options.warnOnFileNameCollision === false) return;
|
|
304
|
+
const map = /* @__PURE__ */ new Map();
|
|
305
|
+
for (const component of components) {
|
|
306
|
+
var _map$get;
|
|
307
|
+
const fileName = normalizeFileName(component.tag, options.stripPrefix, options.fileName);
|
|
308
|
+
const list = (_map$get = map.get(fileName)) !== null && _map$get !== void 0 ? _map$get : [];
|
|
309
|
+
list.push(component);
|
|
310
|
+
map.set(fileName, list);
|
|
311
|
+
}
|
|
312
|
+
for (const [fileName, list] of map) {
|
|
313
|
+
if (list.length <= 1) continue;
|
|
314
|
+
reporter.warn(`[zeus-output-wc] Multiple components map to "${fileName}": ${list.map((item) => item.tag).join(", ")}`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
//#endregion
|
|
318
|
+
exports.default = wc;
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* output-wc v0.1.0-beta.0
|
|
3
|
+
* (c) 2026 baicie
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
**/
|
|
6
|
+
Object.defineProperties(exports, {
|
|
7
|
+
__esModule: { value: true },
|
|
8
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
9
|
+
});
|
|
10
|
+
//#region \0rolldown/runtime.js
|
|
11
|
+
var __create = Object.create;
|
|
12
|
+
var __defProp = Object.defineProperty;
|
|
13
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
14
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
15
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
19
|
+
key = keys[i];
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
21
|
+
get: ((k) => from[k]).bind(null, key),
|
|
22
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
//#endregion
|
|
32
|
+
let node_path = require("node:path");
|
|
33
|
+
node_path = __toESM(node_path, 1);
|
|
34
|
+
let _zeus_js_bundler_plugin = require("@zeus-js/bundler-plugin");
|
|
35
|
+
let _zeus_js_component_dts = require("@zeus-js/component-dts");
|
|
36
|
+
//#region packages/web-c/output-wc/src/generateCustomElementsJson.ts
|
|
37
|
+
function generateCustomElementsJson(options) {
|
|
38
|
+
const { manifest, getModulePath } = options;
|
|
39
|
+
const result = {
|
|
40
|
+
schemaVersion: "1.0.0",
|
|
41
|
+
modules: manifest.components.map((component) => {
|
|
42
|
+
const modulePath = normalizeModulePath(getModulePath(component));
|
|
43
|
+
return {
|
|
44
|
+
kind: "javascript-module",
|
|
45
|
+
path: modulePath,
|
|
46
|
+
declarations: [{
|
|
47
|
+
kind: "class",
|
|
48
|
+
name: `${component.name}Element`,
|
|
49
|
+
tagName: component.tag,
|
|
50
|
+
customElement: true,
|
|
51
|
+
description: component.description,
|
|
52
|
+
attributes: generateAttributes(component),
|
|
53
|
+
members: generateMembers(component),
|
|
54
|
+
events: generateEvents(component),
|
|
55
|
+
slots: generateSlots(component),
|
|
56
|
+
cssParts: component.cssParts.map((name) => ({ name })),
|
|
57
|
+
cssProperties: component.cssVars.map((name) => ({ name }))
|
|
58
|
+
}],
|
|
59
|
+
exports: [{
|
|
60
|
+
kind: "js",
|
|
61
|
+
name: component.exportName,
|
|
62
|
+
declaration: {
|
|
63
|
+
name: `${component.name}Element`,
|
|
64
|
+
module: modulePath
|
|
65
|
+
}
|
|
66
|
+
}]
|
|
67
|
+
};
|
|
68
|
+
})
|
|
69
|
+
};
|
|
70
|
+
return `${JSON.stringify(result, null, 2)}\n`;
|
|
71
|
+
}
|
|
72
|
+
function generateAttributes(component) {
|
|
73
|
+
const result = [];
|
|
74
|
+
for (const [name, prop] of Object.entries(component.props)) {
|
|
75
|
+
const attrName = getAttributeName(name, prop);
|
|
76
|
+
if (attrName === false) continue;
|
|
77
|
+
result.push({
|
|
78
|
+
name: attrName,
|
|
79
|
+
description: prop.description,
|
|
80
|
+
type: { text: formatPropType(prop) },
|
|
81
|
+
default: prop.default === void 0 ? void 0 : JSON.stringify(prop.default)
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
function generateMembers(component) {
|
|
87
|
+
return Object.entries(component.props).map(([name, prop]) => {
|
|
88
|
+
return {
|
|
89
|
+
kind: "field",
|
|
90
|
+
name,
|
|
91
|
+
description: prop.description,
|
|
92
|
+
type: { text: formatPropType(prop) },
|
|
93
|
+
default: prop.default === void 0 ? void 0 : JSON.stringify(prop.default)
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function generateEvents(component) {
|
|
98
|
+
return Object.entries(component.events).map(([name, event]) => {
|
|
99
|
+
return {
|
|
100
|
+
name,
|
|
101
|
+
description: event.description,
|
|
102
|
+
type: { text: event.detail ? `CustomEvent<${formatDetailType(event.detail)}>` : "CustomEvent" }
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function generateSlots(component) {
|
|
107
|
+
return Object.entries(component.slots).map(([name, slot]) => {
|
|
108
|
+
return {
|
|
109
|
+
name: name === "default" ? "" : name,
|
|
110
|
+
description: slot.description
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function getAttributeName(propName, prop) {
|
|
115
|
+
if (prop.attr === false) return false;
|
|
116
|
+
if (typeof prop.attr === "string") return prop.attr;
|
|
117
|
+
return propName.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
118
|
+
}
|
|
119
|
+
function formatPropType(prop) {
|
|
120
|
+
var _prop$values;
|
|
121
|
+
if ((_prop$values = prop.values) === null || _prop$values === void 0 ? void 0 : _prop$values.length) return prop.values.map((value) => JSON.stringify(value)).join(" | ");
|
|
122
|
+
switch (prop.type) {
|
|
123
|
+
case "string": return "string";
|
|
124
|
+
case "number": return "number";
|
|
125
|
+
case "boolean": return "boolean";
|
|
126
|
+
case "array": return "unknown[]";
|
|
127
|
+
case "object": return "Record<string, unknown>";
|
|
128
|
+
default: return "unknown";
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function formatDetailType(detail) {
|
|
132
|
+
return `{ ${Object.entries(detail).map(([name, type]) => `${name}: ${type}`).join("; ")} }`;
|
|
133
|
+
}
|
|
134
|
+
function normalizeModulePath(value) {
|
|
135
|
+
return value.replace(/\\/g, "/");
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region packages/web-c/output-wc/src/imports.ts
|
|
139
|
+
function toAbsoluteImportPath(root, source) {
|
|
140
|
+
const normalizedRoot = normalizePath(root);
|
|
141
|
+
const normalizedSource = normalizePath(source);
|
|
142
|
+
if (normalizedRoot.startsWith("/")) return `${normalizedRoot.replace(/\/$/, "")}/${normalizedSource.replace(/^\//, "")}`;
|
|
143
|
+
return normalizePath(node_path.default.resolve(normalizedRoot, normalizedSource));
|
|
144
|
+
}
|
|
145
|
+
function normalizePath(value) {
|
|
146
|
+
return value.replace(/\\/g, "/");
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region packages/web-c/output-wc/src/generateEntry.ts
|
|
150
|
+
function generateWCEntry(options) {
|
|
151
|
+
const { root, component } = options;
|
|
152
|
+
const source = toAbsoluteImportPath(root, component.source);
|
|
153
|
+
return [
|
|
154
|
+
`import { ${component.exportName} } from ${JSON.stringify(source)};`,
|
|
155
|
+
"",
|
|
156
|
+
`export { ${component.exportName} };`,
|
|
157
|
+
""
|
|
158
|
+
].join("\n");
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region packages/web-c/output-wc/src/generateIndex.ts
|
|
162
|
+
function generateWCIndex(options) {
|
|
163
|
+
const { components, getFileName } = options;
|
|
164
|
+
const lines = [];
|
|
165
|
+
for (const component of components) {
|
|
166
|
+
const fileName = getFileName(component.tag);
|
|
167
|
+
lines.push(`export * from ${JSON.stringify("./" + fileName)};`);
|
|
168
|
+
}
|
|
169
|
+
lines.push("");
|
|
170
|
+
return lines.join("\n");
|
|
171
|
+
}
|
|
172
|
+
function getVirtualComponentId(component) {
|
|
173
|
+
return `zeus:wc:${component.tag}`;
|
|
174
|
+
}
|
|
175
|
+
function getVirtualIndexId() {
|
|
176
|
+
return "zeus:wc:index";
|
|
177
|
+
}
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region packages/web-c/output-wc/src/generateManifest.ts
|
|
180
|
+
function generateZeusComponentsManifest(manifest) {
|
|
181
|
+
return `${JSON.stringify(manifest, null, 2)}\n`;
|
|
182
|
+
}
|
|
183
|
+
//#endregion
|
|
184
|
+
//#region packages/web-c/output-wc/src/index.ts
|
|
185
|
+
function wc(options = {}) {
|
|
186
|
+
var _options$outDir, _options$stripPrefix, _options$manifestFile, _options$customElemen, _options$dts, _options$jsxDts, _options$index, _options$warnOnFileNa;
|
|
187
|
+
const normalized = {
|
|
188
|
+
outDir: (_options$outDir = options.outDir) !== null && _options$outDir !== void 0 ? _options$outDir : "wc",
|
|
189
|
+
stripPrefix: (_options$stripPrefix = options.stripPrefix) !== null && _options$stripPrefix !== void 0 ? _options$stripPrefix : false,
|
|
190
|
+
fileName: options.fileName,
|
|
191
|
+
manifestFile: (_options$manifestFile = options.manifestFile) !== null && _options$manifestFile !== void 0 ? _options$manifestFile : "zeus.components.json",
|
|
192
|
+
customElementsFile: (_options$customElemen = options.customElementsFile) !== null && _options$customElemen !== void 0 ? _options$customElemen : "custom-elements.json",
|
|
193
|
+
dts: (_options$dts = options.dts) !== null && _options$dts !== void 0 ? _options$dts : "auto",
|
|
194
|
+
jsxDts: (_options$jsxDts = options.jsxDts) !== null && _options$jsxDts !== void 0 ? _options$jsxDts : "auto",
|
|
195
|
+
index: (_options$index = options.index) !== null && _options$index !== void 0 ? _options$index : true,
|
|
196
|
+
warnOnFileNameCollision: (_options$warnOnFileNa = options.warnOnFileNameCollision) !== null && _options$warnOnFileNa !== void 0 ? _options$warnOnFileNa : true
|
|
197
|
+
};
|
|
198
|
+
let _outDir = normalized.outDir;
|
|
199
|
+
return {
|
|
200
|
+
name: "zeus-output-wc",
|
|
201
|
+
setup(ctx) {
|
|
202
|
+
_outDir = normalized.outDir;
|
|
203
|
+
ctx.outputs.register("wc", {
|
|
204
|
+
outDir: normalized.outDir,
|
|
205
|
+
stripPrefix: normalized.stripPrefix,
|
|
206
|
+
fileName: normalized.fileName ? (tag) => normalized.fileName(tag) : void 0
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
buildStart(ctx) {
|
|
210
|
+
if (!ctx.manifest) return;
|
|
211
|
+
checkFileNameCollisions(ctx.manifest.components, normalized, { warn: ctx.warn });
|
|
212
|
+
},
|
|
213
|
+
virtualModules(ctx) {
|
|
214
|
+
if (!ctx.manifest) return [];
|
|
215
|
+
const modules = [];
|
|
216
|
+
const hasOutputs = ctx.outputs.has("wc");
|
|
217
|
+
const getFileName = (tag) => {
|
|
218
|
+
if (hasOutputs) return ctx.outputs.getFileName("wc", tag);
|
|
219
|
+
return normalizeFileName(tag, normalized.stripPrefix, normalized.fileName);
|
|
220
|
+
};
|
|
221
|
+
const joinPath = (fileName) => {
|
|
222
|
+
if (hasOutputs) return ctx.outputs.join("wc", fileName);
|
|
223
|
+
return node_path.default.posix.join(_outDir, fileName);
|
|
224
|
+
};
|
|
225
|
+
for (const component of ctx.manifest.components) {
|
|
226
|
+
const fileName = joinPath(getFileName(component.tag));
|
|
227
|
+
modules.push({
|
|
228
|
+
id: getVirtualComponentId(component),
|
|
229
|
+
fileName,
|
|
230
|
+
code: generateWCEntry({
|
|
231
|
+
root: ctx.root,
|
|
232
|
+
component
|
|
233
|
+
})
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
if (normalized.index) modules.push({
|
|
237
|
+
id: getVirtualIndexId(),
|
|
238
|
+
fileName: joinPath("index.js"),
|
|
239
|
+
code: generateWCIndex({
|
|
240
|
+
components: ctx.manifest.components,
|
|
241
|
+
getFileName
|
|
242
|
+
})
|
|
243
|
+
});
|
|
244
|
+
return modules;
|
|
245
|
+
},
|
|
246
|
+
generateBundle(ctx) {
|
|
247
|
+
if (!ctx.manifest) return [];
|
|
248
|
+
const files = [];
|
|
249
|
+
if (normalized.manifestFile) files.push({
|
|
250
|
+
type: "asset",
|
|
251
|
+
fileName: normalized.manifestFile,
|
|
252
|
+
source: generateZeusComponentsManifest(ctx.manifest)
|
|
253
|
+
});
|
|
254
|
+
const hasOutputs = ctx.outputs.has("wc");
|
|
255
|
+
const getFileName = (tag) => {
|
|
256
|
+
if (hasOutputs) return ctx.outputs.getFileName("wc", tag);
|
|
257
|
+
return normalizeFileName(tag, normalized.stripPrefix, normalized.fileName);
|
|
258
|
+
};
|
|
259
|
+
const joinPath = (fileName) => {
|
|
260
|
+
if (hasOutputs) return ctx.outputs.join("wc", fileName);
|
|
261
|
+
return node_path.default.posix.join(_outDir, fileName);
|
|
262
|
+
};
|
|
263
|
+
if (normalized.customElementsFile) files.push({
|
|
264
|
+
type: "asset",
|
|
265
|
+
fileName: normalized.customElementsFile,
|
|
266
|
+
source: generateCustomElementsJson({
|
|
267
|
+
manifest: ctx.manifest,
|
|
268
|
+
getModulePath: (component) => joinPath(getFileName(component.tag))
|
|
269
|
+
})
|
|
270
|
+
});
|
|
271
|
+
const dts = (0, _zeus_js_bundler_plugin.resolvePluginDts)(normalized.dts, ctx);
|
|
272
|
+
const jsxDts = (0, _zeus_js_bundler_plugin.resolvePluginDts)(normalized.jsxDts, ctx);
|
|
273
|
+
if (dts || jsxDts) {
|
|
274
|
+
const dtsFiles = (0, _zeus_js_component_dts.generateWCDtsFiles)(ctx.manifest, {
|
|
275
|
+
outDir: _outDir,
|
|
276
|
+
stripPrefix: normalized.stripPrefix,
|
|
277
|
+
fileName: (tag) => getFileName(tag).replace(/\.js$/, ""),
|
|
278
|
+
perComponent: true,
|
|
279
|
+
index: dts,
|
|
280
|
+
jsx: jsxDts
|
|
281
|
+
});
|
|
282
|
+
for (const file of dtsFiles) files.push({
|
|
283
|
+
type: "asset",
|
|
284
|
+
fileName: file.fileName,
|
|
285
|
+
source: file.source
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return files;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function normalizeFileName(tag, stripPrefix, fileName) {
|
|
293
|
+
let name;
|
|
294
|
+
if (fileName) name = fileName(tag);
|
|
295
|
+
else {
|
|
296
|
+
name = tag;
|
|
297
|
+
if (stripPrefix && name.startsWith(stripPrefix)) name = name.slice(stripPrefix.length);
|
|
298
|
+
}
|
|
299
|
+
if (!name.endsWith(".js")) name = `${name}.js`;
|
|
300
|
+
return name;
|
|
301
|
+
}
|
|
302
|
+
function checkFileNameCollisions(components, options, reporter) {
|
|
303
|
+
if (options.warnOnFileNameCollision === false) return;
|
|
304
|
+
const map = /* @__PURE__ */ new Map();
|
|
305
|
+
for (const component of components) {
|
|
306
|
+
var _map$get;
|
|
307
|
+
const fileName = normalizeFileName(component.tag, options.stripPrefix, options.fileName);
|
|
308
|
+
const list = (_map$get = map.get(fileName)) !== null && _map$get !== void 0 ? _map$get : [];
|
|
309
|
+
list.push(component);
|
|
310
|
+
map.set(fileName, list);
|
|
311
|
+
}
|
|
312
|
+
for (const [fileName, list] of map) {
|
|
313
|
+
if (list.length <= 1) continue;
|
|
314
|
+
reporter.warn(`[zeus-output-wc] Multiple components map to "${fileName}": ${list.map((item) => item.tag).join(", ")}`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
//#endregion
|
|
318
|
+
exports.default = wc;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { DtsMode, ZeusComponentPlugin } from '@zeus-js/bundler-plugin';
|
|
2
|
+
|
|
3
|
+
export interface OutputWCOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Web Component output directory.
|
|
6
|
+
*
|
|
7
|
+
* @default 'wc'
|
|
8
|
+
*/
|
|
9
|
+
outDir?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Strip tag prefix for file name.
|
|
12
|
+
*
|
|
13
|
+
* Example:
|
|
14
|
+
* z-button -> button.js
|
|
15
|
+
*
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
stripPrefix?: string | false;
|
|
19
|
+
/**
|
|
20
|
+
* Custom file name.
|
|
21
|
+
*/
|
|
22
|
+
fileName?: (tag: string) => string;
|
|
23
|
+
/**
|
|
24
|
+
* Component manifest file.
|
|
25
|
+
*
|
|
26
|
+
* @default 'zeus.components.json'
|
|
27
|
+
*/
|
|
28
|
+
manifestFile?: string | false;
|
|
29
|
+
/**
|
|
30
|
+
* Custom Elements Manifest file.
|
|
31
|
+
*
|
|
32
|
+
* @default 'custom-elements.json'
|
|
33
|
+
*/
|
|
34
|
+
customElementsFile?: string | false;
|
|
35
|
+
/**
|
|
36
|
+
* Generate WC d.ts.
|
|
37
|
+
*
|
|
38
|
+
* @default 'auto'
|
|
39
|
+
*/
|
|
40
|
+
dts?: DtsMode;
|
|
41
|
+
/**
|
|
42
|
+
* Generate JSX IntrinsicElements d.ts.
|
|
43
|
+
*
|
|
44
|
+
* @default 'auto'
|
|
45
|
+
*/
|
|
46
|
+
jsxDts?: DtsMode;
|
|
47
|
+
/**
|
|
48
|
+
* Generate wc/index.js.
|
|
49
|
+
*
|
|
50
|
+
* @default true
|
|
51
|
+
*/
|
|
52
|
+
index?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Whether to warn when two components map to the same file name.
|
|
55
|
+
*
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
warnOnFileNameCollision?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export declare function wc(options?: OutputWCOptions): ZeusComponentPlugin;
|
|
62
|
+
|
|
63
|
+
export { wc as default };
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* output-wc v0.1.0-beta.0
|
|
3
|
+
* (c) 2026 baicie
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
**/
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { resolvePluginDts } from "@zeus-js/bundler-plugin";
|
|
8
|
+
import { generateWCDtsFiles } from "@zeus-js/component-dts";
|
|
9
|
+
//#region packages/web-c/output-wc/src/generateCustomElementsJson.ts
|
|
10
|
+
function generateCustomElementsJson(options) {
|
|
11
|
+
const { manifest, getModulePath } = options;
|
|
12
|
+
const result = {
|
|
13
|
+
schemaVersion: "1.0.0",
|
|
14
|
+
modules: manifest.components.map((component) => {
|
|
15
|
+
const modulePath = normalizeModulePath(getModulePath(component));
|
|
16
|
+
return {
|
|
17
|
+
kind: "javascript-module",
|
|
18
|
+
path: modulePath,
|
|
19
|
+
declarations: [{
|
|
20
|
+
kind: "class",
|
|
21
|
+
name: `${component.name}Element`,
|
|
22
|
+
tagName: component.tag,
|
|
23
|
+
customElement: true,
|
|
24
|
+
description: component.description,
|
|
25
|
+
attributes: generateAttributes(component),
|
|
26
|
+
members: generateMembers(component),
|
|
27
|
+
events: generateEvents(component),
|
|
28
|
+
slots: generateSlots(component),
|
|
29
|
+
cssParts: component.cssParts.map((name) => ({ name })),
|
|
30
|
+
cssProperties: component.cssVars.map((name) => ({ name }))
|
|
31
|
+
}],
|
|
32
|
+
exports: [{
|
|
33
|
+
kind: "js",
|
|
34
|
+
name: component.exportName,
|
|
35
|
+
declaration: {
|
|
36
|
+
name: `${component.name}Element`,
|
|
37
|
+
module: modulePath
|
|
38
|
+
}
|
|
39
|
+
}]
|
|
40
|
+
};
|
|
41
|
+
})
|
|
42
|
+
};
|
|
43
|
+
return `${JSON.stringify(result, null, 2)}\n`;
|
|
44
|
+
}
|
|
45
|
+
function generateAttributes(component) {
|
|
46
|
+
const result = [];
|
|
47
|
+
for (const [name, prop] of Object.entries(component.props)) {
|
|
48
|
+
const attrName = getAttributeName(name, prop);
|
|
49
|
+
if (attrName === false) continue;
|
|
50
|
+
result.push({
|
|
51
|
+
name: attrName,
|
|
52
|
+
description: prop.description,
|
|
53
|
+
type: { text: formatPropType(prop) },
|
|
54
|
+
default: prop.default === void 0 ? void 0 : JSON.stringify(prop.default)
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
function generateMembers(component) {
|
|
60
|
+
return Object.entries(component.props).map(([name, prop]) => {
|
|
61
|
+
return {
|
|
62
|
+
kind: "field",
|
|
63
|
+
name,
|
|
64
|
+
description: prop.description,
|
|
65
|
+
type: { text: formatPropType(prop) },
|
|
66
|
+
default: prop.default === void 0 ? void 0 : JSON.stringify(prop.default)
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function generateEvents(component) {
|
|
71
|
+
return Object.entries(component.events).map(([name, event]) => {
|
|
72
|
+
return {
|
|
73
|
+
name,
|
|
74
|
+
description: event.description,
|
|
75
|
+
type: { text: event.detail ? `CustomEvent<${formatDetailType(event.detail)}>` : "CustomEvent" }
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function generateSlots(component) {
|
|
80
|
+
return Object.entries(component.slots).map(([name, slot]) => {
|
|
81
|
+
return {
|
|
82
|
+
name: name === "default" ? "" : name,
|
|
83
|
+
description: slot.description
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function getAttributeName(propName, prop) {
|
|
88
|
+
if (prop.attr === false) return false;
|
|
89
|
+
if (typeof prop.attr === "string") return prop.attr;
|
|
90
|
+
return propName.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
91
|
+
}
|
|
92
|
+
function formatPropType(prop) {
|
|
93
|
+
var _prop$values;
|
|
94
|
+
if ((_prop$values = prop.values) === null || _prop$values === void 0 ? void 0 : _prop$values.length) return prop.values.map((value) => JSON.stringify(value)).join(" | ");
|
|
95
|
+
switch (prop.type) {
|
|
96
|
+
case "string": return "string";
|
|
97
|
+
case "number": return "number";
|
|
98
|
+
case "boolean": return "boolean";
|
|
99
|
+
case "array": return "unknown[]";
|
|
100
|
+
case "object": return "Record<string, unknown>";
|
|
101
|
+
default: return "unknown";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function formatDetailType(detail) {
|
|
105
|
+
return `{ ${Object.entries(detail).map(([name, type]) => `${name}: ${type}`).join("; ")} }`;
|
|
106
|
+
}
|
|
107
|
+
function normalizeModulePath(value) {
|
|
108
|
+
return value.replace(/\\/g, "/");
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region packages/web-c/output-wc/src/imports.ts
|
|
112
|
+
function toAbsoluteImportPath(root, source) {
|
|
113
|
+
const normalizedRoot = normalizePath(root);
|
|
114
|
+
const normalizedSource = normalizePath(source);
|
|
115
|
+
if (normalizedRoot.startsWith("/")) return `${normalizedRoot.replace(/\/$/, "")}/${normalizedSource.replace(/^\//, "")}`;
|
|
116
|
+
return normalizePath(path.resolve(normalizedRoot, normalizedSource));
|
|
117
|
+
}
|
|
118
|
+
function normalizePath(value) {
|
|
119
|
+
return value.replace(/\\/g, "/");
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region packages/web-c/output-wc/src/generateEntry.ts
|
|
123
|
+
function generateWCEntry(options) {
|
|
124
|
+
const { root, component } = options;
|
|
125
|
+
const source = toAbsoluteImportPath(root, component.source);
|
|
126
|
+
return [
|
|
127
|
+
`import { ${component.exportName} } from ${JSON.stringify(source)};`,
|
|
128
|
+
"",
|
|
129
|
+
`export { ${component.exportName} };`,
|
|
130
|
+
""
|
|
131
|
+
].join("\n");
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region packages/web-c/output-wc/src/generateIndex.ts
|
|
135
|
+
function generateWCIndex(options) {
|
|
136
|
+
const { components, getFileName } = options;
|
|
137
|
+
const lines = [];
|
|
138
|
+
for (const component of components) {
|
|
139
|
+
const fileName = getFileName(component.tag);
|
|
140
|
+
lines.push(`export * from ${JSON.stringify("./" + fileName)};`);
|
|
141
|
+
}
|
|
142
|
+
lines.push("");
|
|
143
|
+
return lines.join("\n");
|
|
144
|
+
}
|
|
145
|
+
function getVirtualComponentId(component) {
|
|
146
|
+
return `zeus:wc:${component.tag}`;
|
|
147
|
+
}
|
|
148
|
+
function getVirtualIndexId() {
|
|
149
|
+
return "zeus:wc:index";
|
|
150
|
+
}
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region packages/web-c/output-wc/src/generateManifest.ts
|
|
153
|
+
function generateZeusComponentsManifest(manifest) {
|
|
154
|
+
return `${JSON.stringify(manifest, null, 2)}\n`;
|
|
155
|
+
}
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region packages/web-c/output-wc/src/index.ts
|
|
158
|
+
function wc(options = {}) {
|
|
159
|
+
var _options$outDir, _options$stripPrefix, _options$manifestFile, _options$customElemen, _options$dts, _options$jsxDts, _options$index, _options$warnOnFileNa;
|
|
160
|
+
const normalized = {
|
|
161
|
+
outDir: (_options$outDir = options.outDir) !== null && _options$outDir !== void 0 ? _options$outDir : "wc",
|
|
162
|
+
stripPrefix: (_options$stripPrefix = options.stripPrefix) !== null && _options$stripPrefix !== void 0 ? _options$stripPrefix : false,
|
|
163
|
+
fileName: options.fileName,
|
|
164
|
+
manifestFile: (_options$manifestFile = options.manifestFile) !== null && _options$manifestFile !== void 0 ? _options$manifestFile : "zeus.components.json",
|
|
165
|
+
customElementsFile: (_options$customElemen = options.customElementsFile) !== null && _options$customElemen !== void 0 ? _options$customElemen : "custom-elements.json",
|
|
166
|
+
dts: (_options$dts = options.dts) !== null && _options$dts !== void 0 ? _options$dts : "auto",
|
|
167
|
+
jsxDts: (_options$jsxDts = options.jsxDts) !== null && _options$jsxDts !== void 0 ? _options$jsxDts : "auto",
|
|
168
|
+
index: (_options$index = options.index) !== null && _options$index !== void 0 ? _options$index : true,
|
|
169
|
+
warnOnFileNameCollision: (_options$warnOnFileNa = options.warnOnFileNameCollision) !== null && _options$warnOnFileNa !== void 0 ? _options$warnOnFileNa : true
|
|
170
|
+
};
|
|
171
|
+
let _outDir = normalized.outDir;
|
|
172
|
+
return {
|
|
173
|
+
name: "zeus-output-wc",
|
|
174
|
+
setup(ctx) {
|
|
175
|
+
_outDir = normalized.outDir;
|
|
176
|
+
ctx.outputs.register("wc", {
|
|
177
|
+
outDir: normalized.outDir,
|
|
178
|
+
stripPrefix: normalized.stripPrefix,
|
|
179
|
+
fileName: normalized.fileName ? (tag) => normalized.fileName(tag) : void 0
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
buildStart(ctx) {
|
|
183
|
+
if (!ctx.manifest) return;
|
|
184
|
+
checkFileNameCollisions(ctx.manifest.components, normalized, { warn: ctx.warn });
|
|
185
|
+
},
|
|
186
|
+
virtualModules(ctx) {
|
|
187
|
+
if (!ctx.manifest) return [];
|
|
188
|
+
const modules = [];
|
|
189
|
+
const hasOutputs = ctx.outputs.has("wc");
|
|
190
|
+
const getFileName = (tag) => {
|
|
191
|
+
if (hasOutputs) return ctx.outputs.getFileName("wc", tag);
|
|
192
|
+
return normalizeFileName(tag, normalized.stripPrefix, normalized.fileName);
|
|
193
|
+
};
|
|
194
|
+
const joinPath = (fileName) => {
|
|
195
|
+
if (hasOutputs) return ctx.outputs.join("wc", fileName);
|
|
196
|
+
return path.posix.join(_outDir, fileName);
|
|
197
|
+
};
|
|
198
|
+
for (const component of ctx.manifest.components) {
|
|
199
|
+
const fileName = joinPath(getFileName(component.tag));
|
|
200
|
+
modules.push({
|
|
201
|
+
id: getVirtualComponentId(component),
|
|
202
|
+
fileName,
|
|
203
|
+
code: generateWCEntry({
|
|
204
|
+
root: ctx.root,
|
|
205
|
+
component
|
|
206
|
+
})
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
if (normalized.index) modules.push({
|
|
210
|
+
id: getVirtualIndexId(),
|
|
211
|
+
fileName: joinPath("index.js"),
|
|
212
|
+
code: generateWCIndex({
|
|
213
|
+
components: ctx.manifest.components,
|
|
214
|
+
getFileName
|
|
215
|
+
})
|
|
216
|
+
});
|
|
217
|
+
return modules;
|
|
218
|
+
},
|
|
219
|
+
generateBundle(ctx) {
|
|
220
|
+
if (!ctx.manifest) return [];
|
|
221
|
+
const files = [];
|
|
222
|
+
if (normalized.manifestFile) files.push({
|
|
223
|
+
type: "asset",
|
|
224
|
+
fileName: normalized.manifestFile,
|
|
225
|
+
source: generateZeusComponentsManifest(ctx.manifest)
|
|
226
|
+
});
|
|
227
|
+
const hasOutputs = ctx.outputs.has("wc");
|
|
228
|
+
const getFileName = (tag) => {
|
|
229
|
+
if (hasOutputs) return ctx.outputs.getFileName("wc", tag);
|
|
230
|
+
return normalizeFileName(tag, normalized.stripPrefix, normalized.fileName);
|
|
231
|
+
};
|
|
232
|
+
const joinPath = (fileName) => {
|
|
233
|
+
if (hasOutputs) return ctx.outputs.join("wc", fileName);
|
|
234
|
+
return path.posix.join(_outDir, fileName);
|
|
235
|
+
};
|
|
236
|
+
if (normalized.customElementsFile) files.push({
|
|
237
|
+
type: "asset",
|
|
238
|
+
fileName: normalized.customElementsFile,
|
|
239
|
+
source: generateCustomElementsJson({
|
|
240
|
+
manifest: ctx.manifest,
|
|
241
|
+
getModulePath: (component) => joinPath(getFileName(component.tag))
|
|
242
|
+
})
|
|
243
|
+
});
|
|
244
|
+
const dts = resolvePluginDts(normalized.dts, ctx);
|
|
245
|
+
const jsxDts = resolvePluginDts(normalized.jsxDts, ctx);
|
|
246
|
+
if (dts || jsxDts) {
|
|
247
|
+
const dtsFiles = generateWCDtsFiles(ctx.manifest, {
|
|
248
|
+
outDir: _outDir,
|
|
249
|
+
stripPrefix: normalized.stripPrefix,
|
|
250
|
+
fileName: (tag) => getFileName(tag).replace(/\.js$/, ""),
|
|
251
|
+
perComponent: true,
|
|
252
|
+
index: dts,
|
|
253
|
+
jsx: jsxDts
|
|
254
|
+
});
|
|
255
|
+
for (const file of dtsFiles) files.push({
|
|
256
|
+
type: "asset",
|
|
257
|
+
fileName: file.fileName,
|
|
258
|
+
source: file.source
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
return files;
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function normalizeFileName(tag, stripPrefix, fileName) {
|
|
266
|
+
let name;
|
|
267
|
+
if (fileName) name = fileName(tag);
|
|
268
|
+
else {
|
|
269
|
+
name = tag;
|
|
270
|
+
if (stripPrefix && name.startsWith(stripPrefix)) name = name.slice(stripPrefix.length);
|
|
271
|
+
}
|
|
272
|
+
if (!name.endsWith(".js")) name = `${name}.js`;
|
|
273
|
+
return name;
|
|
274
|
+
}
|
|
275
|
+
function checkFileNameCollisions(components, options, reporter) {
|
|
276
|
+
if (options.warnOnFileNameCollision === false) return;
|
|
277
|
+
const map = /* @__PURE__ */ new Map();
|
|
278
|
+
for (const component of components) {
|
|
279
|
+
var _map$get;
|
|
280
|
+
const fileName = normalizeFileName(component.tag, options.stripPrefix, options.fileName);
|
|
281
|
+
const list = (_map$get = map.get(fileName)) !== null && _map$get !== void 0 ? _map$get : [];
|
|
282
|
+
list.push(component);
|
|
283
|
+
map.set(fileName, list);
|
|
284
|
+
}
|
|
285
|
+
for (const [fileName, list] of map) {
|
|
286
|
+
if (list.length <= 1) continue;
|
|
287
|
+
reporter.warn(`[zeus-output-wc] Multiple components map to "${fileName}": ${list.map((item) => item.tag).join(", ")}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
//#endregion
|
|
291
|
+
export { wc as default };
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zeus-js/output-wc",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Zeus Web Component output plugin",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "dist/output-wc.esm-bundler.js",
|
|
8
|
+
"types": "dist/output-wc.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"index.js",
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/output-wc.d.ts",
|
|
16
|
+
"node": {
|
|
17
|
+
"production": "./dist/output-wc.cjs.prod.js",
|
|
18
|
+
"development": "./dist/output-wc.cjs.js",
|
|
19
|
+
"default": "./index.js"
|
|
20
|
+
},
|
|
21
|
+
"module": "./dist/output-wc.esm-bundler.js",
|
|
22
|
+
"import": "./dist/output-wc.esm-bundler.js",
|
|
23
|
+
"require": "./index.js"
|
|
24
|
+
},
|
|
25
|
+
"./*": "./*"
|
|
26
|
+
},
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/baicie/zeus"
|
|
31
|
+
},
|
|
32
|
+
"buildOptions": {
|
|
33
|
+
"name": "ZeusOutputWC",
|
|
34
|
+
"formats": [
|
|
35
|
+
"esm-bundler",
|
|
36
|
+
"cjs"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@zeus-js/bundler-plugin": "0.1.0-beta.0",
|
|
41
|
+
"@zeus-js/component-dts": "0.1.0-beta.0",
|
|
42
|
+
"@zeus-js/component-analyzer": "0.1.0-beta.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"rollup": "^4.0.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"rollup": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"keywords": [
|
|
53
|
+
"zeus",
|
|
54
|
+
"web-components",
|
|
55
|
+
"custom-elements"
|
|
56
|
+
],
|
|
57
|
+
"author": "Baicie",
|
|
58
|
+
"license": "MIT"
|
|
59
|
+
}
|