@next-core/build-next-bricks 1.13.5 → 1.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.
@@ -55,6 +55,10 @@ try {
55
55
  path.join(distDir, "types.json"),
56
56
  JSON.stringify(config.types, null, 2)
57
57
  );
58
+ await writeFile(
59
+ path.join(distDir, "examples.json"),
60
+ JSON.stringify(config.examples, null, 2)
61
+ );
58
62
  }
59
63
  }
60
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next-core/build-next-bricks",
3
- "version": "1.13.5",
3
+ "version": "1.14.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",
@@ -51,5 +51,5 @@
51
51
  "devDependencies": {
52
52
  "@next-core/brick-manifest": "^0.5.1"
53
53
  },
54
- "gitHead": "cf63c505f1e47aa6b2918e46309c6e40b01431c3"
54
+ "gitHead": "479c966531db9a58feb3f6e0ccc255ff90a91549"
55
55
  }
@@ -8,7 +8,7 @@ const pluginName = "EmitBricksJsonPlugin";
8
8
 
9
9
  export default class EmitBricksJsonPlugin {
10
10
  /**
11
- * @param {{ packageName: string; bricks: string[]; processors: string[]; dependencies: Record<string, string[]>; manifest: PackageManifest; }} options
11
+ * @param {{ packageName: string; bricks: string[]; processors: string[]; dependencies: Record<string, string[]>; manifest: PackageManifest; examples: Record<string, {doc: string}> }} options
12
12
  */
13
13
  constructor(options) {
14
14
  this.packageName = options.packageName;
@@ -18,6 +18,7 @@ export default class EmitBricksJsonPlugin {
18
18
  this.dependencies = options.dependencies;
19
19
  this.manifest = options.manifest;
20
20
  this.types = options.types;
21
+ this.examples = options.examples;
21
22
  }
22
23
 
23
24
  /**
@@ -78,6 +79,12 @@ export default class EmitBricksJsonPlugin {
78
79
  new webpack.sources.RawSource(typesJson, false)
79
80
  );
80
81
 
82
+ const examplesJson = JSON.stringify(this.examples, null, 2);
83
+ compilation.emitAsset(
84
+ "examples.json",
85
+ new webpack.sources.RawSource(examplesJson, false)
86
+ );
87
+
81
88
  console.log("Defined bricks:", this.bricks);
82
89
  console.log("Defined elements:", this.elements);
83
90
  console.log("Defined processors:", this.processors);
package/src/build.js CHANGED
@@ -121,6 +121,7 @@ async function getWebpackConfig(config) {
121
121
  "js-yaml",
122
122
  "i18next-browser-languagedetector",
123
123
  "react-i18next",
124
+ "@babel/parser",
124
125
  "@easyops-cn/brick-next-pipes",
125
126
  "@next-core/pipes",
126
127
  "@next-core/runtime",
@@ -451,6 +452,7 @@ async function getWebpackConfig(config) {
451
452
  dependencies: config.dependencies,
452
453
  manifest: config.manifest,
453
454
  types: config.types,
455
+ examples: config.examples,
454
456
  }),
455
457
  ]
456
458
  : []),
package/src/getDocs.js ADDED
@@ -0,0 +1,45 @@
1
+ import {
2
+ extractExamplesInMarkdown,
3
+ htmlToYaml,
4
+ yamlToHtml,
5
+ htmlTagEntity,
6
+ } from "@next-core/doc-helpers";
7
+
8
+ const YAML_DELIMITER = "# -- YAML DELIMITER (1nbbm8) --";
9
+ const HTML_DELIMITER_START = "<!-- HTML DELIMITER start (1nbbm8) --";
10
+ const HTML_DELIMITER_END = "-- HTML DELIMITER end (1nbbm8) -->";
11
+
12
+ /**
13
+ * @param {string} markdown
14
+ * @param {import("@next-core/brick-manifest").PackageManifest[]} manifests
15
+ */
16
+ export function handleExamplesInMarkdown(markdown, manifests) {
17
+ const examples = extractExamplesInMarkdown(markdown, "");
18
+ let cursor = 0;
19
+ const chunks = [];
20
+ for (const example of examples) {
21
+ const nextCursor = example.codeIndex + example.code.length;
22
+ chunks.push(markdown.substring(cursor, nextCursor));
23
+ if (example.mode === "yaml") {
24
+ const html = yamlToHtml(example.code, manifests);
25
+ chunks.push(
26
+ `${YAML_DELIMITER}\n`,
27
+ html
28
+ .split("\n")
29
+ .map((line) => `# ${line}`)
30
+ .join("\n"),
31
+ "\n"
32
+ );
33
+ } else {
34
+ const yaml = htmlToYaml(example.code, manifests);
35
+ chunks.push(
36
+ `${HTML_DELIMITER_START}\n`,
37
+ htmlTagEntity(yaml),
38
+ `\n${HTML_DELIMITER_END}\n`
39
+ );
40
+ }
41
+ cursor = nextCursor;
42
+ }
43
+ chunks.push(markdown.substring(cursor));
44
+ return chunks.join("");
45
+ }
package/src/scanBricks.js CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  } from "@babel/types";
16
16
  import getTypeDeclaration from "./getTypeDeclaration.js";
17
17
  import isDeprecatedV2Packages from "./isDeprecatedV2Packages.js";
18
+ import { handleExamplesInMarkdown } from "./getDocs.js";
18
19
 
19
20
  /**
20
21
  *
@@ -40,7 +41,7 @@ const validExposeName = /^[-\w]+$/;
40
41
  * Scan defined bricks by AST.
41
42
  *
42
43
  * @param {string} packageDir
43
- * @returns {Promise<{exposes: Exposes; dependencies: Record<string, string[]>; manifest: PackageManifest; types: Record<string, unknown>}>}
44
+ * @returns {Promise<{exposes: Exposes; dependencies: Record<string, string[]>; manifest: PackageManifest; types: Record<string, unknown>; examples: Record<string, {doc: string}>}>}
44
45
  */
45
46
  export default async function scanBricks(packageDir) {
46
47
  /** @type {Map<string, Expose>} */
@@ -839,6 +840,36 @@ export default async function scanBricks(packageDir) {
839
840
  delete providerDoc.usedReferences;
840
841
  }
841
842
 
843
+ /** @type {Record<string, {doc: string}>} */
844
+ const examples = {};
845
+ const srcDocsDir = path.join(packageDir, "docs");
846
+
847
+ for (const brick of manifest.bricks.concat(manifest.providers ?? [])) {
848
+ const lastName = brick.name.split(".").pop();
849
+
850
+ const srcFilePath = path.join(srcDocsDir, `${brick.name}.md`);
851
+ const srcFilePathAlt = path.join(srcDocsDir, `${lastName}.md`);
852
+
853
+ /** @type {string} */
854
+ let brickDoc;
855
+ if (existsSync(srcFilePath)) {
856
+ brickDoc = handleExamplesInMarkdown(
857
+ await readFile(srcFilePath, "utf-8"),
858
+ [manifest]
859
+ );
860
+ } else if (existsSync(srcFilePathAlt)) {
861
+ brickDoc = handleExamplesInMarkdown(
862
+ await readFile(srcFilePathAlt, "utf-8"),
863
+ [manifest]
864
+ );
865
+ }
866
+ if (brickDoc) {
867
+ examples[brick.name] = {
868
+ doc: brickDoc,
869
+ };
870
+ }
871
+ }
872
+
842
873
  // console.log("exposes:", exposes);
843
874
 
844
875
  return {
@@ -871,5 +902,6 @@ export default async function scanBricks(packageDir) {
871
902
  ])
872
903
  )
873
904
  ),
905
+ examples,
874
906
  };
875
907
  }