@next-core/build-next-bricks 1.2.0 → 1.3.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.
@@ -4,30 +4,44 @@ import { existsSync } from "node:fs";
4
4
  import build from "../src/build.js";
5
5
  import scanBricks from "../src/scanBricks.js";
6
6
 
7
+ /**
8
+ * @typedef {T | Array<T>} MaybeArray<T>
9
+ * @template {string} T
10
+ */
11
+
12
+ /** @typedef {import("@next-core/build-next-bricks").BuildNextBricksConfig} BuildNextBricksConfig */
13
+
7
14
  try {
8
15
  const startTime = Date.now();
9
16
 
10
17
  const packageDir = process.cwd();
11
18
  const configJs = path.join(packageDir, "build.config.js");
12
- /** @type {import("@next-core/build-next-bricks").BuildNextBricksConfig} */
13
- let config = {};
19
+ /** @type {MaybeArray<BuildNextBricksConfig>} */
20
+ let rawConfig = {};
14
21
  if (existsSync(configJs)) {
15
- config = (await import(configJs)).default;
22
+ rawConfig = (await import(configJs)).default;
16
23
  }
17
24
 
18
- if (!config.type || config.type === "bricks") {
19
- const scanBricksStartAt = performance.now();
20
- Object.assign(config, await scanBricks(packageDir));
21
- const scanBricksCost = Math.round(performance.now() - scanBricksStartAt);
22
- console.log(
23
- "Scan bricks done in",
24
- scanBricksCost < 1000
25
- ? `${scanBricksCost}ms`
26
- : `${(scanBricksCost / 1000).toFixed(2)}s`
27
- );
25
+ /** @type {Array<BuildNextBricksConfig>} */
26
+ const configList = [].concat(rawConfig);
27
+
28
+ for (const config of configList) {
29
+ if (!config.type || config.type === "bricks") {
30
+ const scanBricksStartAt = performance.now();
31
+ Object.assign(config, await scanBricks(packageDir));
32
+ const scanBricksCost = Math.round(performance.now() - scanBricksStartAt);
33
+ console.log(
34
+ "Scan bricks done in",
35
+ scanBricksCost < 1000
36
+ ? `${scanBricksCost}ms`
37
+ : `${(scanBricksCost / 1000).toFixed(2)}s`
38
+ );
39
+ }
28
40
  }
29
41
 
30
- const compiler = await build(config);
42
+ const compiler = await build(
43
+ configList.length === 1 ? configList[0] : configList
44
+ );
31
45
 
32
46
  const watch = process.argv.includes("--watch");
33
47
 
package/index.d.ts CHANGED
@@ -69,6 +69,9 @@ export interface BuildNextBricksConfig {
69
69
  type?: "bricks" | "container" | "brick-playground";
70
70
  mode?: "development" | "production";
71
71
  entry?: Record<string, string>;
72
+ /** Defaults to "dist" */
73
+ outputPath?: string;
74
+ devOnlyOutputPublicPath?: string;
72
75
  extractCss?: boolean;
73
76
  /** Treat svg as React component instead of asset */
74
77
  svgAsReactComponent?: boolean;
@@ -81,5 +84,5 @@ export interface BuildNextBricksConfig {
81
84
  exposes?: ConstructorParameters<typeof container.ModuleFederationPlugin>;
82
85
  dependencies?: Record<string, string[]>;
83
86
  optimization?: Configuration["optimization"];
84
- moduleFederationShared?: SharedObject;
87
+ moduleFederationShared?: SharedObject | false;
85
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next-core/build-next-bricks",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Build next bricks",
5
5
  "homepage": "https://github.com/easyops-cn/next-core/tree/master/packages/build-next-bricks",
6
6
  "license": "GPL-3.0",
@@ -30,21 +30,21 @@
30
30
  "node": ">=16"
31
31
  },
32
32
  "dependencies": {
33
- "@babel/parser": "^7.21.2",
34
- "@babel/traverse": "^7.21.2",
35
- "@svgr/webpack": "^6.5.1",
33
+ "@babel/parser": "^7.21.4",
34
+ "@babel/traverse": "^7.21.4",
35
+ "@svgr/webpack": "^7.0.0",
36
36
  "babel-loader": "^9.1.2",
37
37
  "css-loader": "^6.7.3",
38
- "cssnano": "^5.1.15",
39
- "cssnano-preset-lite": "^2.1.3",
38
+ "cssnano": "^6.0.0",
39
+ "cssnano-preset-lite": "^3.0.0",
40
40
  "lodash": "^4.17.21",
41
41
  "mini-css-extract-plugin": "^2.7.5",
42
42
  "postcss": "^8.4.21",
43
- "postcss-loader": "^7.1.0",
44
- "postcss-preset-env": "^8.0.1",
43
+ "postcss-loader": "^7.2.4",
44
+ "postcss-preset-env": "^8.3.0",
45
45
  "style-loader": "^3.3.1",
46
- "typescript": "^5.0.2",
47
- "webpack": "^5.76.2"
46
+ "typescript": "^5.0.3",
47
+ "webpack": "^5.78.0"
48
48
  },
49
- "gitHead": "5f7e81c60f23e6b1ad116bd07e943ff743900766"
49
+ "gitHead": "9e99998974ba369577d5ff6c01cd2aefb451a432"
50
50
  }
package/src/build.js CHANGED
@@ -10,6 +10,13 @@ import EmitBricksJsonPlugin from "./EmitBricksJsonPlugin.js";
10
10
  import getCamelPackageName from "./getCamelPackageName.js";
11
11
  import getSvgrLoaders from "./getSvgrLoaders.js";
12
12
 
13
+ /**
14
+ * @typedef {T | Array<T>} MaybeArray<T>
15
+ * @template {string} T
16
+ */
17
+
18
+ /** @typedef {import("@next-core/build-next-bricks").BuildNextBricksConfig} BuildNextBricksConfig */
19
+
13
20
  const require = createRequire(import.meta.url);
14
21
 
15
22
  const { SourceMapDevToolPlugin, IgnorePlugin, ContextReplacementPlugin } =
@@ -48,9 +55,10 @@ const getCssLoaders = (cssOptions) => [
48
55
  ];
49
56
 
50
57
  /**
51
- * @param {import("@next-core/build-next-bricks").BuildNextBricksConfig} config
58
+ * @param {BuildNextBricksConfig} config
59
+ * @returns {import("webpack").Configuration}
52
60
  */
53
- export default async function build(config) {
61
+ async function getWebpackConfig(config) {
54
62
  const packageDir = process.cwd();
55
63
  // const isContainer = config.type === "container";
56
64
  const isBricks = !config.type || config.type === "bricks";
@@ -96,49 +104,55 @@ export default async function build(config) {
96
104
  ...sharedSingletonPackages,
97
105
  ];
98
106
 
99
- /** @type {import("@next-core/build-next-bricks").BuildNextBricksConfig["moduleFederationShared"]} */
100
- const shared = Object.fromEntries(
101
- (
102
- await Promise.all(
103
- sharedPackages.map(async (dep) => {
104
- /** @type {string} */
105
- let depPackageJsonPath;
106
- const depPkgName = dep
107
- .split("/")
108
- .slice(0, dep.startsWith("@") ? 2 : 1)
109
- .join("/");
110
- try {
111
- depPackageJsonPath = require.resolve(`${depPkgName}/package.json`, {
112
- paths: [packageDir],
113
- });
114
- } catch (e) {
115
- console.error(`Shared package not found: "${dep}"`);
116
- return;
117
- }
118
- const depPackageJsonFile = await readFile(depPackageJsonPath, {
119
- encoding: "utf-8",
120
- });
121
- const depPackageJson = JSON.parse(depPackageJsonFile);
122
- const customized = config.moduleFederationShared?.[dep];
123
- if (typeof customized === "string") {
124
- return;
125
- }
126
- return [
127
- dep,
128
- {
129
- singleton: sharedSingletonPackages.includes(dep),
130
- version: depPackageJson.version,
131
- requiredVersion:
132
- packageJson.peerDependencies?.[depPkgName] ??
133
- packageJson.devDependencies?.[depPkgName] ??
134
- packageJson.dependencies?.[depPkgName],
135
- ...customized,
136
- },
137
- ];
138
- })
139
- )
140
- ).filter(Boolean)
141
- );
107
+ /** @type {BuildNextBricksConfig["moduleFederationShared"]} */
108
+ const shared =
109
+ config.moduleFederationShared === false
110
+ ? undefined
111
+ : Object.fromEntries(
112
+ (
113
+ await Promise.all(
114
+ sharedPackages.map(async (dep) => {
115
+ /** @type {string} */
116
+ let depPackageJsonPath;
117
+ const depPkgName = dep
118
+ .split("/")
119
+ .slice(0, dep.startsWith("@") ? 2 : 1)
120
+ .join("/");
121
+ try {
122
+ depPackageJsonPath = require.resolve(
123
+ `${depPkgName}/package.json`,
124
+ {
125
+ paths: [packageDir],
126
+ }
127
+ );
128
+ } catch (e) {
129
+ console.error(`Shared package not found: "${dep}"`);
130
+ return;
131
+ }
132
+ const depPackageJsonFile = await readFile(depPackageJsonPath, {
133
+ encoding: "utf-8",
134
+ });
135
+ const depPackageJson = JSON.parse(depPackageJsonFile);
136
+ const customized = config.moduleFederationShared?.[dep];
137
+ if (typeof customized === "string") {
138
+ return;
139
+ }
140
+ return [
141
+ dep,
142
+ {
143
+ singleton: sharedSingletonPackages.includes(dep),
144
+ version: depPackageJson.version,
145
+ requiredVersion:
146
+ packageJson.peerDependencies?.[depPkgName] ??
147
+ packageJson.devDependencies?.[depPkgName] ??
148
+ packageJson.dependencies?.[depPkgName],
149
+ ...customized,
150
+ },
151
+ ];
152
+ })
153
+ )
154
+ ).filter(Boolean)
155
+ );
142
156
 
143
157
  // console.log(packageName, "shared:", shared);
144
158
 
@@ -169,27 +183,24 @@ export default async function build(config) {
169
183
  // };
170
184
  // }
171
185
 
172
- const outputPath = path.join(packageDir, "dist");
186
+ const outputPath = path.join(packageDir, config.outputPath ?? "dist");
173
187
  const chunksDir = isBricks ? "chunks/" : "";
174
188
 
175
- return webpack({
189
+ return {
176
190
  entry: config.entry || {
177
191
  main: "./src/index",
178
192
  },
179
193
  mode,
180
- devServer: {
181
- static: {
182
- directory: outputPath,
183
- },
184
- port: 3001,
185
- },
186
194
  output: {
187
195
  path: outputPath,
188
196
  filename: `${chunksDir}[name].${
189
197
  mode === "development" ? "bundle" : "[contenthash]"
190
198
  }.js`,
191
199
  // filename: "[name].bundle.js",
192
- publicPath: "auto",
200
+ publicPath:
201
+ mode === "development"
202
+ ? config.devOnlyOutputPublicPath ?? "auto"
203
+ : "auto",
193
204
  hashDigestLength: 8,
194
205
  chunkFilename: `${chunksDir}[name]${
195
206
  mode === "development" ? "" : ".[contenthash]"
@@ -313,26 +324,30 @@ export default async function build(config) {
313
324
  ],
314
325
  }),
315
326
 
316
- new ModuleFederationPlugin({
317
- name: libName,
318
- shared: {
319
- ...config.moduleFederationShared,
320
- ...shared,
321
- },
322
- ...(isBricks
323
- ? {
324
- library: { type: "window", name: libName },
325
- filename:
326
- mode === "development"
327
- ? "index.bundle.js"
328
- : "index.[contenthash].js",
329
- exposes: {
330
- ...config.exposes,
331
- ...extraExposes,
327
+ ...(config.moduleFederationShared !== false
328
+ ? [
329
+ new ModuleFederationPlugin({
330
+ name: libName,
331
+ shared: {
332
+ ...config.moduleFederationShared,
333
+ ...shared,
332
334
  },
333
- }
334
- : null),
335
- }),
335
+ ...(isBricks
336
+ ? {
337
+ library: { type: "window", name: libName },
338
+ filename:
339
+ mode === "development"
340
+ ? "index.bundle.js"
341
+ : "index.[contenthash].js",
342
+ exposes: {
343
+ ...config.exposes,
344
+ ...extraExposes,
345
+ },
346
+ }
347
+ : null),
348
+ }),
349
+ ]
350
+ : []),
336
351
 
337
352
  ...(config.extractCss
338
353
  ? [
@@ -370,5 +385,20 @@ export default async function build(config) {
370
385
 
371
386
  ...(config.plugins || []),
372
387
  ],
373
- });
388
+ };
374
389
  }
390
+
391
+ /**
392
+ * @type {{
393
+ * (config: BuildNextBricksConfig): import("webpack").Compiler;
394
+ * (config: BuildNextBricksConfig[]): import("webpack").MultiCompiler;
395
+ * }}
396
+ */
397
+ const build = async (config) =>
398
+ webpack(
399
+ await (Array.isArray(config)
400
+ ? Promise.all(config.map((conf) => getWebpackConfig(conf)))
401
+ : getWebpackConfig(config))
402
+ );
403
+
404
+ export default build;