@next-core/build-next-bricks 1.11.1 → 1.12.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.11.1",
3
+ "version": "1.12.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.4.1"
52
+ "@next-core/brick-manifest": "^0.5.0"
53
53
  },
54
- "gitHead": "cdd45da473b22e9f884bcbf2f956b4fc564ab2fd"
54
+ "gitHead": "fbd30ceaf3abd9c99c5dc2085a81413c68e367e0"
55
55
  }
@@ -41,11 +41,12 @@ import { getTypeAnnotation } from "./getTypeDeclaration.js";
41
41
 
42
42
  /**
43
43
  * @param {string} name
44
+ * @param {string[] | undefined} alias
44
45
  * @param {NodePath} nodePath
45
46
  * @param {string} source
46
47
  * @returns {BrickManifestAndTypes}
47
48
  */
48
- export default function makeBrickManifest(name, nodePath, source) {
49
+ export default function makeBrickManifest(name, alias, nodePath, source) {
49
50
  const classPath =
50
51
  /** @type {import("@babel/traverse").NodePath<ClassDeclaration>} */ (
51
52
  nodePath.parentPath
@@ -53,6 +54,7 @@ export default function makeBrickManifest(name, nodePath, source) {
53
54
  /** @type {BrickManifestAndTypes} */
54
55
  const manifest = {
55
56
  name,
57
+ alias,
56
58
  properties: [],
57
59
  events: [],
58
60
  slots: [],
package/src/scanBricks.js CHANGED
@@ -189,8 +189,8 @@ export default async function scanBricks(packageDir) {
189
189
  let brickNamespace;
190
190
  /** @type string */
191
191
  let brickName;
192
- let noNamespace = false;
193
- if (fullName.includes(".")) {
192
+ const hasNamespace = fullName.includes(".");
193
+ if (hasNamespace) {
194
194
  [brickNamespace, brickName] = fullName.split(".");
195
195
  if (brickNamespace !== packageName) {
196
196
  throw new Error(
@@ -212,10 +212,9 @@ export default async function scanBricks(packageDir) {
212
212
  } else {
213
213
  // For third-party custom elements, there maybe no namespace.
214
214
  brickName = fullName;
215
- noNamespace = true;
216
215
  if (!validCustomElementName.test(brickName)) {
217
216
  throw new Error(
218
- `Invalid brick: "${fullName}", the brick name must include a \`-\``
217
+ `Invalid brick: "${brickName}", the brick name must include a \`-\``
219
218
  );
220
219
  }
221
220
  }
@@ -228,7 +227,7 @@ export default async function scanBricks(packageDir) {
228
227
  .replace(/\.[^.]+$/, "")
229
228
  .replace(/\/index$/, "")}`,
230
229
  name: getExposeName(brickName),
231
- [Symbol.for("noNamespace")]: noNamespace,
230
+ [Symbol.for("noNamespace")]: !hasNamespace,
232
231
  });
233
232
  }
234
233
 
@@ -378,29 +377,53 @@ export default async function scanBricks(packageDir) {
378
377
  );
379
378
  }
380
379
 
380
+ /** @type {string | undefined} */
381
+ let brickNamespace;
382
+ /** @type {string} */
383
+ let brickName;
381
384
  const fullName = expression.arguments[0].value;
382
- const [brickNamespace, brickName] = fullName.split(".");
385
+ if (fullName.includes(".")) {
386
+ [brickNamespace, brickName] = fullName.split(".");
383
387
 
384
- if (brickNamespace !== packageName) {
385
- throw new Error(
386
- `Invalid brick: "${fullName}", expecting prefixed with the package name: "${packageName}"`
387
- );
388
- }
388
+ if (brickNamespace !== packageName) {
389
+ throw new Error(
390
+ `Invalid brick: "${fullName}", expecting prefixed with the package name: "${packageName}"`
391
+ );
392
+ }
389
393
 
390
- if (!validBrickName.test(fullName)) {
391
- throw new Error(
392
- `Invalid brick: "${fullName}", expecting: "PACKAGE-NAME.BRICK-NAME", where PACKAGE-NAME and BRICK-NAME must be lower-kebab-case, and BRICK-NAME must include a \`-\``
393
- );
394
- }
394
+ if (!validBrickName.test(fullName)) {
395
+ throw new Error(
396
+ `Invalid brick: "${fullName}", expecting: "PACKAGE-NAME.BRICK-NAME", where PACKAGE-NAME and BRICK-NAME must be lower-kebab-case, and BRICK-NAME must include a \`-\``
397
+ );
398
+ }
395
399
 
396
- if (brickName.startsWith("tpl-")) {
397
- throw new Error(
398
- `Invalid brick: "${fullName}", the brick name cannot be started with "tpl-"`
399
- );
400
+ if (brickName.startsWith("tpl-")) {
401
+ throw new Error(
402
+ `Invalid brick: "${fullName}", the brick name cannot be started with "tpl-"`
403
+ );
404
+ }
405
+ } else {
406
+ brickName = fullName;
407
+
408
+ if (!brickName.startsWith("eo-")) {
409
+ throw new Error(
410
+ `Invalid brick: "${brickName}", expecting prefixed with "eo-" for brick name without namespace`
411
+ );
412
+ }
413
+
414
+ if (!validCustomElementName.test(brickName)) {
415
+ throw new Error(
416
+ `Invalid brick: "${brickName}", expecting a \`-\` in brick name`
417
+ );
418
+ }
400
419
  }
401
420
 
402
421
  const defineOptions = expression.arguments[1];
422
+ /** @type {string[]} */
403
423
  const deps = [];
424
+ /** @type {string[]} */
425
+ const aliases = [];
426
+
404
427
  if (defineOptions && defineOptions.type === "ObjectExpression") {
405
428
  /** @type {import("@babel/types").ObjectProperty} */
406
429
  const brickDeps = defineOptions.properties.find(
@@ -427,6 +450,32 @@ export default async function scanBricks(packageDir) {
427
450
  );
428
451
  }
429
452
  }
453
+
454
+ /** @type {import("@babel/types").ObjectProperty} */
455
+ const alias = defineOptions.properties.find(
456
+ (prop) =>
457
+ prop.type === "ObjectProperty" &&
458
+ prop.key.type === "Identifier" &&
459
+ prop.key.name === "alias" &&
460
+ !prop.computed
461
+ );
462
+ if (alias) {
463
+ if (alias.value.type === "ArrayExpression") {
464
+ for (const item of alias.value.elements) {
465
+ if (item.type === "StringLiteral") {
466
+ aliases.push(item.value);
467
+ } else {
468
+ throw new Error(
469
+ `Invalid item in brick dependencies: ${item.type} of brick: "${fullName}", expecting only StringLiteral`
470
+ );
471
+ }
472
+ }
473
+ } else {
474
+ throw new Error(
475
+ `Invalid brick alias: ${alias.value.type} of brick: "${fullName}", expecting only ArrayExpression`
476
+ );
477
+ }
478
+ }
430
479
  }
431
480
  if (deps.length > 0) {
432
481
  specifiedDeps[fullName] = deps;
@@ -436,15 +485,39 @@ export default async function scanBricks(packageDir) {
436
485
 
437
486
  brickSourceFiles.set(fullName, filePath);
438
487
 
439
- manifest.bricks.push(makeBrickManifest(fullName, nodePath, content));
488
+ manifest.bricks.push(
489
+ makeBrickManifest(
490
+ fullName,
491
+ aliases.length > 0 ? aliases : undefined,
492
+ nodePath,
493
+ content
494
+ )
495
+ );
440
496
 
441
- exposes.set(`./${brickName}`, {
497
+ const expose = {
442
498
  import: `./${path
443
499
  .relative(packageDir, overrideImport || filePath)
444
500
  .replace(/\.[^.]+$/, "")
445
501
  .replace(/\/index$/, "")}`,
446
502
  name: getExposeName(brickName),
447
- });
503
+ [Symbol.for("noNamespace")]: !brickNamespace,
504
+ };
505
+
506
+ exposes.set(`./${brickName}`, expose);
507
+ for (const alias of aliases) {
508
+ brickSourceFiles.set(alias, filePath);
509
+ let lastName;
510
+ const hasNamespace = alias.includes(".");
511
+ if (hasNamespace) {
512
+ [, lastName] = alias.split(".");
513
+ } else {
514
+ lastName = alias;
515
+ }
516
+ exposes.set(`./${lastName}`, {
517
+ ...expose,
518
+ [Symbol.for("noNamespace")]: !hasNamespace,
519
+ });
520
+ }
448
521
  }
449
522
  },
450
523
  FunctionDeclaration(nodePath) {