@next-core/build-next-bricks 1.12.0 → 1.13.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.
package/index.d.ts CHANGED
@@ -2,7 +2,9 @@ import type { Compiler, Configuration, RuleSetRule, container } from "webpack";
2
2
 
3
3
  export declare function build(config: BuildNextBricksConfig): Compiler;
4
4
  export declare function getSvgrLoaders(options?: {
5
- /** Set it to true for font icons */
5
+ /** Set it to true for icon font, defaults to false. */
6
+ icon?: boolean | string | number;
7
+ /** Defaults to true when `icon` is truthy, otherwise defaults to false. */
6
8
  convertCurrentColor?: boolean;
7
9
  }): RuleSetRule["use"];
8
10
 
@@ -77,7 +79,7 @@ export interface BuildNextBricksConfig {
77
79
  svgAsReactComponent?: boolean;
78
80
  /** Customize rules for svg, this will take precedence over `svgAsReactComponent` */
79
81
  svgRules?: RuleSetRule[];
80
- /** By default the image assets are named `images/[hash][ext][query]` */
82
+ /** By default the image assets are named `images/[hash][ext]` */
81
83
  imageAssetFilename?: string | ((pathData: any, assetInfo: any) => string);
82
84
  plugins?: Configuration["plugins"];
83
85
  moduleRules?: RuleSetRule[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next-core/build-next-bricks",
3
- "version": "1.12.0",
3
+ "version": "1.13.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",
@@ -51,5 +51,5 @@
51
51
  "devDependencies": {
52
52
  "@next-core/brick-manifest": "^0.5.0"
53
53
  },
54
- "gitHead": "fbd30ceaf3abd9c99c5dc2085a81413c68e367e0"
54
+ "gitHead": "ade903abfe7858ec69518eed83a91c43b54bdd4a"
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
- { encoding: "utf-8" }
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,12 +218,31 @@ 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
 
187
241
  const outputPath = path.join(packageDir, config.outputPath ?? "dist");
188
242
  const chunksDir = isBricks ? "chunks/" : "";
189
243
 
244
+ const imageAssetFilename = config.imageAssetFilename ?? "images/[hash][ext]";
245
+
190
246
  return {
191
247
  entry: config.entry || {
192
248
  main: "./src/index",
@@ -267,7 +323,7 @@ async function getWebpackConfig(config) {
267
323
  ),
268
324
  type: "asset/resource",
269
325
  generator: {
270
- filename: config.imageAssetFilename ?? "images/[hash][ext][query]",
326
+ filename: imageAssetFilename,
271
327
  },
272
328
  },
273
329
  ...(config.svgRules ??
@@ -275,6 +331,29 @@ async function getWebpackConfig(config) {
275
331
  ? [
276
332
  {
277
333
  test: /\.svg$/i,
334
+ type: "asset/resource",
335
+ // Match `xxx.svg?url`
336
+ resourceQuery: /url/,
337
+ generator: {
338
+ filename: imageAssetFilename,
339
+ },
340
+ },
341
+ {
342
+ test: /\.svg$/i,
343
+ // Exclude issuer of js files
344
+ issuer: {
345
+ not: /\.[jt]sx?$/,
346
+ },
347
+ type: "asset/resource",
348
+ generator: {
349
+ filename: imageAssetFilename,
350
+ },
351
+ },
352
+ {
353
+ test: /\.svg$/i,
354
+ issuer: /\.[jt]sx?$/,
355
+ // Exclude `xxx.svg?url`
356
+ resourceQuery: { not: /url/ },
278
357
  use: getSvgrLoaders(),
279
358
  },
280
359
  ]
@@ -10,7 +10,7 @@ export default function getSvgrLoaders(options = {}) {
10
10
  loader: "@svgr/webpack",
11
11
  options: {
12
12
  babel: false,
13
- icon: true,
13
+ icon: options.icon ?? false,
14
14
  svgoConfig: {
15
15
  plugins: [
16
16
  {
@@ -21,7 +21,7 @@ export default function getSvgrLoaders(options = {}) {
21
21
  removeViewBox: false,
22
22
 
23
23
  convertColors: {
24
- currentColor: options.convertCurrentColor ?? false,
24
+ currentColor: options.convertCurrentColor ?? !!options.icon,
25
25
  },
26
26
  },
27
27
  },