@umijs/bundler-utoopack 4.6.50 → 4.6.52

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/dist/config.d.ts CHANGED
@@ -3,6 +3,10 @@ import type { BundleOptions } from '@utoo/pack';
3
3
  import type { IOpts } from './types';
4
4
  export declare function mergeExtraPostcssPlugins(postcssConfig: any, extraPlugins?: any[]): any;
5
5
  export declare function getProdUtooPackConfig(opts: IOpts): Promise<BundleOptions>;
6
+ export declare function getSSRUtooPackConfig(opts: IOpts & {
7
+ serverBuildPath: string;
8
+ useHash?: boolean;
9
+ }): Promise<BundleOptions>;
6
10
  export declare type IDevOpts = {
7
11
  afterMiddlewares?: any[];
8
12
  beforeMiddlewares?: any[];
package/dist/config.js CHANGED
@@ -31,6 +31,7 @@ var config_exports = {};
31
31
  __export(config_exports, {
32
32
  getDevUtooPackConfig: () => getDevUtooPackConfig,
33
33
  getProdUtooPackConfig: () => getProdUtooPackConfig,
34
+ getSSRUtooPackConfig: () => getSSRUtooPackConfig,
34
35
  mergeExtraPostcssPlugins: () => mergeExtraPostcssPlugins
35
36
  });
36
37
  module.exports = __toCommonJS(config_exports);
@@ -39,15 +40,24 @@ var import_utils = require("@umijs/utils");
39
40
  var import_pack = require("@utoo/pack");
40
41
  var import_fs = __toESM(require("fs"));
41
42
  var import_path = require("path");
42
- function normalizeDefineValue(val) {
43
- if (!import_utils.lodash.isPlainObject(val)) {
44
- return JSON.stringify(val);
45
- } else {
46
- return Object.keys(val).reduce((obj, key) => {
47
- obj[key] = normalizeDefineValue(val[key]);
48
- return obj;
49
- }, {});
43
+ function getUtoopackDefine(opts) {
44
+ const define = Object.fromEntries(
45
+ Object.entries(opts.config.define || {}).map(([key, value]) => {
46
+ return [key, normalizeUtoopackDefineValue(value)];
47
+ })
48
+ );
49
+ if (process.env.SOCKET_SERVER) {
50
+ define["process.env.SOCKET_SERVER"] = JSON.stringify(
51
+ process.env.SOCKET_SERVER
52
+ );
53
+ }
54
+ return define;
55
+ }
56
+ function normalizeUtoopackDefineValue(value) {
57
+ if (typeof value === "string") {
58
+ return JSON.stringify(value);
50
59
  }
60
+ return value;
51
61
  }
52
62
  function getModularizeImports(extraBabelPlugins) {
53
63
  return extraBabelPlugins.filter((p) => /^import$|babel-plugin-import/.test(p[0])).reduce(
@@ -243,6 +253,9 @@ function mergeExtraPostcssPlugins(postcssConfig, extraPlugins = []) {
243
253
  function getUserUtoopackConfig(utoopackConfig = {}) {
244
254
  return import_utils.lodash.omit(utoopackConfig, ["root"]);
245
255
  }
256
+ function getDefaultPersistentCaching() {
257
+ return process.platform !== "win32";
258
+ }
246
259
  async function getProdUtooPackConfig(opts) {
247
260
  var _a;
248
261
  const webpackConfig = await (0, import_bundler_webpack.getConfig)({
@@ -279,17 +292,7 @@ async function getProdUtooPackConfig(opts) {
279
292
  const emotion = extraBabelPlugins.some((p) => {
280
293
  return p === "@emotion" || p === "@emotion/babel-plugin";
281
294
  });
282
- const define = {};
283
- if (opts.config.define) {
284
- for (const key of Object.keys(opts.config.define)) {
285
- define[key] = normalizeDefineValue(opts.config.define[key]);
286
- }
287
- }
288
- if (process.env.SOCKET_SERVER) {
289
- define["process.env.SOCKET_SERVER"] = normalizeDefineValue(
290
- process.env.SOCKET_SERVER
291
- );
292
- }
295
+ const define = getUtoopackDefine(opts);
293
296
  const {
294
297
  publicPath,
295
298
  runtimePublicPath,
@@ -343,6 +346,97 @@ async function getProdUtooPackConfig(opts) {
343
346
  };
344
347
  return utooBundlerOpts;
345
348
  }
349
+ async function getSSRUtooPackConfig(opts) {
350
+ var _a;
351
+ const utooBundlerOpts = await getProdUtooPackConfig({
352
+ ...opts,
353
+ clean: false,
354
+ disableCopy: true
355
+ });
356
+ const entry = Object.entries(opts.entry)[0];
357
+ const entryName = (entry == null ? void 0 : entry[0]) || "umi.server";
358
+ const entryPath = entry == null ? void 0 : entry[1];
359
+ const filename = opts.useHash ? "[name].[contenthash:8].js" : (0, import_path.basename)(opts.serverBuildPath);
360
+ const ssrAssetsLoader = {
361
+ loader: require.resolve("./ssrAssetsLoader"),
362
+ options: {
363
+ cwd: opts.cwd
364
+ }
365
+ };
366
+ const ssrStylesLoader = {
367
+ loader: require.resolve("./ssrStylesLoader"),
368
+ options: {
369
+ cwd: opts.cwd
370
+ }
371
+ };
372
+ const ssrAssetRules = [
373
+ "*.png",
374
+ "*.jpg",
375
+ "*.jpeg",
376
+ "*.gif",
377
+ "*.webp",
378
+ "*.avif",
379
+ "*.ico",
380
+ "*.woff",
381
+ "*.woff2",
382
+ "*.ttf",
383
+ "*.eot",
384
+ "*.mp3",
385
+ "*.mp4"
386
+ ].reduce((memo, key) => {
387
+ memo[key] = {
388
+ loaders: [ssrAssetsLoader],
389
+ as: "*.js"
390
+ };
391
+ return memo;
392
+ }, {});
393
+ const ssrStyleRules = ["*.css", "*.less", "*.sass", "*.scss"].reduce(
394
+ (memo, key) => {
395
+ memo[key] = {
396
+ loaders: [ssrStylesLoader],
397
+ as: "*.js"
398
+ };
399
+ return memo;
400
+ },
401
+ {}
402
+ );
403
+ utooBundlerOpts.config = {
404
+ ...utooBundlerOpts.config,
405
+ entry: [
406
+ {
407
+ name: entryName,
408
+ import: entryPath,
409
+ library: {}
410
+ }
411
+ ],
412
+ output: {
413
+ ...utooBundlerOpts.config.output,
414
+ path: (0, import_path.dirname)(opts.serverBuildPath),
415
+ filename,
416
+ chunkFilename: filename,
417
+ clean: false,
418
+ copy: [],
419
+ publicPath: "/"
420
+ },
421
+ target: "node",
422
+ sourceMaps: false,
423
+ stats: true,
424
+ nodePolyfill: false,
425
+ module: {
426
+ ...utooBundlerOpts.config.module,
427
+ rules: {
428
+ ...(_a = utooBundlerOpts.config.module) == null ? void 0 : _a.rules,
429
+ ...ssrAssetRules,
430
+ ...ssrStyleRules
431
+ }
432
+ },
433
+ optimization: {
434
+ ...utooBundlerOpts.config.optimization,
435
+ minify: false
436
+ }
437
+ };
438
+ return utooBundlerOpts;
439
+ }
346
440
  async function getDevUtooPackConfig(opts) {
347
441
  var _a;
348
442
  let webpackConfig = await (0, import_bundler_webpack.getConfig)({
@@ -379,17 +473,7 @@ async function getDevUtooPackConfig(opts) {
379
473
  const emotion = extraBabelPlugins.some((p) => {
380
474
  return p === "@emotion" || p === "@emotion/babel-plugin";
381
475
  });
382
- const define = {};
383
- if (opts.config.define) {
384
- for (const key of Object.keys(opts.config.define)) {
385
- define[key] = normalizeDefineValue(opts.config.define[key]);
386
- }
387
- }
388
- if (process.env.SOCKET_SERVER) {
389
- define["process.env.SOCKET_SERVER"] = normalizeDefineValue(
390
- process.env.SOCKET_SERVER
391
- );
392
- }
476
+ const define = getUtoopackDefine(opts);
393
477
  const {
394
478
  publicPath,
395
479
  runtimePublicPath,
@@ -406,7 +490,7 @@ async function getDevUtooPackConfig(opts) {
406
490
  ...process.env.SOCKET_SERVER ? {
407
491
  processEnv: {
408
492
  ...utooBundlerOpts.processEnv || {},
409
- "process.env.SOCKET_SERVER": define["process.env.SOCKET_SERVER"]
493
+ "process.env.SOCKET_SERVER": process.env.SOCKET_SERVER
410
494
  }
411
495
  } : {},
412
496
  config: import_utils.lodash.merge(
@@ -441,8 +525,8 @@ async function getDevUtooPackConfig(opts) {
441
525
  emotion
442
526
  },
443
527
  define,
444
- // dev enable persistent cache by default
445
- persistentCaching: true,
528
+ // Windows persistent cache restore is currently unstable in utoopack dev.
529
+ persistentCaching: getDefaultPersistentCaching(),
446
530
  nodePolyfill: true,
447
531
  mdx: !!mdx,
448
532
  externals: getNormalizedExternals(userExternals),
@@ -468,5 +552,6 @@ async function getDevUtooPackConfig(opts) {
468
552
  0 && (module.exports = {
469
553
  getDevUtooPackConfig,
470
554
  getProdUtooPackConfig,
555
+ getSSRUtooPackConfig,
471
556
  mergeExtraPostcssPlugins
472
557
  });
package/dist/index.d.ts CHANGED
@@ -4,4 +4,8 @@ export { findRootDir } from '@utoo/pack';
4
4
  export * from './config';
5
5
  export type { IUtoopackUserConfig } from './types';
6
6
  export declare function build(opts: IOpts): Promise<any>;
7
+ export declare function buildSSR(opts: IOpts & {
8
+ serverBuildPath: string;
9
+ useHash?: boolean;
10
+ }): Promise<any>;
7
11
  export declare function dev(opts: IDevOpts): Promise<any>;
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
33
  build: () => build,
34
+ buildSSR: () => buildSSR,
34
35
  dev: () => dev,
35
36
  findRootDir: () => import_pack.findRootDir
36
37
  });
@@ -106,6 +107,44 @@ async function build(opts) {
106
107
  );
107
108
  return stats;
108
109
  }
110
+ async function buildSSR(opts) {
111
+ var _a;
112
+ const { cwd } = opts;
113
+ const buildStartTime = Date.now();
114
+ const { build: utooPackBuild, findRootDir: findRootDir2 } = require("@utoo/pack");
115
+ const rootDir = getUtoopackRootDir(cwd, opts.config.utoopack, findRootDir2);
116
+ const utooPackConfig = await (0, import_config.getSSRUtooPackConfig)({
117
+ ...opts,
118
+ rootDir
119
+ });
120
+ try {
121
+ await utooPackBuild(utooPackConfig, cwd, rootDir);
122
+ } catch (e) {
123
+ console.error(e.message);
124
+ const err = new Error("Build SSR with utoopack failed.");
125
+ err.stack = null;
126
+ throw err;
127
+ }
128
+ const statsPath = import_path.default.join(
129
+ ((_a = utooPackConfig.config.output) == null ? void 0 : _a.path) || "server",
130
+ "stats.json"
131
+ );
132
+ const stats = JSON.parse(import_fs.default.readFileSync(statsPath, "utf-8"));
133
+ stats.hasErrors = () => false;
134
+ stats.toJson = () => stats;
135
+ stats.toString = () => {
136
+ };
137
+ stats.compilation = {
138
+ ...stats,
139
+ assets: stats.assets.reduce(
140
+ (acc, cur) => Object.assign(acc, { [cur.name]: cur }),
141
+ {}
142
+ )
143
+ };
144
+ const time = Date.now() - buildStartTime;
145
+ stats.time = time;
146
+ return stats;
147
+ }
109
148
  async function dev(opts) {
110
149
  const { cwd, onDevCompileDone } = opts;
111
150
  if (!opts) {
@@ -269,6 +308,7 @@ async function dev(opts) {
269
308
  // Annotate the CommonJS export names for ESM import in node:
270
309
  0 && (module.exports = {
271
310
  build,
311
+ buildSSR,
272
312
  dev,
273
313
  findRootDir,
274
314
  ...require("./config")
@@ -0,0 +1 @@
1
+ export default function ssrAssetsLoader(this: any): string;
@@ -0,0 +1,35 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/ssrAssetsLoader.ts
20
+ var ssrAssetsLoader_exports = {};
21
+ __export(ssrAssetsLoader_exports, {
22
+ default: () => ssrAssetsLoader
23
+ });
24
+ module.exports = __toCommonJS(ssrAssetsLoader_exports);
25
+ var import_utils = require("@umijs/utils");
26
+ function ensureLastSlash(path) {
27
+ return path.endsWith("/") ? path : path + "/";
28
+ }
29
+ function ssrAssetsLoader() {
30
+ var _a;
31
+ const options = ((_a = this.getOptions) == null ? void 0 : _a.call(this)) || {};
32
+ const cwd = (0, import_utils.winPath)(options.cwd || this.rootContext || process.cwd());
33
+ const filename = (0, import_utils.winPath)(this.resourcePath).replace(ensureLastSlash(cwd), "");
34
+ return `export default global.g_getAssets(${JSON.stringify(filename)});`;
35
+ }
@@ -0,0 +1,2 @@
1
+ /// <reference types="node" />
2
+ export default function ssrStylesLoader(this: any, content: string | Buffer): string;
@@ -0,0 +1,54 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/ssrStylesLoader.ts
20
+ var ssrStylesLoader_exports = {};
21
+ __export(ssrStylesLoader_exports, {
22
+ default: () => ssrStylesLoader
23
+ });
24
+ module.exports = __toCommonJS(ssrStylesLoader_exports);
25
+ var import_utils = require("@umijs/utils");
26
+ function ensureLastSlash(path) {
27
+ return path.endsWith("/") ? path : path + "/";
28
+ }
29
+ function hashString(str) {
30
+ let hash = Buffer.from(str).toString("base64").replace(/=/g, "");
31
+ hash = hash.substring(hash.length - 5);
32
+ return hash;
33
+ }
34
+ function getClassNames(code) {
35
+ const classNames = /* @__PURE__ */ new Set();
36
+ const regexp = /\.([_a-zA-Z][\w-]*)/g;
37
+ let match;
38
+ while (match = regexp.exec(code)) {
39
+ classNames.add(match[1]);
40
+ }
41
+ return Array.from(classNames);
42
+ }
43
+ function ssrStylesLoader(content) {
44
+ var _a;
45
+ const options = ((_a = this.getOptions) == null ? void 0 : _a.call(this)) || {};
46
+ const cwd = (0, import_utils.winPath)(options.cwd || this.rootContext || process.cwd());
47
+ const filename = (0, import_utils.winPath)(this.resourcePath).replace(ensureLastSlash(cwd), "");
48
+ const code = Buffer.isBuffer(content) ? content.toString() : content;
49
+ const cssModuleObject = getClassNames(code).sort().reduce((memo, key) => {
50
+ memo[key] = `${key}___${hashString(`${filename}@${key}`)}`;
51
+ return memo;
52
+ }, {});
53
+ return `export default ${JSON.stringify(cssModuleObject)};`;
54
+ }
package/dist/util.js CHANGED
@@ -25,14 +25,27 @@ __export(util_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(util_exports);
27
27
  var import_utils = require("@umijs/utils");
28
+ function cleanVersionRange(version) {
29
+ return version == null ? void 0 : version.replace(/^[^\d]*/, "");
30
+ }
28
31
  function getPackVersion(packVersion) {
29
- var _a, _b;
32
+ var _a;
30
33
  if (packVersion) {
31
34
  return packVersion;
32
35
  }
36
+ try {
37
+ const pkgPath = require.resolve("@utoo/pack/package.json", {
38
+ paths: [__dirname]
39
+ });
40
+ const pkg = require(pkgPath);
41
+ if (pkg.version) {
42
+ return pkg.version;
43
+ }
44
+ } catch {
45
+ }
33
46
  try {
34
47
  const pkg = require("../package.json");
35
- return (_b = (_a = pkg.dependencies) == null ? void 0 : _a["@utoo/pack"]) == null ? void 0 : _b.replace(/^[^\d]*/, "");
48
+ return cleanVersionRange((_a = pkg.dependencies) == null ? void 0 : _a["@utoo/pack"]);
36
49
  } catch {
37
50
  return void 0;
38
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/bundler-utoopack",
3
- "version": "4.6.50",
3
+ "version": "4.6.52",
4
4
  "description": "@umijs/bundler-utoopack",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,7 @@
8
8
  "dist"
9
9
  ],
10
10
  "dependencies": {
11
- "@utoo/pack": "1.4.2",
11
+ "@utoo/pack": "1.4.4",
12
12
  "compression": "^1.7.4",
13
13
  "connect-history-api-fallback": "^2.0.0",
14
14
  "cors": "^2.8.5",
@@ -20,8 +20,8 @@
20
20
  "resolve-url-loader": "5.0.0",
21
21
  "sass": "1.54.0",
22
22
  "sass-loader": "13.2.0",
23
- "@umijs/bundler-utils": "4.6.50",
24
- "@umijs/bundler-webpack": "4.6.50"
23
+ "@umijs/bundler-utils": "4.6.52",
24
+ "@umijs/bundler-webpack": "4.6.52"
25
25
  },
26
26
  "devDependencies": {
27
27
  "father": "4.1.5"