sveld 0.13.2 → 0.14.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/CHANGELOG.md +13 -0
- package/LICENSE +1 -1
- package/README.md +4 -3
- package/cli.js +1 -1
- package/lib/ComponentParser.d.ts +7 -2
- package/lib/ComponentParser.js +102 -11
- package/lib/create-exports.d.ts +2 -1
- package/lib/create-exports.js +13 -4
- package/lib/get-svelte-entry.js +4 -4
- package/lib/rollup-plugin.js +15 -8
- package/lib/writer/Writer.js +6 -13
- package/lib/writer/writer-json.js +1 -1
- package/lib/writer/writer-markdown.d.ts +2 -1
- package/lib/writer/writer-markdown.js +7 -6
- package/lib/writer/writer-ts-definitions.js +30 -10
- package/package.json +25 -26
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
10
10
|
- add isAccessor field to API
|
|
11
11
|
- update Markdown writer to generate a separate table for accessors -->
|
|
12
12
|
|
|
13
|
+
## [0.14.0](https://github.com/carbon-design-system/sveld/releases/tag/v0.14.0) - 2022-04-09
|
|
14
|
+
|
|
15
|
+
- add `sveltekit:prefetch`, `sveltekit:noscroll` attributes to props that extend `a` attributes
|
|
16
|
+
- use type-only imports for `SvelteComponentTyped` and extended props
|
|
17
|
+
|
|
18
|
+
## [0.13.4](https://github.com/carbon-design-system/sveld/releases/tag/v0.13.4) - 2022-02-26
|
|
19
|
+
|
|
20
|
+
- use file name as module name if library only has a single default export
|
|
21
|
+
|
|
22
|
+
## [0.13.3](https://github.com/carbon-design-system/sveld/releases/tag/v0.13.3) - 2022-02-13
|
|
23
|
+
|
|
24
|
+
- component module exports should not be recognized as accessors
|
|
25
|
+
|
|
13
26
|
## [0.13.2](https://github.com/carbon-design-system/sveld/releases/tag/v0.13.2) - 2022-02-10
|
|
14
27
|
|
|
15
28
|
- do not wrap TS `@event` detail in `CustomEvent` if type contains `CustomEvent`
|
package/LICENSE
CHANGED
|
@@ -186,7 +186,7 @@
|
|
|
186
186
|
same "printed page" as the copyright notice for easier
|
|
187
187
|
identification within third-party archives.
|
|
188
188
|
|
|
189
|
-
Copyright
|
|
189
|
+
Copyright 2020 IBM
|
|
190
190
|
|
|
191
191
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
192
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -173,11 +173,11 @@ export default {
|
|
|
173
173
|
};
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
-
When building the library
|
|
176
|
+
When building the library, TypeScript definitions are emitted to the `types` folder by default.
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
Customize the output folder using the `typesOptions.outDir` option.
|
|
179
179
|
|
|
180
|
-
|
|
180
|
+
The following example emits the output to the `dist` folder:
|
|
181
181
|
|
|
182
182
|
```diff
|
|
183
183
|
sveld({
|
|
@@ -190,6 +190,7 @@ sveld({
|
|
|
190
190
|
The [integration](integration) folder contains example set-ups:
|
|
191
191
|
|
|
192
192
|
- [single-export](integration/single-export): library that exports one component
|
|
193
|
+
- [single-export-default-only](integration/single-export-default-only): library that exports one component using the concise `export { default } ...` syntax
|
|
193
194
|
- [multi-export](integration/multi-export): multi-component library without JSDoc annotations (types are inferred)
|
|
194
195
|
- [multi-export-typed](integration/multi-export-typed): multi-component library with JSDoc annotations
|
|
195
196
|
- [multi-export-typed-ts-only](integration/multi-export-typed-ts-only): multi-component library that only generates TS definitions
|
package/cli.js
CHANGED
package/lib/ComponentParser.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as commentParser from "comment-parser";
|
|
2
1
|
interface ComponentParserDiagnostics {
|
|
3
2
|
moduleName: string;
|
|
4
3
|
filePath: string;
|
|
@@ -34,7 +33,9 @@ interface DispatchedEvent {
|
|
|
34
33
|
detail?: any;
|
|
35
34
|
}
|
|
36
35
|
declare type ComponentEvent = ForwardedEvent | DispatchedEvent;
|
|
37
|
-
interface TypeDef
|
|
36
|
+
interface TypeDef {
|
|
37
|
+
type: string;
|
|
38
|
+
name: string;
|
|
38
39
|
description?: string;
|
|
39
40
|
ts: string;
|
|
40
41
|
}
|
|
@@ -53,6 +54,7 @@ interface Extends {
|
|
|
53
54
|
}
|
|
54
55
|
export interface ParsedComponent {
|
|
55
56
|
props: ComponentProp[];
|
|
57
|
+
moduleExports: ComponentProp[];
|
|
56
58
|
slots: ComponentSlot[];
|
|
57
59
|
events: ComponentEvent[];
|
|
58
60
|
typedefs: TypeDef[];
|
|
@@ -64,12 +66,14 @@ export default class ComponentParser {
|
|
|
64
66
|
private options?;
|
|
65
67
|
private source?;
|
|
66
68
|
private compiled?;
|
|
69
|
+
private parsed?;
|
|
67
70
|
private rest_props?;
|
|
68
71
|
private extends?;
|
|
69
72
|
private componentComment?;
|
|
70
73
|
private readonly reactive_vars;
|
|
71
74
|
private readonly vars;
|
|
72
75
|
private readonly props;
|
|
76
|
+
private readonly moduleExports;
|
|
73
77
|
private readonly slots;
|
|
74
78
|
private readonly events;
|
|
75
79
|
private readonly typedefs;
|
|
@@ -81,6 +85,7 @@ export default class ComponentParser {
|
|
|
81
85
|
private sourceAtPos;
|
|
82
86
|
private collectReactiveVars;
|
|
83
87
|
private addProp;
|
|
88
|
+
private addModuleExport;
|
|
84
89
|
private aliasType;
|
|
85
90
|
private addSlot;
|
|
86
91
|
private addDispatchedEvent;
|
package/lib/ComponentParser.js
CHANGED
|
@@ -29,6 +29,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
29
29
|
this.reactive_vars = new Set();
|
|
30
30
|
this.vars = new Set();
|
|
31
31
|
this.props = new Map();
|
|
32
|
+
this.moduleExports = new Map();
|
|
32
33
|
this.slots = new Map();
|
|
33
34
|
this.events = new Map();
|
|
34
35
|
this.typedefs = new Map();
|
|
@@ -80,6 +81,17 @@ var ComponentParser = /** @class */ (function () {
|
|
|
80
81
|
this.props.set(prop_name, data);
|
|
81
82
|
}
|
|
82
83
|
};
|
|
84
|
+
ComponentParser.prototype.addModuleExport = function (prop_name, data) {
|
|
85
|
+
if (ComponentParser.assignValue(prop_name) === undefined)
|
|
86
|
+
return;
|
|
87
|
+
if (this.moduleExports.has(prop_name)) {
|
|
88
|
+
var existing_slot = this.moduleExports.get(prop_name);
|
|
89
|
+
this.moduleExports.set(prop_name, __assign(__assign({}, existing_slot), data));
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
this.moduleExports.set(prop_name, data);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
83
95
|
ComponentParser.prototype.aliasType = function (type) {
|
|
84
96
|
if (type === "*")
|
|
85
97
|
return "any";
|
|
@@ -120,7 +132,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
120
132
|
};
|
|
121
133
|
ComponentParser.prototype.parseCustomTypes = function () {
|
|
122
134
|
var _this = this;
|
|
123
|
-
commentParser(this.source).forEach(function (_a) {
|
|
135
|
+
commentParser.parse(this.source, { spacing: "preserve" }).forEach(function (_a) {
|
|
124
136
|
var tags = _a.tags;
|
|
125
137
|
tags.forEach(function (_a) {
|
|
126
138
|
var tag = _a.tag, tagType = _a.type, name = _a.name, description = _a.description;
|
|
@@ -159,11 +171,13 @@ var ComponentParser = /** @class */ (function () {
|
|
|
159
171
|
ComponentParser.prototype.cleanup = function () {
|
|
160
172
|
this.source = undefined;
|
|
161
173
|
this.compiled = undefined;
|
|
174
|
+
this.parsed = undefined;
|
|
162
175
|
this.rest_props = undefined;
|
|
163
176
|
this["extends"] = undefined;
|
|
164
177
|
this.componentComment = undefined;
|
|
165
178
|
this.reactive_vars.clear();
|
|
166
179
|
this.props.clear();
|
|
180
|
+
this.moduleExports.clear();
|
|
167
181
|
this.slots.clear();
|
|
168
182
|
this.events.clear();
|
|
169
183
|
this.typedefs.clear();
|
|
@@ -171,20 +185,94 @@ var ComponentParser = /** @class */ (function () {
|
|
|
171
185
|
};
|
|
172
186
|
ComponentParser.prototype.parseSvelteComponent = function (source, diagnostics) {
|
|
173
187
|
var _this = this;
|
|
174
|
-
var _a;
|
|
188
|
+
var _a, _b, _c;
|
|
175
189
|
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.verbose) {
|
|
176
|
-
|
|
190
|
+
console.log("[parsing] \"".concat(diagnostics.moduleName, "\" ").concat(diagnostics.filePath));
|
|
177
191
|
}
|
|
178
192
|
this.cleanup();
|
|
179
193
|
this.source = source;
|
|
180
194
|
this.compiled = (0, compiler_1.compile)(source);
|
|
195
|
+
this.parsed = (0, compiler_1.parse)(source);
|
|
181
196
|
this.collectReactiveVars();
|
|
182
197
|
this.parseCustomTypes();
|
|
198
|
+
if ((_b = this.parsed) === null || _b === void 0 ? void 0 : _b.module) {
|
|
199
|
+
(0, compiler_1.walk)((_c = this.parsed) === null || _c === void 0 ? void 0 : _c.module, {
|
|
200
|
+
enter: function (node) {
|
|
201
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
202
|
+
if (node.type === "ExportNamedDeclaration") {
|
|
203
|
+
var _j = ((_a = node.declaration) === null || _a === void 0 ? void 0 : _a.declarations) ? node.declaration.declarations[0] : node.declaration, declaration_type = _j.type, id = _j.id, init = _j.init, body = _j.body;
|
|
204
|
+
var prop_name = id.name;
|
|
205
|
+
var value = undefined;
|
|
206
|
+
var type = undefined;
|
|
207
|
+
var kind = node.declaration.kind;
|
|
208
|
+
var description = undefined;
|
|
209
|
+
var isFunction = false;
|
|
210
|
+
var isFunctionDeclaration = false;
|
|
211
|
+
if (init != null) {
|
|
212
|
+
if (init.type === "ObjectExpression" ||
|
|
213
|
+
init.type === "BinaryExpression" ||
|
|
214
|
+
init.type === "ArrayExpression" ||
|
|
215
|
+
init.type === "ArrowFunctionExpression") {
|
|
216
|
+
value = (_b = _this.sourceAtPos(init.start, init.end)) === null || _b === void 0 ? void 0 : _b.replace(/\n/g, " ");
|
|
217
|
+
type = value;
|
|
218
|
+
isFunction = init.type === "ArrowFunctionExpression";
|
|
219
|
+
if (init.type === "BinaryExpression") {
|
|
220
|
+
if ((init === null || init === void 0 ? void 0 : init.left.type) === "Literal" && typeof (init === null || init === void 0 ? void 0 : init.left.value) === "string") {
|
|
221
|
+
type = "string";
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (init.type === "UnaryExpression") {
|
|
227
|
+
value = _this.sourceAtPos(init.start, init.end);
|
|
228
|
+
type = typeof ((_c = init.argument) === null || _c === void 0 ? void 0 : _c.value);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
value = init.raw;
|
|
232
|
+
type = init.value == null ? undefined : typeof init.value;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (declaration_type === "FunctionDeclaration") {
|
|
237
|
+
value = "() => " + ((_d = _this.sourceAtPos(body.start, body.end)) === null || _d === void 0 ? void 0 : _d.replace(/\n/g, " "));
|
|
238
|
+
type = "() => any";
|
|
239
|
+
kind = "function";
|
|
240
|
+
isFunction = true;
|
|
241
|
+
isFunctionDeclaration = true;
|
|
242
|
+
}
|
|
243
|
+
if (node.leadingComments) {
|
|
244
|
+
var last_comment = node.leadingComments[node.leadingComments.length - 1];
|
|
245
|
+
var comment = commentParser.parse(ComponentParser.formatComment(last_comment.value), {
|
|
246
|
+
spacing: "preserve"
|
|
247
|
+
});
|
|
248
|
+
var tag = (_e = comment[0]) === null || _e === void 0 ? void 0 : _e.tags[((_f = comment[0]) === null || _f === void 0 ? void 0 : _f.tags.length) - 1];
|
|
249
|
+
if ((tag === null || tag === void 0 ? void 0 : tag.tag) === "type")
|
|
250
|
+
type = _this.aliasType(tag.type);
|
|
251
|
+
description = ComponentParser.assignValue((_h = (_g = comment[0]) === null || _g === void 0 ? void 0 : _g.description) === null || _h === void 0 ? void 0 : _h.trim());
|
|
252
|
+
}
|
|
253
|
+
if (!description && _this.typedefs.has(type)) {
|
|
254
|
+
description = _this.typedefs.get(type).description;
|
|
255
|
+
}
|
|
256
|
+
_this.addModuleExport(prop_name, {
|
|
257
|
+
name: prop_name,
|
|
258
|
+
kind: kind,
|
|
259
|
+
description: description,
|
|
260
|
+
type: type,
|
|
261
|
+
value: value,
|
|
262
|
+
isFunction: isFunction,
|
|
263
|
+
isFunctionDeclaration: isFunctionDeclaration,
|
|
264
|
+
constant: kind === "const",
|
|
265
|
+
reactive: false
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
183
271
|
var dispatcher_name = undefined;
|
|
184
272
|
var callees = [];
|
|
185
|
-
(0, compiler_1.walk)(this.
|
|
273
|
+
(0, compiler_1.walk)({ html: this.parsed.html, instance: this.parsed.instance }, {
|
|
186
274
|
enter: function (node, parent, prop) {
|
|
187
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
275
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
188
276
|
if (node.type === "CallExpression") {
|
|
189
277
|
if (node.callee.name === "createEventDispatcher") {
|
|
190
278
|
dispatcher_name = parent === null || parent === void 0 ? void 0 : parent.id.name;
|
|
@@ -222,7 +310,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
222
310
|
node.declaration = declaration_1;
|
|
223
311
|
prop_name = exportedName;
|
|
224
312
|
}
|
|
225
|
-
var
|
|
313
|
+
var _o = node.declaration.declarations ? node.declaration.declarations[0] : node.declaration, declaration_type = _o.type, id = _o.id, init = _o.init, body = _o.body;
|
|
226
314
|
prop_name !== null && prop_name !== void 0 ? prop_name : (prop_name = id.name);
|
|
227
315
|
var value = undefined;
|
|
228
316
|
var type = undefined;
|
|
@@ -264,11 +352,13 @@ var ComponentParser = /** @class */ (function () {
|
|
|
264
352
|
}
|
|
265
353
|
if (node.leadingComments) {
|
|
266
354
|
var last_comment = node.leadingComments[node.leadingComments.length - 1];
|
|
267
|
-
var comment = commentParser(ComponentParser.formatComment(last_comment.value)
|
|
355
|
+
var comment = commentParser.parse(ComponentParser.formatComment(last_comment.value), {
|
|
356
|
+
spacing: "preserve"
|
|
357
|
+
});
|
|
268
358
|
var tag = (_e = comment[0]) === null || _e === void 0 ? void 0 : _e.tags[((_f = comment[0]) === null || _f === void 0 ? void 0 : _f.tags.length) - 1];
|
|
269
359
|
if ((tag === null || tag === void 0 ? void 0 : tag.tag) === "type")
|
|
270
360
|
type = _this.aliasType(tag.type);
|
|
271
|
-
description = ComponentParser.assignValue((_g = comment[0]) === null || _g === void 0 ? void 0 : _g.description);
|
|
361
|
+
description = ComponentParser.assignValue((_h = (_g = comment[0]) === null || _g === void 0 ? void 0 : _g.description) === null || _h === void 0 ? void 0 : _h.trim());
|
|
272
362
|
}
|
|
273
363
|
if (!description && _this.typedefs.has(type)) {
|
|
274
364
|
description = _this.typedefs.get(type).description;
|
|
@@ -286,13 +376,13 @@ var ComponentParser = /** @class */ (function () {
|
|
|
286
376
|
});
|
|
287
377
|
}
|
|
288
378
|
if (node.type === "Comment") {
|
|
289
|
-
var data = (
|
|
379
|
+
var data = (_k = (_j = node === null || node === void 0 ? void 0 : node.data) === null || _j === void 0 ? void 0 : _j.trim()) !== null && _k !== void 0 ? _k : "";
|
|
290
380
|
if (/^@component/.test(data)) {
|
|
291
381
|
_this.componentComment = data.replace(/^@component/, "");
|
|
292
382
|
}
|
|
293
383
|
}
|
|
294
384
|
if (node.type === "Slot") {
|
|
295
|
-
var slot_name = (
|
|
385
|
+
var slot_name = (_l = node.attributes.find(function (attr) { return attr.name === "name"; })) === null || _l === void 0 ? void 0 : _l.value[0].data;
|
|
296
386
|
var slot_props = node.attributes
|
|
297
387
|
.filter(function (attr) { return attr.name !== "name"; })
|
|
298
388
|
.reduce(function (slot_props, _a) {
|
|
@@ -329,7 +419,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
329
419
|
}
|
|
330
420
|
return __assign(__assign({}, slot_props), (_b = {}, _b[name] = slot_prop_value, _b));
|
|
331
421
|
}, {});
|
|
332
|
-
var fallback = (
|
|
422
|
+
var fallback = (_m = node.children) === null || _m === void 0 ? void 0 : _m.map(function (_a) {
|
|
333
423
|
var start = _a.start, end = _a.end;
|
|
334
424
|
return _this.sourceAtPos(start, end);
|
|
335
425
|
}).join("").trim();
|
|
@@ -383,6 +473,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
383
473
|
}
|
|
384
474
|
return prop;
|
|
385
475
|
}),
|
|
476
|
+
moduleExports: ComponentParser.mapToArray(this.moduleExports),
|
|
386
477
|
slots: ComponentParser.mapToArray(this.slots)
|
|
387
478
|
.map(function (slot) {
|
|
388
479
|
try {
|
package/lib/create-exports.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ParsedExports } from "./parse-exports";
|
|
2
|
-
|
|
2
|
+
import { ComponentDocs } from "./rollup-plugin";
|
|
3
|
+
export declare function createExports(parsed_exports: ParsedExports, components: ComponentDocs): string;
|
|
3
4
|
export declare function removeSvelteExt(filePath: string): string;
|
|
4
5
|
export declare function convertSvelteExt(filePath: string): string;
|
package/lib/create-exports.js
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
exports.__esModule = true;
|
|
3
3
|
exports.convertSvelteExt = exports.removeSvelteExt = exports.createExports = void 0;
|
|
4
|
-
function createExports(parsed_exports) {
|
|
4
|
+
function createExports(parsed_exports, components) {
|
|
5
5
|
var source = Object.entries(parsed_exports).map(function (_a) {
|
|
6
6
|
var id = _a[0], exportee = _a[1];
|
|
7
|
+
var module_exports = [];
|
|
8
|
+
if (components.has(id)) {
|
|
9
|
+
module_exports = components.get(id).moduleExports.map(function (moduleExport) {
|
|
10
|
+
return moduleExport.name;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
var named_exports = "";
|
|
14
|
+
if (module_exports.length > 0)
|
|
15
|
+
named_exports = ", " + module_exports.join(", ");
|
|
7
16
|
if (id === "default" || exportee["default"]) {
|
|
8
17
|
if (exportee.mixed) {
|
|
9
|
-
return "export { default as ".concat(id, " } from \"").concat(exportee.source, "\";\nexport { default } from \"").concat(exportee.source, "\";");
|
|
18
|
+
return "export { default as ".concat(id).concat(named_exports, " } from \"").concat(exportee.source, "\";\nexport { default } from \"").concat(exportee.source, "\";");
|
|
10
19
|
}
|
|
11
|
-
return "export { default } from \"".concat(exportee.source, "\";");
|
|
20
|
+
return "export { default".concat(named_exports, " } from \"").concat(exportee.source, "\";");
|
|
12
21
|
}
|
|
13
|
-
return "export { default as ".concat(id, " } from \"").concat(exportee.source, "\";");
|
|
22
|
+
return "export { default as ".concat(id).concat(named_exports, " } from \"").concat(exportee.source, "\";");
|
|
14
23
|
});
|
|
15
24
|
return source.join("\n");
|
|
16
25
|
}
|
package/lib/get-svelte-entry.js
CHANGED
|
@@ -14,7 +14,7 @@ function getSvelteEntry(entryPoint) {
|
|
|
14
14
|
return entryPoint;
|
|
15
15
|
}
|
|
16
16
|
else {
|
|
17
|
-
|
|
17
|
+
console.log("Invalid entry point: ".concat(entry_path, "."));
|
|
18
18
|
return null;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -23,12 +23,12 @@ function getSvelteEntry(entryPoint) {
|
|
|
23
23
|
var pkg = JSON.parse(fs.readFileSync(pkg_path, "utf-8"));
|
|
24
24
|
if (pkg.svelte !== undefined)
|
|
25
25
|
return pkg.svelte;
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
console.log("Could not determine an entry point.\n");
|
|
27
|
+
console.log('Specify an entry point to your Svelte code in the "svelte" field of your package.json.\n');
|
|
28
28
|
return null;
|
|
29
29
|
}
|
|
30
30
|
else {
|
|
31
|
-
|
|
31
|
+
console.log("Could not locate a package.json file.\n");
|
|
32
32
|
return null;
|
|
33
33
|
}
|
|
34
34
|
}
|
package/lib/rollup-plugin.js
CHANGED
|
@@ -48,7 +48,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
48
48
|
};
|
|
49
49
|
exports.__esModule = true;
|
|
50
50
|
exports.writeOutput = exports.generateBundle = void 0;
|
|
51
|
-
var fs = require("fs
|
|
51
|
+
var fs = require("fs");
|
|
52
|
+
var fsp = require("fs/promises");
|
|
52
53
|
var path = require("path");
|
|
53
54
|
var fg = require("fast-glob");
|
|
54
55
|
var writer_ts_definitions_1 = require("./writer/writer-ts-definitions");
|
|
@@ -92,7 +93,7 @@ function pluginSveld(opts) {
|
|
|
92
93
|
exports["default"] = pluginSveld;
|
|
93
94
|
function generateBundle(input, glob) {
|
|
94
95
|
return __awaiter(this, void 0, void 0, function () {
|
|
95
|
-
var dir, entry, exports, components, parser, _i,
|
|
96
|
+
var dir, entry, exports, components, parser, exportEntries, _i, exportEntries_1, _a, exportName, entry_1, filePath, _b, ext, name_1, moduleName, source, processed;
|
|
96
97
|
return __generator(this, function (_c) {
|
|
97
98
|
switch (_c.label) {
|
|
98
99
|
case 0:
|
|
@@ -110,14 +111,20 @@ function generateBundle(input, glob) {
|
|
|
110
111
|
}
|
|
111
112
|
components = new Map();
|
|
112
113
|
parser = new ComponentParser_1["default"]();
|
|
113
|
-
|
|
114
|
+
exportEntries = Object.entries(exports);
|
|
115
|
+
_i = 0, exportEntries_1 = exportEntries;
|
|
114
116
|
_c.label = 1;
|
|
115
117
|
case 1:
|
|
116
|
-
if (!(_i <
|
|
117
|
-
|
|
118
|
-
filePath = entry_1.source
|
|
118
|
+
if (!(_i < exportEntries_1.length)) return [3 /*break*/, 5];
|
|
119
|
+
_a = exportEntries_1[_i], exportName = _a[0], entry_1 = _a[1];
|
|
120
|
+
filePath = entry_1.source;
|
|
121
|
+
_b = path.parse(filePath), ext = _b.ext, name_1 = _b.name;
|
|
122
|
+
moduleName = exportName;
|
|
123
|
+
if (exportEntries.length === 1 && exportName === "default") {
|
|
124
|
+
moduleName = name_1;
|
|
125
|
+
}
|
|
119
126
|
if (!(ext === ".svelte")) return [3 /*break*/, 4];
|
|
120
|
-
return [4 /*yield*/,
|
|
127
|
+
return [4 /*yield*/, fsp.readFile(path.resolve(dir, filePath), "utf-8")];
|
|
121
128
|
case 2:
|
|
122
129
|
source = _c.sent();
|
|
123
130
|
return [4 /*yield*/, (0, compiler_1.preprocess)(source, [(0, svelte_preprocess_1.typescript)(), (0, svelte_preprocess_1.replace)([[/<style.+<\/style>/gims, ""]])], {
|
|
@@ -151,7 +158,7 @@ function writeOutput(result, opts, input) {
|
|
|
151
158
|
(0, writer_json_1["default"])(result.components, __assign(__assign({ outFile: "COMPONENT_API.json" }, opts === null || opts === void 0 ? void 0 : opts.jsonOptions), { input: input, inputDir: inputDir }));
|
|
152
159
|
}
|
|
153
160
|
if (opts === null || opts === void 0 ? void 0 : opts.markdown) {
|
|
154
|
-
(0, writer_markdown_1["default"])(result.components, __assign({ outFile: "COMPONENT_INDEX.md" }, opts === null || opts === void 0 ? void 0 : opts.markdownOptions));
|
|
161
|
+
(0, writer_markdown_1["default"])(result.components, __assign({ outFile: path.join(process.cwd(), "COMPONENT_INDEX.md") }, opts === null || opts === void 0 ? void 0 : opts.markdownOptions));
|
|
155
162
|
}
|
|
156
163
|
}
|
|
157
164
|
exports.writeOutput = writeOutput;
|
package/lib/writer/Writer.js
CHANGED
|
@@ -36,7 +36,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
exports.__esModule = true;
|
|
39
|
-
var
|
|
39
|
+
var path = require("path");
|
|
40
|
+
var fsp = require("fs/promises");
|
|
40
41
|
var prettier = require("prettier");
|
|
41
42
|
var Writer = /** @class */ (function () {
|
|
42
43
|
function Writer(options) {
|
|
@@ -47,29 +48,21 @@ var Writer = /** @class */ (function () {
|
|
|
47
48
|
return prettier.format(raw, this.options);
|
|
48
49
|
}
|
|
49
50
|
catch (error) {
|
|
50
|
-
|
|
51
|
+
console.error(error);
|
|
51
52
|
return raw;
|
|
52
53
|
}
|
|
53
54
|
};
|
|
54
55
|
Writer.prototype.write = function (filePath, raw) {
|
|
55
56
|
return __awaiter(this, void 0, void 0, function () {
|
|
56
|
-
var error_1;
|
|
57
57
|
return __generator(this, function (_a) {
|
|
58
58
|
switch (_a.label) {
|
|
59
|
-
case 0:
|
|
60
|
-
_a.trys.push([0, 3, , 4]);
|
|
61
|
-
return [4 /*yield*/, fs.ensureFile(filePath)];
|
|
59
|
+
case 0: return [4 /*yield*/, fsp.mkdir(path.parse(filePath).dir, { recursive: true })];
|
|
62
60
|
case 1:
|
|
63
61
|
_a.sent();
|
|
64
|
-
return [4 /*yield*/,
|
|
62
|
+
return [4 /*yield*/, fsp.writeFile(filePath, this.format(raw))];
|
|
65
63
|
case 2:
|
|
66
64
|
_a.sent();
|
|
67
|
-
return [
|
|
68
|
-
case 3:
|
|
69
|
-
error_1 = _a.sent();
|
|
70
|
-
process.stderr.write(error_1 + "\n");
|
|
71
|
-
return [3 /*break*/, 4];
|
|
72
|
-
case 4: return [2 /*return*/];
|
|
65
|
+
return [2 /*return*/];
|
|
73
66
|
}
|
|
74
67
|
});
|
|
75
68
|
});
|
|
@@ -74,7 +74,7 @@ function writeJson(components, options) {
|
|
|
74
74
|
return [4 /*yield*/, writer.write(output_path, JSON.stringify(output))];
|
|
75
75
|
case 1:
|
|
76
76
|
_a.sent();
|
|
77
|
-
|
|
77
|
+
console.log("created \"".concat(options.outFile, "\".\n"));
|
|
78
78
|
return [2 /*return*/];
|
|
79
79
|
}
|
|
80
80
|
});
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ComponentDocs } from "../rollup-plugin";
|
|
2
2
|
import WriterMarkdown, { AppendType } from "./WriterMarkdown";
|
|
3
3
|
export interface WriteMarkdownOptions {
|
|
4
|
+
write?: boolean;
|
|
4
5
|
outFile: string;
|
|
5
6
|
onAppend?: (type: AppendType, document: WriterMarkdown, components: ComponentDocs) => void;
|
|
6
7
|
}
|
|
7
|
-
export default function writeMarkdown(components: ComponentDocs, options: WriteMarkdownOptions): Promise<
|
|
8
|
+
export default function writeMarkdown(components: ComponentDocs, options: WriteMarkdownOptions): Promise<string>;
|
|
@@ -45,7 +45,6 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
45
45
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
46
|
};
|
|
47
47
|
exports.__esModule = true;
|
|
48
|
-
var path = require("path");
|
|
49
48
|
var WriterMarkdown_1 = require("./WriterMarkdown");
|
|
50
49
|
var writer_ts_definitions_1 = require("./writer-ts-definitions");
|
|
51
50
|
var PROP_TABLE_HEADER = "| Prop name | Kind | Reactive | Type | Default value | Description |\n| :- | :- | :- | :- |\n";
|
|
@@ -87,11 +86,11 @@ function formatEventDetail(detail) {
|
|
|
87
86
|
}
|
|
88
87
|
function writeMarkdown(components, options) {
|
|
89
88
|
return __awaiter(this, void 0, void 0, function () {
|
|
90
|
-
var
|
|
89
|
+
var write, document, keys;
|
|
91
90
|
return __generator(this, function (_a) {
|
|
92
91
|
switch (_a.label) {
|
|
93
92
|
case 0:
|
|
94
|
-
|
|
93
|
+
write = (options === null || options === void 0 ? void 0 : options.write) !== false;
|
|
95
94
|
document = new WriterMarkdown_1["default"]({
|
|
96
95
|
onAppend: function (type, document) {
|
|
97
96
|
var _a;
|
|
@@ -148,11 +147,13 @@ function writeMarkdown(components, options) {
|
|
|
148
147
|
document.append("p", "None.");
|
|
149
148
|
}
|
|
150
149
|
});
|
|
151
|
-
return [
|
|
150
|
+
if (!write) return [3 /*break*/, 2];
|
|
151
|
+
return [4 /*yield*/, document.write(options.outFile, document.end())];
|
|
152
152
|
case 1:
|
|
153
153
|
_a.sent();
|
|
154
|
-
|
|
155
|
-
|
|
154
|
+
console.log("created \"".concat(options.outFile, "\"."));
|
|
155
|
+
_a.label = 2;
|
|
156
|
+
case 2: return [2 /*return*/, document.end()];
|
|
156
157
|
}
|
|
157
158
|
});
|
|
158
159
|
});
|
|
@@ -84,8 +84,8 @@ function addCommentLine(value, returnValue) {
|
|
|
84
84
|
return "* ".concat(returnValue || value, "\n");
|
|
85
85
|
}
|
|
86
86
|
function genPropDef(def) {
|
|
87
|
-
var _a;
|
|
88
|
-
var
|
|
87
|
+
var _a, _b, _c;
|
|
88
|
+
var initial_props = def.props
|
|
89
89
|
.filter(function (prop) { return !prop.isFunctionDeclaration && prop.kind !== "const"; })
|
|
90
90
|
.map(function (prop) {
|
|
91
91
|
var _a;
|
|
@@ -105,11 +105,22 @@ function genPropDef(def) {
|
|
|
105
105
|
.join("");
|
|
106
106
|
var prop_value = prop.constant && !prop.isFunction ? prop.value : prop.type;
|
|
107
107
|
return "\n ".concat(prop_comments.length > 0 ? "/**\n".concat(prop_comments, "*/") : EMPTY_STR, "\n ").concat(prop.name, "?: ").concat(prop_value, ";");
|
|
108
|
-
})
|
|
109
|
-
|
|
108
|
+
});
|
|
109
|
+
if (((_a = def.rest_props) === null || _a === void 0 ? void 0 : _a.type) === "Element") {
|
|
110
|
+
var elements = (_b = def.rest_props) === null || _b === void 0 ? void 0 : _b.name.split("|").map(function (element) { return element.replace(/\s+/g, ""); });
|
|
111
|
+
if (elements.includes("a")) {
|
|
112
|
+
initial_props.push([
|
|
113
|
+
"\n",
|
|
114
|
+
"\n /**\n * SvelteKit attribute to enable data prefetching\n * if a link is hovered over or touched on mobile.\n * @see https://kit.svelte.dev/docs/a-options#sveltekit-prefetch\n * @default false\n */\n \"sveltekit:prefetch\"?: boolean;\n ",
|
|
115
|
+
"\n",
|
|
116
|
+
"\n /**\n * SvelteKit attribute to prevent scrolling\n * after the link is clicked.\n * @see https://kit.svelte.dev/docs/a-options#sveltekit-prefetch\n * @default false\n */\n \"sveltekit:noscroll\"?: boolean;\n ",
|
|
117
|
+
].join("\n"));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
var props = initial_props.join("\n");
|
|
110
121
|
var props_name = "".concat(def.moduleName, "Props");
|
|
111
122
|
var prop_def = EMPTY_STR;
|
|
112
|
-
if (((
|
|
123
|
+
if (((_c = def.rest_props) === null || _c === void 0 ? void 0 : _c.type) === "Element") {
|
|
113
124
|
var extend_tag_map = def.rest_props.name
|
|
114
125
|
.split("|")
|
|
115
126
|
.map(function (name) { return "svelte.JSX.HTMLAttributes<HTMLElementTagNameMap[\"".concat(name.trim(), "\"]>"); })
|
|
@@ -159,7 +170,7 @@ function genAccessors(def) {
|
|
|
159
170
|
function genImports(def) {
|
|
160
171
|
if (def["extends"] === undefined)
|
|
161
172
|
return "";
|
|
162
|
-
return "import { ".concat(def["extends"].interface, " } from ").concat(def["extends"]["import"], ";");
|
|
173
|
+
return "import type { ".concat(def["extends"].interface, " } from ").concat(def["extends"]["import"], ";");
|
|
163
174
|
}
|
|
164
175
|
function genComponentComment(def) {
|
|
165
176
|
if (!def.componentComment)
|
|
@@ -171,15 +182,24 @@ function genComponentComment(def) {
|
|
|
171
182
|
.map(function (line) { return "* ".concat(line); })
|
|
172
183
|
.join("\n"), "\n*/");
|
|
173
184
|
}
|
|
185
|
+
function genModuleExports(def) {
|
|
186
|
+
return def.moduleExports
|
|
187
|
+
.map(function (prop) {
|
|
188
|
+
var _a;
|
|
189
|
+
var prop_comments = [addCommentLine((_a = prop.description) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, "\n* "))].filter(Boolean).join("");
|
|
190
|
+
return "\n ".concat(prop_comments.length > 0 ? "/**\n".concat(prop_comments, "*/") : EMPTY_STR, "\n export type ").concat(prop.name, " = ").concat(prop.type || ANY_TYPE, ";");
|
|
191
|
+
})
|
|
192
|
+
.join("\n");
|
|
193
|
+
}
|
|
174
194
|
function writeTsDefinition(component) {
|
|
175
|
-
var moduleName = component.moduleName, typedefs = component.typedefs, props = component.props, slots = component.slots, events = component.events, rest_props = component.rest_props, _extends = component["extends"], componentComment = component.componentComment;
|
|
195
|
+
var moduleName = component.moduleName, typedefs = component.typedefs, props = component.props, moduleExports = component.moduleExports, slots = component.slots, events = component.events, rest_props = component.rest_props, _extends = component["extends"], componentComment = component.componentComment;
|
|
176
196
|
var _a = genPropDef({
|
|
177
197
|
moduleName: moduleName,
|
|
178
198
|
props: props,
|
|
179
199
|
rest_props: rest_props,
|
|
180
200
|
"extends": _extends
|
|
181
201
|
}), props_name = _a.props_name, prop_def = _a.prop_def;
|
|
182
|
-
return "\n /// <reference types=\"svelte\" />\n import { SvelteComponentTyped } from \"svelte\";\n ".concat(genImports({ "extends": _extends }), "\n ").concat(getTypeDefs({ typedefs: typedefs }), "\n ").concat(prop_def, "\n ").concat(genComponentComment({ componentComment: componentComment }), "\n export default class ").concat(moduleName === "default" ? "" : moduleName, " extends SvelteComponentTyped<\n ").concat(props_name, ",\n {").concat(genEventDef({ events: events }), "},\n {").concat(genSlotDef({ slots: slots }), "}\n > {\n ").concat(genAccessors({ props: props }), "\n }");
|
|
202
|
+
return "\n /// <reference types=\"svelte\" />\n import type { SvelteComponentTyped } from \"svelte\";\n ".concat(genImports({ "extends": _extends }), "\n ").concat(genModuleExports({ moduleExports: moduleExports }), "\n ").concat(getTypeDefs({ typedefs: typedefs }), "\n ").concat(prop_def, "\n ").concat(genComponentComment({ componentComment: componentComment }), "\n export default class ").concat(moduleName === "default" ? "" : moduleName, " extends SvelteComponentTyped<\n ").concat(props_name, ",\n {").concat(genEventDef({ events: events }), "},\n {").concat(genSlotDef({ slots: slots }), "}\n > {\n ").concat(genAccessors({ props: props }), "\n }");
|
|
183
203
|
}
|
|
184
204
|
exports.writeTsDefinition = writeTsDefinition;
|
|
185
205
|
function writeTsDefinitions(components, options) {
|
|
@@ -192,7 +212,7 @@ function writeTsDefinitions(components, options) {
|
|
|
192
212
|
case 0:
|
|
193
213
|
ts_base_path = path.join(process.cwd(), options.outDir, "index.d.ts");
|
|
194
214
|
writer = new Writer_1["default"]({ parser: "typescript", printWidth: 80 });
|
|
195
|
-
indexDTs = options.preamble + (0, create_exports_1.createExports)(options.exports);
|
|
215
|
+
indexDTs = options.preamble + (0, create_exports_1.createExports)(options.exports, components);
|
|
196
216
|
_c.label = 1;
|
|
197
217
|
case 1:
|
|
198
218
|
_c.trys.push([1, 7, 8, 13]);
|
|
@@ -228,7 +248,7 @@ function writeTsDefinitions(components, options) {
|
|
|
228
248
|
case 13: return [4 /*yield*/, writer.write(ts_base_path, indexDTs)];
|
|
229
249
|
case 14:
|
|
230
250
|
_c.sent();
|
|
231
|
-
|
|
251
|
+
console.log("created TypeScript definitions.");
|
|
232
252
|
return [2 /*return*/];
|
|
233
253
|
}
|
|
234
254
|
});
|
package/package.json
CHANGED
|
@@ -1,44 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sveld",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Generate TypeScript definitions for your Svelte components.",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"types": "./lib/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
-
"build
|
|
11
|
-
"
|
|
12
|
-
"test:unit": "
|
|
13
|
-
"test:
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
"peerDependencies": {
|
|
18
|
-
"svelte": "^3.31.0"
|
|
9
|
+
"dev": "vite",
|
|
10
|
+
"build": "vite build",
|
|
11
|
+
"preview": "vite preview",
|
|
12
|
+
"test:unit": "vitest",
|
|
13
|
+
"test:snapshot": "node tests/test-snapshot",
|
|
14
|
+
"test:integration": "node tests/test-integration",
|
|
15
|
+
"format": "prettier --write \"{src,tests}/**/*.{js,ts,svelte,md}\"",
|
|
16
|
+
"prepack": "tsc"
|
|
19
17
|
},
|
|
20
18
|
"dependencies": {
|
|
21
19
|
"@rollup/plugin-node-resolve": "^11.0.1",
|
|
22
|
-
"acorn": "^8.
|
|
23
|
-
"comment-parser": "^
|
|
24
|
-
"fast-glob": "^3.2.
|
|
25
|
-
"fs-extra": "^9.0.1",
|
|
20
|
+
"acorn": "^8.7.0",
|
|
21
|
+
"comment-parser": "^1.3.0",
|
|
22
|
+
"fast-glob": "^3.2.11",
|
|
26
23
|
"prettier": "^2.5.1",
|
|
27
|
-
"rollup": "^2.
|
|
24
|
+
"rollup": "^2.68.0",
|
|
28
25
|
"rollup-plugin-svelte": "^7.1.0",
|
|
29
|
-
"svelte": "^3.46.
|
|
30
|
-
"svelte-preprocess": "^4.10.
|
|
26
|
+
"svelte": "^3.46.4",
|
|
27
|
+
"svelte-preprocess": "^4.10.4",
|
|
31
28
|
"typescript": "^4.5.5"
|
|
32
29
|
},
|
|
33
30
|
"devDependencies": {
|
|
34
|
-
"@
|
|
35
|
-
"@types/node": "^
|
|
36
|
-
"@types/prettier": "^2.
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
31
|
+
"@sveltejs/vite-plugin-svelte": "next",
|
|
32
|
+
"@types/node": "^17.0.21",
|
|
33
|
+
"@types/prettier": "^2.4.4",
|
|
34
|
+
"carbon-components-svelte": "^0.61.1",
|
|
35
|
+
"carbon-preprocess-svelte": "^0.6.0",
|
|
36
|
+
"codemirror": "^5.65.2",
|
|
37
|
+
"prettier-plugin-svelte": "^2.6.0",
|
|
38
|
+
"svelte-highlight": "^5.3.0",
|
|
39
|
+
"vite": "^2.8.6",
|
|
40
|
+
"vitest": "^0.6.0"
|
|
42
41
|
},
|
|
43
42
|
"bin": {
|
|
44
43
|
"sveld": "./cli.js"
|