@next-core/build-next-bricks 1.8.0 → 1.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next-core/build-next-bricks",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "description": "Build next bricks",
5
5
  "homepage": "https://github.com/easyops-cn/next-core/tree/v3/packages/build-next-bricks",
6
6
  "license": "GPL-3.0",
@@ -49,7 +49,7 @@
49
49
  "webpack": "^5.84.1"
50
50
  },
51
51
  "devDependencies": {
52
- "@next-core/brick-manifest": "^0.1.0"
52
+ "@next-core/brick-manifest": "^0.3.0"
53
53
  },
54
- "gitHead": "a2f8ca6b36b45b4a306f4b933e6bc2ca195cdd1f"
54
+ "gitHead": "14b533269133db07014b416199d988b0025f2b3c"
55
55
  }
package/src/build.js CHANGED
@@ -81,6 +81,8 @@ async function getWebpackConfig(config) {
81
81
  "js-yaml",
82
82
  "i18next-browser-languagedetector",
83
83
  "react-i18next",
84
+ "@easyops-cn/brick-next-pipes",
85
+ "@next-core/pipes",
84
86
  "@next-core/runtime",
85
87
  "@next-core/easyops-runtime",
86
88
  "@next-core/http",
@@ -1,10 +1,12 @@
1
1
  import { parse } from "doctrine";
2
+ import { getTypeAnnotation } from "./utils.js";
2
3
 
3
4
  /**
4
5
  * @typedef {import("@next-core/brick-manifest").BrickManifest} BrickManifest
5
6
  * @typedef {import("@next-core/brick-manifest").PropertyManifest} PropertyManifest
6
7
  * @typedef {import("@next-core/brick-manifest").EventManifest} EventManifest
7
8
  * @typedef {import("@next-core/brick-manifest").MethodManifest} MethodManifest
9
+ * @typedef {import("@next-core/brick-manifest").ProviderManifest} ProviderManifest
8
10
  * @typedef {import("@babel/types").Node} Node
9
11
  * @typedef {import("@babel/traverse").NodePath} NodePath
10
12
  * @typedef {import("@babel/types").ClassDeclaration} ClassDeclaration
@@ -14,7 +16,8 @@ import { parse } from "doctrine";
14
16
  /**
15
17
  * @param {string} name
16
18
  * @param {NodePath} nodePath
17
- * @param {string} source
19
+ * @param {string} source'
20
+ * @param {Set<string>} referenceSet
18
21
  * @returns {BrickManifest}
19
22
  */
20
23
  export default function makeBrickManifest(name, nodePath, source) {
@@ -32,6 +35,7 @@ export default function makeBrickManifest(name, nodePath, source) {
32
35
  const docComment = findDocComment(nodePath, source);
33
36
  if (docComment) {
34
37
  manifest.description = docComment.description;
38
+ manifest.deprecated = getDeprecatedInfo(docComment.tags);
35
39
  for (const tag of docComment.tags) {
36
40
  if (tag.title === "slot") {
37
41
  const match = tag.description.match(/^(?:([-\w]+)\s+-\s+)?(.*)$/);
@@ -53,6 +57,25 @@ export default function makeBrickManifest(name, nodePath, source) {
53
57
  return manifest;
54
58
  }
55
59
 
60
+ /**
61
+ * @param {string} name
62
+ * @param {NodePath} nodePath
63
+ * @param {string} source
64
+ * @returns {ProviderManifest}
65
+ */
66
+ export function makeProviderManifest(name, nodePath, source) {
67
+ /** @type {ProviderManifest} */
68
+ const manifest = {
69
+ name,
70
+ };
71
+ const docComment = findDocComment(nodePath, source);
72
+ if (docComment) {
73
+ manifest.description = docComment.description;
74
+ manifest.deprecated = getDeprecatedInfo(docComment.tags);
75
+ }
76
+ return manifest;
77
+ }
78
+
56
79
  /**
57
80
  * @param {NodePath} nodePath
58
81
  * @param {string} source
@@ -73,8 +96,9 @@ function findDocComment({ node, parentPath }, source) {
73
96
  * @param {BrickManifest} manifest
74
97
  * @param {Node[]} nodes
75
98
  * @param {string} source
99
+ * @param {Set<string>} referenceSet
76
100
  */
77
- function scanFields(manifest, nodes, source) {
101
+ function scanFields(manifest, nodes, source, referenceSet = new Set()) {
78
102
  for (const node of nodes) {
79
103
  if (node.type === "ClassAccessorProperty" && node.decorators?.length) {
80
104
  for (const { expression } of node.decorators) {
@@ -124,6 +148,12 @@ function scanFields(manifest, nodes, source) {
124
148
  ) {
125
149
  const { typeAnnotation } = node.typeAnnotation;
126
150
  prop.type = getTypeWithoutUndefined(typeAnnotation, source);
151
+ prop.types = getTypesWithoutUndefined(
152
+ typeAnnotation,
153
+ source,
154
+ referenceSet
155
+ );
156
+ prop.reference = [...referenceSet];
127
157
  }
128
158
  if (node.value && !prop.default) {
129
159
  prop.default = source.substring(
@@ -188,6 +218,12 @@ function scanFields(manifest, nodes, source) {
188
218
  const param = typeAnnotation.typeParameters.params[0];
189
219
  event.detail ??= {};
190
220
  event.detail.type = source.substring(param.start, param.end);
221
+ event.detail.types = getTypeAnnotation(
222
+ param,
223
+ source,
224
+ referenceSet
225
+ );
226
+ event.detail.reference = [...referenceSet];
191
227
  }
192
228
  }
193
229
  manifest.events.push(event);
@@ -223,6 +259,12 @@ function scanFields(manifest, nodes, source) {
223
259
  typeAnnotation.start,
224
260
  typeAnnotation.end
225
261
  );
262
+ method.return.types = getTypeAnnotation(
263
+ typeAnnotation,
264
+ source,
265
+ referenceSet
266
+ );
267
+ method.return.reference = [...referenceSet];
226
268
  }
227
269
  manifest.methods.push(method);
228
270
  }
@@ -266,6 +308,23 @@ function getTypeWithoutUndefined(node, source) {
266
308
  return source.substring(node.start, node.end);
267
309
  }
268
310
 
311
+ /**
312
+ * @param {import("@babel/types")/.typeAnnotation} typeAnnotation
313
+ * @param {string} source
314
+ * @param {Set<string} set
315
+ */
316
+ function getTypesWithoutUndefined(typeAnnotation, source, set) {
317
+ if (typeAnnotation.type === "TSUnionType" && typeAnnotation.types) {
318
+ return {
319
+ type: "union",
320
+ types: typeAnnotation.types
321
+ .filter((type) => type.type !== "TSUndefinedKeyword")
322
+ .map((item) => getTypeAnnotation(item, source, set)),
323
+ };
324
+ }
325
+ return getTypeAnnotation(typeAnnotation, source, set);
326
+ }
327
+
269
328
  /**
270
329
  * @param {Tag[]} tags
271
330
  * @param {string} title
package/src/scanBricks.js CHANGED
@@ -5,7 +5,9 @@ import { parse } from "@babel/parser";
5
5
  import babelTraverse from "@babel/traverse";
6
6
  import _ from "lodash";
7
7
  import getCamelPackageName from "./getCamelPackageName.js";
8
- import makeBrickManifest from "./makeBrickManifest.js";
8
+ import makeBrickManifest, {
9
+ makeProviderManifest,
10
+ } from "./makeBrickManifest.js";
9
11
  import { BASE_TYPE, TS_KEYWORD_LIST, getTypeAnnotation } from "./utils.js";
10
12
  import {
11
13
  isImportDefaultSpecifier,
@@ -57,6 +59,7 @@ export default async function scanBricks(packageDir) {
57
59
  package: packageJson.name,
58
60
  name: packageName,
59
61
  bricks: [],
62
+ providers: [],
60
63
  };
61
64
 
62
65
  /** @type {Map<string, Set<string>} */
@@ -216,7 +219,10 @@ export default async function scanBricks(packageDir) {
216
219
  }
217
220
 
218
221
  traverse(ast, {
219
- CallExpression({ node: { callee, arguments: args } }) {
222
+ CallExpression(nodePath) {
223
+ const {
224
+ node: { callee, arguments: args },
225
+ } = nodePath;
220
226
  // Match `customProcessors.define(...)`
221
227
  // Match `customElements.define(...)`
222
228
  if (
@@ -265,6 +271,9 @@ export default async function scanBricks(packageDir) {
265
271
  const { type, value: fullName } = args[0];
266
272
  if (type === "StringLiteral") {
267
273
  collectBrick(fullName);
274
+ manifest.providers.push(
275
+ makeProviderManifest(fullName, nodePath, content)
276
+ );
268
277
  } else {
269
278
  throw new Error(
270
279
  "Please call `customElements.define()` only with literal string"
@@ -691,26 +700,51 @@ export default async function scanBricks(packageDir) {
691
700
 
692
701
  if (manifest && manifest.bricks.length) {
693
702
  manifest.bricks.forEach((brickDoc) => {
694
- const { name, properties } = brickDoc;
703
+ const { name, properties = [], events = [], methods = [] } = brickDoc;
695
704
  const importInfo = bricksImportsInfo[name];
696
- const fieldTypes = properties
697
- .map(
698
- ({ type }) =>
699
- type &&
700
- type
701
- .match(/\w+/g)
702
- .filter(
703
- (item) => !(BASE_TYPE[item] || TS_KEYWORD_LIST.includes(item))
704
- )
705
- )
706
- .flat(1);
705
+ const fieldTypes = new Set();
706
+
707
+ properties.forEach((item) => {
708
+ importInfo.properties = (importInfo.properties || []).concat({
709
+ name: item.name,
710
+ types: item.types,
711
+ });
712
+ (item.reference || []).forEach((item) => fieldTypes.add(item));
713
+
714
+ delete item.types;
715
+ delete item.reference;
716
+ });
717
+
718
+ events.forEach((item) => {
719
+ if (item.detail) {
720
+ importInfo.events = (importInfo.events || []).concat({
721
+ name: item.name,
722
+ types: item.detail.types,
723
+ });
724
+ (item.detail.reference || []).forEach((item) => fieldTypes.add(item));
725
+
726
+ delete item.detail.types;
727
+ delete item.detail.reference;
728
+ }
729
+ });
730
+
731
+ methods.forEach((item) => {
732
+ if (item.return) {
733
+ importInfo.methods = (importInfo.methods || []).concat({
734
+ name: item.name,
735
+ types: item.return.types,
736
+ });
737
+ (item.return.reference || []).forEach((item) => fieldTypes.add(item));
738
+
739
+ delete item.return.types;
740
+ delete item.return.reference;
741
+ }
742
+ });
707
743
 
708
744
  const importKeysSet = new Set();
709
- if (Array.isArray(fieldTypes) && fieldTypes.length) {
710
- [...new Set(fieldTypes)].forEach((type) =>
711
- findType(type, importInfo, importKeysSet)
712
- );
713
- }
745
+ [...fieldTypes].forEach((type) =>
746
+ findType(type, importInfo, importKeysSet)
747
+ );
714
748
  });
715
749
  }
716
750
 
@@ -725,7 +759,15 @@ export default async function scanBricks(packageDir) {
725
759
  manifest,
726
760
  types: Object.fromEntries(
727
761
  Object.entries(bricksImportsInfo).map(([k, v]) => {
728
- return [k, v.types];
762
+ return [
763
+ k,
764
+ {
765
+ properties: v.properties,
766
+ events: v.events,
767
+ methods: v.methods,
768
+ types: v.types,
769
+ },
770
+ ];
729
771
  })
730
772
  ),
731
773
  };