@next-core/build-next-bricks 1.13.6 → 1.14.1

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.6",
3
+ "version": "1.14.1",
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",
@@ -31,9 +31,9 @@
31
31
  "node": ">=16"
32
32
  },
33
33
  "dependencies": {
34
- "@babel/parser": "^7.22.7",
35
- "@babel/traverse": "^7.22.8",
36
- "@svgr/webpack": "^8.0.1",
34
+ "@babel/parser": "^7.22.15",
35
+ "@babel/traverse": "^7.22.15",
36
+ "@svgr/webpack": "^8.1.0",
37
37
  "babel-loader": "^9.1.3",
38
38
  "css-loader": "^6.8.1",
39
39
  "cssnano": "^6.0.1",
@@ -41,15 +41,15 @@
41
41
  "doctrine": "^3.0.0",
42
42
  "lodash": "^4.17.21",
43
43
  "mini-css-extract-plugin": "^2.7.6",
44
- "postcss": "^8.4.26",
44
+ "postcss": "^8.4.29",
45
45
  "postcss-loader": "^7.3.3",
46
- "postcss-preset-env": "^8.5.1",
46
+ "postcss-preset-env": "^9.1.3",
47
47
  "style-loader": "^3.3.3",
48
- "typescript": "^5.1.6",
48
+ "typescript": "^5.2.2",
49
49
  "webpack": "^5.88.2"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@next-core/brick-manifest": "^0.5.1"
53
53
  },
54
- "gitHead": "ad9c309ab3cc264f9ae8b913aeb7e64448a974ca"
54
+ "gitHead": "3aa3336e81687b4c2e3e633c02cc2c802a1201d8"
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
@@ -452,6 +452,7 @@ async function getWebpackConfig(config) {
452
452
  dependencies: config.dependencies,
453
453
  manifest: config.manifest,
454
454
  types: config.types,
455
+ examples: config.examples,
455
456
  }),
456
457
  ]
457
458
  : []),
package/src/getDocs.js ADDED
@@ -0,0 +1,46 @@
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
+ * @returns {Promise<string>}
16
+ */
17
+ export async function handleExamplesInMarkdown(markdown, manifests) {
18
+ const examples = extractExamplesInMarkdown(markdown, "");
19
+ let cursor = 0;
20
+ const chunks = [];
21
+ for (const example of examples) {
22
+ const nextCursor = example.codeIndex + example.code.length;
23
+ chunks.push(markdown.substring(cursor, nextCursor));
24
+ if (example.mode === "yaml") {
25
+ const html = await yamlToHtml(example.code, manifests);
26
+ chunks.push(
27
+ `${YAML_DELIMITER}\n`,
28
+ html
29
+ .split("\n")
30
+ .map((line) => `# ${line}`)
31
+ .join("\n"),
32
+ "\n"
33
+ );
34
+ } else {
35
+ const yaml = htmlToYaml(example.code, manifests);
36
+ chunks.push(
37
+ `${HTML_DELIMITER_START}\n`,
38
+ htmlTagEntity(yaml),
39
+ `\n${HTML_DELIMITER_END}\n`
40
+ );
41
+ }
42
+ cursor = nextCursor;
43
+ }
44
+ chunks.push(markdown.substring(cursor));
45
+ return chunks.join("");
46
+ }
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 = await handleExamplesInMarkdown(
857
+ await readFile(srcFilePath, "utf-8"),
858
+ [manifest]
859
+ );
860
+ } else if (existsSync(srcFilePathAlt)) {
861
+ brickDoc = await 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
  }