sveld 0.11.0 → 0.13.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 +17 -0
- package/README.md +29 -2
- package/lib/ComponentParser.d.ts +1 -0
- package/lib/ComponentParser.js +35 -15
- package/lib/create-exports.js +3 -3
- package/lib/get-svelte-entry.d.ts +2 -2
- package/lib/get-svelte-entry.js +14 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/lib/path.d.ts +6 -0
- package/lib/path.js +13 -0
- package/lib/rollup-plugin.js +3 -5
- package/lib/sveld.d.ts +11 -0
- package/lib/sveld.js +60 -0
- package/lib/writer/WriterMarkdown.js +4 -4
- package/lib/writer/writer-json.js +3 -2
- package/lib/writer/writer-markdown.js +10 -10
- package/lib/writer/writer-ts-definitions.js +25 -18
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,23 @@ 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.13.0](https://github.com/carbon-design-system/sveld/releases/tag/v0.13.0) - 2022-01-22
|
|
14
|
+
|
|
15
|
+
- export `sveld` for programmatic usage
|
|
16
|
+
- upgrade prettier, rollup, svelte, svelte-preprocess, typescript
|
|
17
|
+
|
|
18
|
+
## [0.12.1](https://github.com/carbon-design-system/sveld/releases/tag/v0.12.1) - 2022-01-20
|
|
19
|
+
|
|
20
|
+
- specify `@default undefined` for undefined prop values (i.e., `let prop1; let prop2 = undefined`)
|
|
21
|
+
|
|
22
|
+
## [0.12.0](https://github.com/carbon-design-system/sveld/releases/tag/v0.12.0) - 2022-01-02
|
|
23
|
+
|
|
24
|
+
- support props defined via renamed exports (i.e., `let className; export { className as class }`)
|
|
25
|
+
|
|
26
|
+
## [0.11.1](https://github.com/carbon-design-system/sveld/releases/tag/v0.11.1) - 2021-12-31
|
|
27
|
+
|
|
28
|
+
- replace backslashes with forward slashes in COMPONENT_API.json `filePath` values
|
|
29
|
+
|
|
13
30
|
## [0.11.0](https://github.com/carbon-design-system/sveld/releases/tag/v0.11.0) - 2021-12-16
|
|
14
31
|
|
|
15
32
|
- support writing `<!-- @component -->` comments in Svelte components to TypeScript definitions
|
package/README.md
CHANGED
|
@@ -149,7 +149,7 @@ yarn add -D sveld
|
|
|
149
149
|
npm i -D sveld
|
|
150
150
|
```
|
|
151
151
|
|
|
152
|
-
###
|
|
152
|
+
### Rollup
|
|
153
153
|
|
|
154
154
|
Import and add `sveld` as a plugin to your `rollup.config.js`.
|
|
155
155
|
|
|
@@ -194,9 +194,36 @@ Append `--json` or `--markdown` flags to generate documentation in JSON/Markdown
|
|
|
194
194
|
npx sveld --json --markdown
|
|
195
195
|
```
|
|
196
196
|
|
|
197
|
+
### Node.js
|
|
198
|
+
|
|
199
|
+
You can also use `sveld` programmatically in Node.js.
|
|
200
|
+
|
|
201
|
+
If no `input` is specified, `sveld` will infer the entry point based on the `package.json#svelte` field.
|
|
202
|
+
|
|
203
|
+
```js
|
|
204
|
+
const { sveld } = require("sveld");
|
|
205
|
+
const pkg = require("./package.json");
|
|
206
|
+
|
|
207
|
+
sveld({
|
|
208
|
+
input: "./src/index.js",
|
|
209
|
+
glob: true,
|
|
210
|
+
markdown: true,
|
|
211
|
+
markdownOptions: {
|
|
212
|
+
onAppend: (type, document, components) => {
|
|
213
|
+
if (type === "h1")
|
|
214
|
+
document.append("quote", `${components.size} components exported from ${pkg.name}@${pkg.version}.`);
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
json: true,
|
|
218
|
+
jsonOptions: {
|
|
219
|
+
outFile: "docs/src/COMPONENT_API.json",
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
197
224
|
### Publishing to NPM
|
|
198
225
|
|
|
199
|
-
|
|
226
|
+
TypeScript definitions are outputted to the `types` folder by default. Don't forget to include the folder in your `package.json` when publishing the package to NPM.
|
|
200
227
|
|
|
201
228
|
```diff
|
|
202
229
|
{
|
package/lib/ComponentParser.d.ts
CHANGED
package/lib/ComponentParser.js
CHANGED
|
@@ -27,6 +27,7 @@ var DEFAULT_SLOT_NAME = "__default__";
|
|
|
27
27
|
var ComponentParser = /** @class */ (function () {
|
|
28
28
|
function ComponentParser(options) {
|
|
29
29
|
this.reactive_vars = new Set();
|
|
30
|
+
this.vars = new Set();
|
|
30
31
|
this.props = new Map();
|
|
31
32
|
this.slots = new Map();
|
|
32
33
|
this.events = new Map();
|
|
@@ -148,7 +149,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
148
149
|
type: type,
|
|
149
150
|
name: name,
|
|
150
151
|
description: ComponentParser.assignValue(description),
|
|
151
|
-
ts: /(\}|\};)$/.test(type) ? "interface "
|
|
152
|
+
ts: /(\}|\};)$/.test(type) ? "interface ".concat(name, " ").concat(type) : "type ".concat(name, " = ").concat(type)
|
|
152
153
|
});
|
|
153
154
|
break;
|
|
154
155
|
}
|
|
@@ -172,7 +173,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
172
173
|
var _this = this;
|
|
173
174
|
var _a;
|
|
174
175
|
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.verbose) {
|
|
175
|
-
process.stdout.write("[parsing] \""
|
|
176
|
+
process.stdout.write("[parsing] \"".concat(diagnostics.moduleName, "\" ").concat(diagnostics.filePath, "\n"));
|
|
176
177
|
}
|
|
177
178
|
this.cleanup();
|
|
178
179
|
this.source = source;
|
|
@@ -183,7 +184,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
183
184
|
var callees = [];
|
|
184
185
|
(0, compiler_1.walk)(this.compiled.ast, {
|
|
185
186
|
enter: function (node, parent, prop) {
|
|
186
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
187
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
187
188
|
if (node.type === "CallExpression") {
|
|
188
189
|
if (node.callee.name === "createEventDispatcher") {
|
|
189
190
|
dispatcher_name = parent === null || parent === void 0 ? void 0 : parent.id.name;
|
|
@@ -201,9 +202,28 @@ var ComponentParser = /** @class */ (function () {
|
|
|
201
202
|
};
|
|
202
203
|
}
|
|
203
204
|
}
|
|
204
|
-
if (node.type === "
|
|
205
|
-
|
|
206
|
-
|
|
205
|
+
if (node.type === "VariableDeclaration") {
|
|
206
|
+
_this.vars.add(node);
|
|
207
|
+
}
|
|
208
|
+
if (node.type === "ExportNamedDeclaration") {
|
|
209
|
+
// Handle renamed exports
|
|
210
|
+
var prop_name = void 0;
|
|
211
|
+
if (node.declaration == null && ((_a = node.specifiers[0]) === null || _a === void 0 ? void 0 : _a.type) === "ExportSpecifier") {
|
|
212
|
+
var specifier = node.specifiers[0];
|
|
213
|
+
var localName_1 = specifier.local.name, exportedName = specifier.exported.name;
|
|
214
|
+
var declaration_1;
|
|
215
|
+
// Search through all variable declarations for this variable
|
|
216
|
+
// Limitation: the variable must have been declared before the export
|
|
217
|
+
_this.vars.forEach(function (varDecl) {
|
|
218
|
+
if (varDecl.declarations.some(function (decl) { return decl.id.type === "Identifier" && decl.id.name === localName_1; })) {
|
|
219
|
+
declaration_1 = varDecl;
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
node.declaration = declaration_1;
|
|
223
|
+
prop_name = exportedName;
|
|
224
|
+
}
|
|
225
|
+
var _m = node.declaration.declarations ? node.declaration.declarations[0] : node.declaration, declaration_type = _m.type, id = _m.id, init = _m.init, body = _m.body;
|
|
226
|
+
prop_name !== null && prop_name !== void 0 ? prop_name : (prop_name = id.name);
|
|
207
227
|
var value = undefined;
|
|
208
228
|
var type = undefined;
|
|
209
229
|
var kind = node.declaration.kind;
|
|
@@ -215,7 +235,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
215
235
|
init.type === "BinaryExpression" ||
|
|
216
236
|
init.type === "ArrayExpression" ||
|
|
217
237
|
init.type === "ArrowFunctionExpression") {
|
|
218
|
-
value = (
|
|
238
|
+
value = (_b = _this.sourceAtPos(init.start, init.end)) === null || _b === void 0 ? void 0 : _b.replace(/\n/g, " ");
|
|
219
239
|
type = value;
|
|
220
240
|
isFunction = init.type === "ArrowFunctionExpression";
|
|
221
241
|
if (init.type === "BinaryExpression") {
|
|
@@ -227,7 +247,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
227
247
|
else {
|
|
228
248
|
if (init.type === "UnaryExpression") {
|
|
229
249
|
value = _this.sourceAtPos(init.start, init.end);
|
|
230
|
-
type = typeof ((
|
|
250
|
+
type = typeof ((_c = init.argument) === null || _c === void 0 ? void 0 : _c.value);
|
|
231
251
|
}
|
|
232
252
|
else {
|
|
233
253
|
value = init.raw;
|
|
@@ -236,7 +256,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
236
256
|
}
|
|
237
257
|
}
|
|
238
258
|
if (declaration_type === "FunctionDeclaration") {
|
|
239
|
-
value = "() => " + ((
|
|
259
|
+
value = "() => " + ((_d = _this.sourceAtPos(body.start, body.end)) === null || _d === void 0 ? void 0 : _d.replace(/\n/g, " "));
|
|
240
260
|
type = "() => any";
|
|
241
261
|
kind = "function";
|
|
242
262
|
isFunction = true;
|
|
@@ -245,10 +265,10 @@ var ComponentParser = /** @class */ (function () {
|
|
|
245
265
|
if (node.leadingComments) {
|
|
246
266
|
var last_comment = node.leadingComments[node.leadingComments.length - 1];
|
|
247
267
|
var comment = commentParser(ComponentParser.formatComment(last_comment.value));
|
|
248
|
-
var tag = (
|
|
268
|
+
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
269
|
if ((tag === null || tag === void 0 ? void 0 : tag.tag) === "type")
|
|
250
270
|
type = _this.aliasType(tag.type);
|
|
251
|
-
description = ComponentParser.assignValue((
|
|
271
|
+
description = ComponentParser.assignValue((_g = comment[0]) === null || _g === void 0 ? void 0 : _g.description);
|
|
252
272
|
}
|
|
253
273
|
if (!description && _this.typedefs.has(type)) {
|
|
254
274
|
description = _this.typedefs.get(type).description;
|
|
@@ -266,13 +286,13 @@ var ComponentParser = /** @class */ (function () {
|
|
|
266
286
|
});
|
|
267
287
|
}
|
|
268
288
|
if (node.type === "Comment") {
|
|
269
|
-
var data = (
|
|
289
|
+
var data = (_j = (_h = node === null || node === void 0 ? void 0 : node.data) === null || _h === void 0 ? void 0 : _h.trim()) !== null && _j !== void 0 ? _j : "";
|
|
270
290
|
if (/^@component/.test(data)) {
|
|
271
291
|
_this.componentComment = data.replace(/^@component/, "");
|
|
272
292
|
}
|
|
273
293
|
}
|
|
274
294
|
if (node.type === "Slot") {
|
|
275
|
-
var slot_name = (
|
|
295
|
+
var slot_name = (_k = node.attributes.find(function (attr) { return attr.name === "name"; })) === null || _k === void 0 ? void 0 : _k.value[0].data;
|
|
276
296
|
var slot_props = node.attributes
|
|
277
297
|
.filter(function (attr) { return attr.name !== "name"; })
|
|
278
298
|
.reduce(function (slot_props, _a) {
|
|
@@ -309,7 +329,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
309
329
|
}
|
|
310
330
|
return __assign(__assign({}, slot_props), (_b = {}, _b[name] = slot_prop_value, _b));
|
|
311
331
|
}, {});
|
|
312
|
-
var fallback = (
|
|
332
|
+
var fallback = (_l = node.children) === null || _l === void 0 ? void 0 : _l.map(function (_a) {
|
|
313
333
|
var start = _a.start, end = _a.end;
|
|
314
334
|
return _this.sourceAtPos(start, end);
|
|
315
335
|
}).join("").trim();
|
|
@@ -375,7 +395,7 @@ var ComponentParser = /** @class */ (function () {
|
|
|
375
395
|
}
|
|
376
396
|
if (slot_props_1[key].value === undefined)
|
|
377
397
|
slot_props_1[key].value = "any";
|
|
378
|
-
new_props_1.push(key
|
|
398
|
+
new_props_1.push("".concat(key, ": ").concat(slot_props_1[key].value));
|
|
379
399
|
});
|
|
380
400
|
var formatted_slot_props = new_props_1.length === 0 ? "{}" : "{ " + new_props_1.join(", ") + " }";
|
|
381
401
|
return __assign(__assign({}, slot), { slot_props: formatted_slot_props });
|
package/lib/create-exports.js
CHANGED
|
@@ -6,11 +6,11 @@ function createExports(parsed_exports) {
|
|
|
6
6
|
var id = _a[0], exportee = _a[1];
|
|
7
7
|
if (id === "default" || exportee["default"]) {
|
|
8
8
|
if (exportee.mixed) {
|
|
9
|
-
return "export { default as "
|
|
9
|
+
return "export { default as ".concat(id, " } from \"").concat(exportee.source, "\";\nexport { default } from \"").concat(exportee.source, "\";");
|
|
10
10
|
}
|
|
11
|
-
return "export { default } from \""
|
|
11
|
+
return "export { default } from \"".concat(exportee.source, "\";");
|
|
12
12
|
}
|
|
13
|
-
return "export { default as "
|
|
13
|
+
return "export { default as ".concat(id, " } from \"").concat(exportee.source, "\";");
|
|
14
14
|
});
|
|
15
15
|
return source.join("\n");
|
|
16
16
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare type SvelteEntryPoint = string;
|
|
2
2
|
/**
|
|
3
|
-
* Get the file path
|
|
3
|
+
* Get the file path entry point for uncompiled Svelte source code
|
|
4
4
|
* Expects a "svelte" field in the consumer's `package.json`
|
|
5
5
|
*/
|
|
6
|
-
export declare function getSvelteEntry(): SvelteEntryPoint | null;
|
|
6
|
+
export declare function getSvelteEntry(entryPoint?: SvelteEntryPoint): SvelteEntryPoint | null;
|
package/lib/get-svelte-entry.js
CHANGED
|
@@ -4,17 +4,27 @@ exports.getSvelteEntry = void 0;
|
|
|
4
4
|
var fs = require("fs");
|
|
5
5
|
var path = require("path");
|
|
6
6
|
/**
|
|
7
|
-
* Get the file path
|
|
7
|
+
* Get the file path entry point for uncompiled Svelte source code
|
|
8
8
|
* Expects a "svelte" field in the consumer's `package.json`
|
|
9
9
|
*/
|
|
10
|
-
function getSvelteEntry() {
|
|
10
|
+
function getSvelteEntry(entryPoint) {
|
|
11
|
+
if (entryPoint) {
|
|
12
|
+
var entry_path = path.join(process.cwd(), entryPoint);
|
|
13
|
+
if (fs.existsSync(entry_path)) {
|
|
14
|
+
return entry_path;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
process.stdout.write("Invalid entry point: ".concat(entry_path, ".\n"));
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
11
21
|
var pkg_path = path.join(process.cwd(), "package.json");
|
|
12
22
|
if (fs.existsSync(pkg_path)) {
|
|
13
23
|
var pkg = JSON.parse(fs.readFileSync(pkg_path, "utf-8"));
|
|
14
24
|
if (pkg.svelte !== undefined)
|
|
15
25
|
return pkg.svelte;
|
|
16
|
-
process.stdout.write("Could not determine an
|
|
17
|
-
process.stdout.write('Specify an
|
|
26
|
+
process.stdout.write("Could not determine an entry point.\n");
|
|
27
|
+
process.stdout.write('Specify an entry point to your Svelte code in the "svelte" field of your package.json.\n');
|
|
18
28
|
return null;
|
|
19
29
|
}
|
|
20
30
|
else {
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -7,10 +7,12 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
9
|
exports.__esModule = true;
|
|
10
|
-
exports.cli = exports.ComponentParser = exports["default"] = void 0;
|
|
10
|
+
exports.sveld = exports.cli = exports.ComponentParser = exports["default"] = void 0;
|
|
11
11
|
var rollup_plugin_1 = require("./rollup-plugin");
|
|
12
12
|
__createBinding(exports, rollup_plugin_1, "default");
|
|
13
13
|
var ComponentParser_1 = require("./ComponentParser");
|
|
14
14
|
__createBinding(exports, ComponentParser_1, "default", "ComponentParser");
|
|
15
15
|
var cli_1 = require("./cli");
|
|
16
16
|
__createBinding(exports, cli_1, "cli");
|
|
17
|
+
var sveld_1 = require("./sveld");
|
|
18
|
+
__createBinding(exports, sveld_1, "sveld");
|
package/lib/path.d.ts
ADDED
package/lib/path.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.normalizeSeparators = void 0;
|
|
4
|
+
var path_1 = require("path");
|
|
5
|
+
/**
|
|
6
|
+
* Normalize directory separators to always use `/`.
|
|
7
|
+
* @param filePath A file path.
|
|
8
|
+
* @returns Path with normalized separators.
|
|
9
|
+
*/
|
|
10
|
+
function normalizeSeparators(filePath) {
|
|
11
|
+
return path_1.sep === "/" ? filePath : filePath.split(path_1.sep).join("/");
|
|
12
|
+
}
|
|
13
|
+
exports.normalizeSeparators = normalizeSeparators;
|
package/lib/rollup-plugin.js
CHANGED
|
@@ -59,6 +59,7 @@ var get_svelte_entry_1 = require("./get-svelte-entry");
|
|
|
59
59
|
var parse_exports_1 = require("./parse-exports");
|
|
60
60
|
var compiler_1 = require("svelte/compiler");
|
|
61
61
|
var svelte_preprocess_1 = require("svelte-preprocess");
|
|
62
|
+
var path_1 = require("./path");
|
|
62
63
|
function pluginSveld(opts) {
|
|
63
64
|
var result;
|
|
64
65
|
var input;
|
|
@@ -99,12 +100,9 @@ function generateBundle(input, glob) {
|
|
|
99
100
|
entry = fs.readFileSync(input, "utf-8");
|
|
100
101
|
exports = (0, parse_exports_1.parseExports)(entry);
|
|
101
102
|
if (glob) {
|
|
102
|
-
fg.sync([dir
|
|
103
|
+
fg.sync(["".concat(dir, "/**/*.svelte")]).forEach(function (file) {
|
|
103
104
|
var moduleName = path.parse(file).name.replace(/\-/g, "");
|
|
104
|
-
var source = "./" + path.relative(dir, file);
|
|
105
|
-
if (path.sep !== "/") {
|
|
106
|
-
source = source.split(path.sep).join("/");
|
|
107
|
-
}
|
|
105
|
+
var source = (0, path_1.normalizeSeparators)("./" + path.relative(dir, file));
|
|
108
106
|
if (exports[moduleName]) {
|
|
109
107
|
exports[moduleName].source = source;
|
|
110
108
|
}
|
package/lib/sveld.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PluginSveldOptions } from "./rollup-plugin";
|
|
2
|
+
interface SveldOptions extends PluginSveldOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Specify the input to the uncompiled Svelte source.
|
|
5
|
+
* If no value is provided, `sveld` will attempt to infer
|
|
6
|
+
* the entry point from the `package.json#svelte` field.
|
|
7
|
+
*/
|
|
8
|
+
input?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function sveld(opts?: SveldOptions): Promise<void>;
|
|
11
|
+
export {};
|
package/lib/sveld.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
39
|
+
exports.sveld = void 0;
|
|
40
|
+
var get_svelte_entry_1 = require("./get-svelte-entry");
|
|
41
|
+
var rollup_plugin_1 = require("./rollup-plugin");
|
|
42
|
+
function sveld(opts) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
44
|
+
var input, result;
|
|
45
|
+
return __generator(this, function (_a) {
|
|
46
|
+
switch (_a.label) {
|
|
47
|
+
case 0:
|
|
48
|
+
input = (0, get_svelte_entry_1.getSvelteEntry)(opts === null || opts === void 0 ? void 0 : opts.input);
|
|
49
|
+
if (input === null)
|
|
50
|
+
return [2 /*return*/];
|
|
51
|
+
return [4 /*yield*/, (0, rollup_plugin_1.generateBundle)(input, (opts === null || opts === void 0 ? void 0 : opts.glob) === true)];
|
|
52
|
+
case 1:
|
|
53
|
+
result = _a.sent();
|
|
54
|
+
(0, rollup_plugin_1.writeOutput)(result, opts || {}, input);
|
|
55
|
+
return [2 /*return*/];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
exports.sveld = sveld;
|
|
@@ -40,9 +40,9 @@ var WriterMarkdown = /** @class */ (function (_super) {
|
|
|
40
40
|
case "h5":
|
|
41
41
|
case "h6":
|
|
42
42
|
var length_1 = Number(type.slice(-1));
|
|
43
|
-
this.source += Array.from({ length: length_1 })
|
|
43
|
+
this.source += "".concat(Array.from({ length: length_1 })
|
|
44
44
|
.map(function (_) { return "#"; })
|
|
45
|
-
.join("")
|
|
45
|
+
.join(""), " ").concat(raw);
|
|
46
46
|
if (this.hasToC && type === "h2") {
|
|
47
47
|
this.toc.push({
|
|
48
48
|
array: Array.from({ length: (length_1 - 1) * 2 }),
|
|
@@ -51,7 +51,7 @@ var WriterMarkdown = /** @class */ (function (_super) {
|
|
|
51
51
|
}
|
|
52
52
|
break;
|
|
53
53
|
case "quote":
|
|
54
|
-
this.source += "> "
|
|
54
|
+
this.source += "> ".concat(raw);
|
|
55
55
|
break;
|
|
56
56
|
case "p":
|
|
57
57
|
this.source += raw;
|
|
@@ -78,7 +78,7 @@ var WriterMarkdown = /** @class */ (function (_super) {
|
|
|
78
78
|
this.source = this.source.replace("<!-- __TOC__ -->", this.toc
|
|
79
79
|
.map(function (_a) {
|
|
80
80
|
var array = _a.array, raw = _a.raw;
|
|
81
|
-
return array.join(" ")
|
|
81
|
+
return "".concat(array.join(" "), " - [").concat(raw, "](#").concat(raw.toLowerCase().replace(/\`/g, "").replace(/\s+/g, "-"), ")");
|
|
82
82
|
})
|
|
83
83
|
.join("\n"));
|
|
84
84
|
return this.source;
|
|
@@ -48,6 +48,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
48
48
|
};
|
|
49
49
|
exports.__esModule = true;
|
|
50
50
|
var path = require("path");
|
|
51
|
+
var path_1 = require("../path");
|
|
51
52
|
var Writer_1 = require("./Writer");
|
|
52
53
|
function writeJson(components, options) {
|
|
53
54
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -59,7 +60,7 @@ function writeJson(components, options) {
|
|
|
59
60
|
total: components.size,
|
|
60
61
|
components: Array.from(components, function (_a) {
|
|
61
62
|
var moduleName = _a[0], component = _a[1];
|
|
62
|
-
return (__assign(__assign({}, component), { filePath: path.join(options.inputDir, path.normalize(component.filePath)) }));
|
|
63
|
+
return (__assign(__assign({}, component), { filePath: (0, path_1.normalizeSeparators)(path.join(options.inputDir, path.normalize(component.filePath))) }));
|
|
63
64
|
}).sort(function (a, b) {
|
|
64
65
|
if (a.moduleName < b.moduleName)
|
|
65
66
|
return -1;
|
|
@@ -73,7 +74,7 @@ function writeJson(components, options) {
|
|
|
73
74
|
return [4 /*yield*/, writer.write(output_path, JSON.stringify(output))];
|
|
74
75
|
case 1:
|
|
75
76
|
_a.sent();
|
|
76
|
-
process.stdout.write("created \""
|
|
77
|
+
process.stdout.write("created \"".concat(options.outFile, "\".\n"));
|
|
77
78
|
return [2 /*return*/];
|
|
78
79
|
}
|
|
79
80
|
});
|
|
@@ -55,15 +55,15 @@ var MD_TYPE_UNDEFINED = "--";
|
|
|
55
55
|
function formatPropType(type) {
|
|
56
56
|
if (type === undefined)
|
|
57
57
|
return MD_TYPE_UNDEFINED;
|
|
58
|
-
return "<code>"
|
|
58
|
+
return "<code>".concat(type.replace(/\|/g, "|"), "</code>");
|
|
59
59
|
}
|
|
60
60
|
function escapeHtml(text) {
|
|
61
61
|
return text.replace(/\</g, "<").replace(/\>/g, ">");
|
|
62
62
|
}
|
|
63
63
|
function formatPropValue(value) {
|
|
64
64
|
if (value === undefined)
|
|
65
|
-
return
|
|
66
|
-
return "<code>"
|
|
65
|
+
return "<code>".concat(value, "</code>");
|
|
66
|
+
return "<code>".concat(value.replace(/`/g, "\\`").replace(/\|/g, "|"), "</code>");
|
|
67
67
|
}
|
|
68
68
|
function formatPropDescription(description) {
|
|
69
69
|
if (description === undefined || description.trim().length === 0)
|
|
@@ -104,11 +104,11 @@ function writeMarkdown(components, options) {
|
|
|
104
104
|
keys = Array.from(components.keys()).sort();
|
|
105
105
|
keys.forEach(function (key) {
|
|
106
106
|
var component = components.get(key);
|
|
107
|
-
document.append("h2", "`"
|
|
107
|
+
document.append("h2", "`".concat(component.moduleName, "`"));
|
|
108
108
|
if (component.typedefs.length > 0) {
|
|
109
|
-
document.append("h3", "Types").append("raw", "```ts\n"
|
|
109
|
+
document.append("h3", "Types").append("raw", "```ts\n".concat((0, writer_ts_definitions_1.getTypeDefs)({
|
|
110
110
|
typedefs: component.typedefs
|
|
111
|
-
})
|
|
111
|
+
}), "\n```\n\n"));
|
|
112
112
|
}
|
|
113
113
|
document.append("h3", "Props");
|
|
114
114
|
if (component.props.length > 0) {
|
|
@@ -121,7 +121,7 @@ function writeMarkdown(components, options) {
|
|
|
121
121
|
return 0;
|
|
122
122
|
})
|
|
123
123
|
.forEach(function (prop) {
|
|
124
|
-
document.append("raw", "| "
|
|
124
|
+
document.append("raw", "| ".concat(prop.name, " | ").concat("<code>".concat(prop.kind, "</code>"), " | ").concat(prop.reactive ? "Yes" : "No", " | ").concat(formatPropType(prop.type), " | ").concat(formatPropValue(prop.value), " | ").concat(formatPropDescription(prop.description), " |\n"));
|
|
125
125
|
});
|
|
126
126
|
}
|
|
127
127
|
else {
|
|
@@ -131,7 +131,7 @@ function writeMarkdown(components, options) {
|
|
|
131
131
|
if (component.slots.length > 0) {
|
|
132
132
|
document.append("raw", SLOT_TABLE_HEADER);
|
|
133
133
|
component.slots.forEach(function (slot) {
|
|
134
|
-
document.append("raw", "| "
|
|
134
|
+
document.append("raw", "| ".concat(slot["default"] ? MD_TYPE_UNDEFINED : slot.name, " | ").concat(slot["default"] ? "Yes" : "No", " | ").concat(formatSlotProps(slot.slot_props), " | ").concat(formatSlotFallback(slot.fallback), " |\n"));
|
|
135
135
|
});
|
|
136
136
|
}
|
|
137
137
|
else {
|
|
@@ -141,7 +141,7 @@ function writeMarkdown(components, options) {
|
|
|
141
141
|
if (component.events.length > 0) {
|
|
142
142
|
document.append("raw", EVENT_TABLE_HEADER);
|
|
143
143
|
component.events.forEach(function (event) {
|
|
144
|
-
document.append("raw", "| "
|
|
144
|
+
document.append("raw", "| ".concat(event.name, " | ").concat(event.type, " | ").concat(event.type === "dispatched" ? formatEventDetail(event.detail) : MD_TYPE_UNDEFINED, " |\n"));
|
|
145
145
|
});
|
|
146
146
|
}
|
|
147
147
|
else {
|
|
@@ -151,7 +151,7 @@ function writeMarkdown(components, options) {
|
|
|
151
151
|
return [4 /*yield*/, document.write(output_path, document.end())];
|
|
152
152
|
case 1:
|
|
153
153
|
_a.sent();
|
|
154
|
-
process.stdout.write("created \""
|
|
154
|
+
process.stdout.write("created \"".concat(options.outFile, "\".\n"));
|
|
155
155
|
return [2 /*return*/];
|
|
156
156
|
}
|
|
157
157
|
});
|
|
@@ -69,19 +69,19 @@ exports.formatTsProps = formatTsProps;
|
|
|
69
69
|
function getTypeDefs(def) {
|
|
70
70
|
if (def.typedefs.length === 0)
|
|
71
71
|
return EMPTY_STR;
|
|
72
|
-
return def.typedefs.map(function (typedef) { return "export "
|
|
72
|
+
return def.typedefs.map(function (typedef) { return "export ".concat(typedef.ts); }).join("\n\n");
|
|
73
73
|
}
|
|
74
74
|
exports.getTypeDefs = getTypeDefs;
|
|
75
75
|
function clampKey(key) {
|
|
76
76
|
if (/(\-|\s+|\:)/.test(key)) {
|
|
77
|
-
return /(\"|\')/.test(key) ? key : "[\""
|
|
77
|
+
return /(\"|\')/.test(key) ? key : "[\"".concat(key, "\"]");
|
|
78
78
|
}
|
|
79
79
|
return key;
|
|
80
80
|
}
|
|
81
81
|
function addCommentLine(value, returnValue) {
|
|
82
82
|
if (!value)
|
|
83
83
|
return undefined;
|
|
84
|
-
return "* "
|
|
84
|
+
return "* ".concat(returnValue || value, "\n");
|
|
85
85
|
}
|
|
86
86
|
function genPropDef(def) {
|
|
87
87
|
var _a;
|
|
@@ -89,28 +89,35 @@ function genPropDef(def) {
|
|
|
89
89
|
.filter(function (prop) { return !prop.isFunctionDeclaration && prop.kind !== "const"; })
|
|
90
90
|
.map(function (prop) {
|
|
91
91
|
var _a;
|
|
92
|
+
var defaultValue = prop.value;
|
|
93
|
+
if (typeof prop.value === "string") {
|
|
94
|
+
defaultValue = prop.value.replace(/\s+/g, " ");
|
|
95
|
+
}
|
|
96
|
+
if (prop.value === undefined) {
|
|
97
|
+
defaultValue = "undefined";
|
|
98
|
+
}
|
|
92
99
|
var prop_comments = [
|
|
93
100
|
addCommentLine((_a = prop.description) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, "\n* ")),
|
|
94
101
|
addCommentLine(prop.constant, "@constant"),
|
|
95
|
-
|
|
102
|
+
"* @default ".concat(defaultValue, "\n"),
|
|
96
103
|
]
|
|
97
104
|
.filter(Boolean)
|
|
98
105
|
.join("");
|
|
99
106
|
var prop_value = prop.constant && !prop.isFunction ? prop.value : prop.type;
|
|
100
|
-
return "\n "
|
|
107
|
+
return "\n ".concat(prop_comments.length > 0 ? "/**\n".concat(prop_comments, "*/") : EMPTY_STR, "\n ").concat(prop.name, "?: ").concat(prop_value, ";");
|
|
101
108
|
})
|
|
102
109
|
.join("\n");
|
|
103
|
-
var props_name = def.moduleName
|
|
110
|
+
var props_name = "".concat(def.moduleName, "Props");
|
|
104
111
|
var prop_def = EMPTY_STR;
|
|
105
112
|
if (((_a = def.rest_props) === null || _a === void 0 ? void 0 : _a.type) === "Element") {
|
|
106
113
|
var extend_tag_map = def.rest_props.name
|
|
107
114
|
.split("|")
|
|
108
|
-
.map(function (name) { return "svelte.JSX.HTMLAttributes<HTMLElementTagNameMap[\""
|
|
115
|
+
.map(function (name) { return "svelte.JSX.HTMLAttributes<HTMLElementTagNameMap[\"".concat(name.trim(), "\"]>"); })
|
|
109
116
|
.join(",");
|
|
110
|
-
prop_def = "\n export interface "
|
|
117
|
+
prop_def = "\n export interface ".concat(props_name, " extends ").concat(def["extends"] !== undefined ? "".concat(def["extends"].interface, ", ") : "").concat(extend_tag_map, " {\n ").concat(props, "\n }\n ");
|
|
111
118
|
}
|
|
112
119
|
else {
|
|
113
|
-
prop_def = "\n export interface "
|
|
120
|
+
prop_def = "\n export interface ".concat(props_name, " ").concat(def["extends"] !== undefined ? "extends ".concat(def["extends"].interface) : "", " {\n ").concat(props, "\n }\n ");
|
|
114
121
|
}
|
|
115
122
|
return {
|
|
116
123
|
props_name: props_name,
|
|
@@ -122,14 +129,14 @@ function genSlotDef(def) {
|
|
|
122
129
|
.map(function (_a) {
|
|
123
130
|
var name = _a.name, slot_props = _a.slot_props, rest = __rest(_a, ["name", "slot_props"]);
|
|
124
131
|
var key = rest["default"] ? "default" : clampKey(name);
|
|
125
|
-
return clampKey(key)
|
|
132
|
+
return "".concat(clampKey(key), ": ").concat(formatTsProps(slot_props), ";");
|
|
126
133
|
})
|
|
127
134
|
.join("\n");
|
|
128
135
|
}
|
|
129
136
|
function genEventDef(def) {
|
|
130
137
|
return def.events
|
|
131
138
|
.map(function (event) {
|
|
132
|
-
return clampKey(event.name)
|
|
139
|
+
return "".concat(clampKey(event.name), ": ").concat(event.type === "dispatched" ? "CustomEvent<".concat(event.detail || ANY_TYPE, ">") : "WindowEventMap[\"".concat(event.name, "\"]"), ";");
|
|
133
140
|
})
|
|
134
141
|
.join("\n");
|
|
135
142
|
}
|
|
@@ -139,24 +146,24 @@ function genAccessors(def) {
|
|
|
139
146
|
.map(function (prop) {
|
|
140
147
|
var _a;
|
|
141
148
|
var prop_comments = [addCommentLine((_a = prop.description) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, "\n* "))].filter(Boolean).join("");
|
|
142
|
-
return "\n "
|
|
149
|
+
return "\n ".concat(prop_comments.length > 0 ? "/**\n".concat(prop_comments, "*/") : EMPTY_STR, "\n ").concat(prop.name, ": ").concat(prop.type, ";");
|
|
143
150
|
})
|
|
144
151
|
.join("\n");
|
|
145
152
|
}
|
|
146
153
|
function genImports(def) {
|
|
147
154
|
if (def["extends"] === undefined)
|
|
148
155
|
return "";
|
|
149
|
-
return "import { "
|
|
156
|
+
return "import { ".concat(def["extends"].interface, " } from ").concat(def["extends"]["import"], ";");
|
|
150
157
|
}
|
|
151
158
|
function genComponentComment(def) {
|
|
152
159
|
if (!def.componentComment)
|
|
153
160
|
return "";
|
|
154
161
|
if (!/\n/.test(def.componentComment))
|
|
155
|
-
return "/** "
|
|
156
|
-
return "/*"
|
|
162
|
+
return "/** ".concat(def.componentComment.trim(), " */");
|
|
163
|
+
return "/*".concat(def.componentComment
|
|
157
164
|
.split("\n")
|
|
158
|
-
.map(function (line) { return "* "
|
|
159
|
-
.join("\n")
|
|
165
|
+
.map(function (line) { return "* ".concat(line); })
|
|
166
|
+
.join("\n"), "\n*/");
|
|
160
167
|
}
|
|
161
168
|
function writeTsDefinition(component) {
|
|
162
169
|
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;
|
|
@@ -166,7 +173,7 @@ function writeTsDefinition(component) {
|
|
|
166
173
|
rest_props: rest_props,
|
|
167
174
|
"extends": _extends
|
|
168
175
|
}), props_name = _a.props_name, prop_def = _a.prop_def;
|
|
169
|
-
return "\n /// <reference types=\"svelte\" />\n import { SvelteComponentTyped } from \"svelte\";\n "
|
|
176
|
+
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 }");
|
|
170
177
|
}
|
|
171
178
|
exports.writeTsDefinition = writeTsDefinition;
|
|
172
179
|
function writeTsDefinitions(components, options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sveld",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Generate TypeScript definitions for your Svelte components.",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"test": "run-p test:*",
|
|
12
12
|
"test:unit": "tsnd node_modules/tape/bin/tape tests/*.test.ts",
|
|
13
13
|
"test:integration": "node tests/integration",
|
|
14
|
-
"format": "prettier --write
|
|
14
|
+
"format": "prettier --write \"{src,tests}/**/*.{js,ts,svelte}\"",
|
|
15
15
|
"prepack": "run-s build test"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
"comment-parser": "^0.7.6",
|
|
24
24
|
"fast-glob": "^3.2.7",
|
|
25
25
|
"fs-extra": "^9.0.1",
|
|
26
|
-
"prettier": "^2.
|
|
27
|
-
"rollup": "^2.
|
|
26
|
+
"prettier": "^2.5.1",
|
|
27
|
+
"rollup": "^2.66.0",
|
|
28
28
|
"rollup-plugin-svelte": "^7.1.0",
|
|
29
|
-
"svelte": "^3.
|
|
30
|
-
"svelte-preprocess": "^4.
|
|
31
|
-
"typescript": "^4.
|
|
29
|
+
"svelte": "^3.46.2",
|
|
30
|
+
"svelte-preprocess": "^4.10.2",
|
|
31
|
+
"typescript": "^4.5.5"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/fs-extra": "^9.0.12",
|