@next-core/build-next-bricks 1.11.1 → 1.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/package.json +3 -3
- package/src/build.js +55 -1
- package/src/makeBrickManifest.js +3 -1
- package/src/scanBricks.js +96 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next-core/build-next-bricks",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.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.
|
|
52
|
+
"@next-core/brick-manifest": "^0.5.0"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "06fca79a3a3b63a995d92729cfbed8e312cfdccd"
|
|
55
55
|
}
|
package/src/build.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
2
3
|
import { readFile } from "node:fs/promises";
|
|
3
4
|
import { createRequire } from "node:module";
|
|
4
5
|
import webpack from "webpack";
|
|
@@ -6,6 +7,7 @@ import MiniCssExtractPlugin from "mini-css-extract-plugin";
|
|
|
6
7
|
import postcssPresetEnv from "postcss-preset-env";
|
|
7
8
|
import cssnano from "cssnano";
|
|
8
9
|
import cssnanoPresetLite from "cssnano-preset-lite";
|
|
10
|
+
import _ from "lodash";
|
|
9
11
|
import EmitBricksJsonPlugin from "./EmitBricksJsonPlugin.js";
|
|
10
12
|
import getCamelPackageName from "./getCamelPackageName.js";
|
|
11
13
|
import getSvgrLoaders from "./getSvgrLoaders.js";
|
|
@@ -65,13 +67,48 @@ async function getWebpackConfig(config) {
|
|
|
65
67
|
|
|
66
68
|
const packageJsonFile = await readFile(
|
|
67
69
|
path.join(packageDir, "package.json"),
|
|
68
|
-
|
|
70
|
+
"utf-8"
|
|
69
71
|
);
|
|
70
72
|
const packageJson = JSON.parse(packageJsonFile);
|
|
71
73
|
const packageName = packageJson.name.split("/").pop();
|
|
72
74
|
const camelPackageName = getCamelPackageName(packageName);
|
|
73
75
|
const libName = isBricks ? `bricks/${packageName}` : config.type;
|
|
74
76
|
|
|
77
|
+
/** @type {string[]} */
|
|
78
|
+
let commonBricks;
|
|
79
|
+
const commonBricksJsonFile = path.join(
|
|
80
|
+
packageDir,
|
|
81
|
+
"../../shared/common-bricks/common-bricks.json"
|
|
82
|
+
);
|
|
83
|
+
if (existsSync(commonBricksJsonFile)) {
|
|
84
|
+
const commonBricksJson = JSON.parse(
|
|
85
|
+
await readFile(commonBricksJsonFile, "utf-8")
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
/** @type {Set<string, string>} */
|
|
89
|
+
const commonBricksMap = new Map();
|
|
90
|
+
for (const [pkg, bricks] of Object.entries(commonBricksJson)) {
|
|
91
|
+
for (const brick of bricks) {
|
|
92
|
+
const existedPkg = commonBricksMap.get(brick);
|
|
93
|
+
if (existedPkg && existedPkg !== pkg) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`Conflicted common brick: "${brick}" in package "${existedPkg}" and "${pkg}"`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
commonBricksMap.set(brick, pkg);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
commonBricks = Object.prototype.hasOwnProperty.call(
|
|
103
|
+
commonBricksJson,
|
|
104
|
+
packageName
|
|
105
|
+
)
|
|
106
|
+
? commonBricksJson[packageName]
|
|
107
|
+
: [];
|
|
108
|
+
} else {
|
|
109
|
+
commonBricks = [];
|
|
110
|
+
}
|
|
111
|
+
|
|
75
112
|
const sharedSingletonPackages = [
|
|
76
113
|
"history",
|
|
77
114
|
"i18next",
|
|
@@ -181,6 +218,23 @@ async function getWebpackConfig(config) {
|
|
|
181
218
|
}
|
|
182
219
|
}
|
|
183
220
|
|
|
221
|
+
const invalidElements = _.difference(elements, commonBricks);
|
|
222
|
+
if (invalidElements.length > 0) {
|
|
223
|
+
throw new Error(
|
|
224
|
+
`Find common bricks in \`${packageName}\` which are not in common-bricks.json: ${invalidElements.join(
|
|
225
|
+
", "
|
|
226
|
+
)}`
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
const missingElements = _.difference(commonBricks, elements);
|
|
230
|
+
if (missingElements.length > 0) {
|
|
231
|
+
throw new Error(
|
|
232
|
+
`Missing common bricks in \`${packageName}\`: ${missingElements.join(
|
|
233
|
+
", "
|
|
234
|
+
)}`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
184
238
|
/** @type {Record<string, { import: string; name: string; }>} */
|
|
185
239
|
const extraExposes = {};
|
|
186
240
|
|
package/src/makeBrickManifest.js
CHANGED
|
@@ -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
|
-
|
|
193
|
-
if (
|
|
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: "${
|
|
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")]:
|
|
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
|
-
|
|
385
|
+
if (fullName.includes(".")) {
|
|
386
|
+
[brickNamespace, brickName] = fullName.split(".");
|
|
383
387
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
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
|
-
|
|
391
|
-
|
|
392
|
-
|
|
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
|
-
|
|
397
|
-
|
|
398
|
-
|
|
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(
|
|
488
|
+
manifest.bricks.push(
|
|
489
|
+
makeBrickManifest(
|
|
490
|
+
fullName,
|
|
491
|
+
aliases.length > 0 ? aliases : undefined,
|
|
492
|
+
nodePath,
|
|
493
|
+
content
|
|
494
|
+
)
|
|
495
|
+
);
|
|
440
496
|
|
|
441
|
-
|
|
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) {
|