@umijs/bundler-utoopack 4.6.56 → 4.6.58

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
@@ -1,9 +1,10 @@
1
1
  import type { IOpts as IConfigOpts } from '@umijs/bundler-webpack';
2
2
  import type { BundleOptions } from '@utoo/pack';
3
3
  import type { IOpts } from './types';
4
+ export declare function normalizeUtoopackPath(path: string): string;
4
5
  export declare function mergeExtraPostcssPlugins(postcssConfig: any, extraPlugins?: any[]): any;
5
- export declare function getProdUtooPackConfig(opts: IOpts): Promise<BundleOptions>;
6
- export declare function getSSRUtooPackConfig(opts: IOpts & {
6
+ export declare function getProdUtooPackConfig(rawOpts: IOpts): Promise<BundleOptions>;
7
+ export declare function getSSRUtooPackConfig(rawOpts: IOpts & {
7
8
  serverBuildPath: string;
8
9
  useHash?: boolean;
9
10
  isDev?: boolean;
@@ -35,5 +36,5 @@ export declare type IDevOpts = {
35
36
  onBeforeMiddleware?: Function;
36
37
  disableCopy?: boolean;
37
38
  clean?: boolean;
38
- } & Pick<IConfigOpts, 'cache' | 'pkg'>;
39
- export declare function getDevUtooPackConfig(opts: IDevOpts): Promise<BundleOptions>;
39
+ } & Pick<IConfigOpts, 'cache' | 'pkg' | 'staticPathPrefix'>;
40
+ export declare function getDevUtooPackConfig(rawOpts: IDevOpts): Promise<BundleOptions>;
package/dist/config.js CHANGED
@@ -32,7 +32,8 @@ __export(config_exports, {
32
32
  getDevUtooPackConfig: () => getDevUtooPackConfig,
33
33
  getProdUtooPackConfig: () => getProdUtooPackConfig,
34
34
  getSSRUtooPackConfig: () => getSSRUtooPackConfig,
35
- mergeExtraPostcssPlugins: () => mergeExtraPostcssPlugins
35
+ mergeExtraPostcssPlugins: () => mergeExtraPostcssPlugins,
36
+ normalizeUtoopackPath: () => normalizeUtoopackPath
36
37
  });
37
38
  module.exports = __toCommonJS(config_exports);
38
39
  var import_bundler_webpack = require("@umijs/bundler-webpack");
@@ -40,6 +41,11 @@ var import_utils = require("@umijs/utils");
40
41
  var import_pack = require("@utoo/pack");
41
42
  var import_fs = __toESM(require("fs"));
42
43
  var import_path = require("path");
44
+ var DEFAULT_STATIC_PATH_PREFIX = "static/";
45
+ function getAssetModuleFilename(staticPathPrefix) {
46
+ const prefix = staticPathPrefix !== void 0 ? staticPathPrefix : DEFAULT_STATIC_PATH_PREFIX;
47
+ return `${prefix}[name].[contenthash:8]`;
48
+ }
43
49
  function getUtoopackDefine(opts) {
44
50
  const define = Object.fromEntries(
45
51
  Object.entries(opts.config.define || {}).map(([key, value]) => {
@@ -195,8 +201,42 @@ function getExtraBabelModuleRules(opts) {
195
201
  }
196
202
  };
197
203
  }
204
+ function normalizeUtoopackPath(path) {
205
+ return path.replace(/\\/g, "/");
206
+ }
207
+ function normalizeUtoopackEntry(entry) {
208
+ if (typeof entry === "string") {
209
+ return normalizeUtoopackPath(entry);
210
+ }
211
+ if (Array.isArray(entry)) {
212
+ return entry.map(normalizeUtoopackEntry);
213
+ }
214
+ if (entry && typeof entry === "object") {
215
+ return Object.fromEntries(
216
+ Object.entries(entry).map(([key, value]) => [
217
+ key,
218
+ normalizeUtoopackEntry(value)
219
+ ])
220
+ );
221
+ }
222
+ return entry;
223
+ }
224
+ function normalizeUtoopackOpts(opts) {
225
+ return {
226
+ ...opts,
227
+ cwd: normalizeUtoopackPath(opts.cwd),
228
+ rootDir: normalizeUtoopackPath(opts.rootDir),
229
+ entry: normalizeUtoopackEntry(opts.entry)
230
+ };
231
+ }
198
232
  function getNormalizedAlias(alias, rootDir) {
199
- const newAlias = { ...alias };
233
+ const newAlias = Object.fromEntries(
234
+ Object.entries(alias || {}).map(([key, value]) => [
235
+ normalizeUtoopackPath(key),
236
+ normalizeUtoopackPath(value)
237
+ ])
238
+ );
239
+ const normalizedRootDir = normalizeUtoopackPath(rootDir);
200
240
  for (const [key, value] of Object.entries(newAlias)) {
201
241
  if (key.endsWith("/*") || value.endsWith("/*") || key.endsWith("/") || value.endsWith("/") || key.endsWith("$")) {
202
242
  continue;
@@ -204,7 +244,9 @@ function getNormalizedAlias(alias, rootDir) {
204
244
  const ext = (0, import_path.extname)(value);
205
245
  if (ext) {
206
246
  let isDirectory = false;
207
- const candidates = [.../* @__PURE__ */ new Set([value, (0, import_path.resolve)(rootDir, value)])];
247
+ const candidates = [
248
+ .../* @__PURE__ */ new Set([value, (0, import_path.resolve)(normalizedRootDir, value)])
249
+ ];
208
250
  for (const candidate of candidates) {
209
251
  try {
210
252
  const stat = import_fs.default.statSync(candidate);
@@ -219,40 +261,116 @@ function getNormalizedAlias(alias, rootDir) {
219
261
  }
220
262
  newAlias[`${key}/*`] = `${value}/*`;
221
263
  }
222
- newAlias[`${rootDir}/*`] = `${rootDir}/*`;
264
+ newAlias[`${normalizedRootDir}/*`] = `${normalizedRootDir}/*`;
223
265
  return newAlias;
224
266
  }
267
+ function normalizeExternalValue(v) {
268
+ if (Array.isArray(v)) {
269
+ const [url, ...members] = v;
270
+ const scriptPrefix = /^script\s+/.exec(url);
271
+ const script = scriptPrefix ? url.slice(scriptPrefix[0].length) : url;
272
+ if (scriptPrefix) {
273
+ return {
274
+ // ['antd', 'Button'] => `antd.Button`
275
+ root: members.join("."),
276
+ type: "script",
277
+ // `script https://example.com/lib/script.js` => `https://example.com/lib/script.js`
278
+ script
279
+ };
280
+ } else if (url === "promise" && typeof members[0] === "string") {
281
+ return `promise ${members[0]}`;
282
+ } else {
283
+ return {
284
+ root: members.join("."),
285
+ script
286
+ };
287
+ }
288
+ } else if (typeof v === "string") {
289
+ return v.replace(/^window(\s+|\.)/, "");
290
+ }
291
+ }
292
+ function toWildcardExternalConfig(value) {
293
+ const normalized = normalizeExternalValue(value);
294
+ if (typeof normalized === "string") {
295
+ const externalType = /^(commonjs|esm|promise)\s+/.exec(normalized);
296
+ if (externalType) {
297
+ return {
298
+ root: normalized.slice(externalType[0].length),
299
+ type: externalType[1]
300
+ };
301
+ }
302
+ return {
303
+ root: normalized
304
+ };
305
+ }
306
+ }
307
+ function getExternalSubPathGlob(externalKey) {
308
+ if (!externalKey.includes("*")) return;
309
+ const parts = externalKey.split("/");
310
+ const packageName = externalKey.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
311
+ const subPathGlob = externalKey.slice(packageName.length);
312
+ if (!packageName || !subPathGlob.startsWith("/")) return;
313
+ return {
314
+ packageName,
315
+ subPathGlob
316
+ };
317
+ }
318
+ function escapeRegexChar(char) {
319
+ return /[|\\{}()[\]^$+?.]/.test(char) ? `\\${char}` : char;
320
+ }
321
+ function globToSubPathRegex(glob) {
322
+ let regex = "^";
323
+ for (const char of glob) {
324
+ if (char === "*") {
325
+ regex += ".+";
326
+ } else if (char === "/") {
327
+ regex += "\\/";
328
+ } else {
329
+ regex += escapeRegexChar(char);
330
+ }
331
+ }
332
+ return `/${regex}$/`;
333
+ }
334
+ function addWildcardSubPath(config, subPathGlob) {
335
+ var _a;
336
+ return {
337
+ ...config,
338
+ subPath: {
339
+ ...config.subPath || {},
340
+ rules: [
341
+ ...((_a = config.subPath) == null ? void 0 : _a.rules) || [],
342
+ {
343
+ regex: globToSubPathRegex(subPathGlob),
344
+ target: ""
345
+ }
346
+ ]
347
+ }
348
+ };
349
+ }
225
350
  function getNormalizedExternals(externals) {
226
- return Object.entries(externals || {}).reduce(
227
- (ret, [k, v]) => {
228
- if (Array.isArray(v)) {
229
- const [url, ...members] = v;
230
- const scriptPrefix = /^script\s+/.exec(url);
231
- const script = scriptPrefix ? url.slice(scriptPrefix[0].length) : url;
232
- if (scriptPrefix) {
233
- ret[k] = {
234
- // ['antd', 'Button'] => `antd.Button`
235
- root: members.join("."),
236
- type: "script",
237
- // `script https://example.com/lib/script.js` => `https://example.com/lib/script.js`
238
- script
239
- };
240
- } else if (url === "promise" && typeof members[0] === "string") {
241
- ret[k] = `promise ${members[0]}`;
242
- } else {
243
- ret[k] = {
244
- root: members.join("."),
245
- script
246
- };
351
+ const ret = Object.entries(externals || {}).reduce(
352
+ (memo, [k, v]) => {
353
+ if (!getExternalSubPathGlob(k)) {
354
+ const normalized = normalizeExternalValue(v);
355
+ if (normalized !== void 0) {
356
+ memo[k] = normalized;
247
357
  }
248
- } else if (typeof v === "string") {
249
- ret[k] = v.replace(/^window(\s+|\.)/, "");
250
- } else {
251
358
  }
252
- return ret;
359
+ return memo;
253
360
  },
254
361
  {}
255
362
  );
363
+ Object.entries(externals || {}).forEach(([k, v]) => {
364
+ const externalSubPathGlob = getExternalSubPathGlob(k);
365
+ if (!externalSubPathGlob) return;
366
+ const normalized = toWildcardExternalConfig(v);
367
+ if (!normalized) return;
368
+ ret[externalSubPathGlob.packageName] = addWildcardSubPath(
369
+ ret[externalSubPathGlob.packageName] && typeof ret[externalSubPathGlob.packageName] === "object" ? ret[externalSubPathGlob.packageName] : normalized,
370
+ externalSubPathGlob.subPathGlob
371
+ );
372
+ });
373
+ return ret;
256
374
  }
257
375
  function getSvgModuleRules(opts) {
258
376
  const { svgr, svgo = {} } = opts;
@@ -353,8 +471,9 @@ function getUserUtoopackConfig(utoopackConfig = {}) {
353
471
  function getDefaultPersistentCaching() {
354
472
  return process.platform !== "win32";
355
473
  }
356
- async function getProdUtooPackConfig(opts) {
474
+ async function getProdUtooPackConfig(rawOpts) {
357
475
  var _a;
476
+ const opts = normalizeUtoopackOpts(rawOpts);
358
477
  const webpackConfig = await (0, import_bundler_webpack.getConfig)({
359
478
  cwd: opts.cwd,
360
479
  rootDir: opts.rootDir,
@@ -374,6 +493,7 @@ async function getProdUtooPackConfig(opts) {
374
493
  extraBabelIncludes: opts.config.extraBabelIncludes,
375
494
  chainWebpack: opts.chainWebpack,
376
495
  modifyWebpackConfig: opts.modifyWebpackConfig,
496
+ staticPathPrefix: opts.staticPathPrefix,
377
497
  pkg: opts.pkg,
378
498
  disableCopy: opts.disableCopy
379
499
  });
@@ -410,6 +530,7 @@ async function getProdUtooPackConfig(opts) {
410
530
  output: {
411
531
  clean: opts.clean,
412
532
  publicPath: runtimePublicPath ? "runtime" : publicPath || "/",
533
+ assetModuleFilename: getAssetModuleFilename(opts.staticPathPrefix),
413
534
  ...opts.disableCopy ? { copy: [] } : { copy: ["public"].concat(copy) }
414
535
  },
415
536
  optimization: {
@@ -444,7 +565,8 @@ async function getProdUtooPackConfig(opts) {
444
565
  };
445
566
  return utooBundlerOpts;
446
567
  }
447
- async function getSSRUtooPackConfig(opts) {
568
+ async function getSSRUtooPackConfig(rawOpts) {
569
+ const opts = normalizeUtoopackOpts(rawOpts);
448
570
  const utooBundlerOpts = await getProdUtooPackConfig({
449
571
  ...opts,
450
572
  clean: false,
@@ -483,8 +605,9 @@ async function getSSRUtooPackConfig(opts) {
483
605
  };
484
606
  return utooBundlerOpts;
485
607
  }
486
- async function getDevUtooPackConfig(opts) {
608
+ async function getDevUtooPackConfig(rawOpts) {
487
609
  var _a;
610
+ const opts = normalizeUtoopackOpts(rawOpts);
488
611
  let webpackConfig = await (0, import_bundler_webpack.getConfig)({
489
612
  cwd: opts.cwd,
490
613
  rootDir: opts.rootDir,
@@ -503,6 +626,7 @@ async function getDevUtooPackConfig(opts) {
503
626
  extraBabelIncludes: opts.config.extraBabelIncludes,
504
627
  chainWebpack: opts.chainWebpack,
505
628
  modifyWebpackConfig: opts.modifyWebpackConfig,
629
+ staticPathPrefix: opts.staticPathPrefix,
506
630
  // TO avoild bundler webpack add extra entry.
507
631
  hmr: false,
508
632
  analyze: process.env.ANALYZE
@@ -545,6 +669,7 @@ async function getDevUtooPackConfig(opts) {
545
669
  output: {
546
670
  clean: opts.clean === void 0 ? true : opts.clean,
547
671
  publicPath: runtimePublicPath ? "runtime" : publicPath || "/",
672
+ assetModuleFilename: getAssetModuleFilename(opts.staticPathPrefix),
548
673
  ...opts.disableCopy ? { copy: [] } : { copy: ["public"].concat(copy) }
549
674
  },
550
675
  resolve: {
@@ -601,5 +726,6 @@ async function getDevUtooPackConfig(opts) {
601
726
  getDevUtooPackConfig,
602
727
  getProdUtooPackConfig,
603
728
  getSSRUtooPackConfig,
604
- mergeExtraPostcssPlugins
729
+ mergeExtraPostcssPlugins,
730
+ normalizeUtoopackPath
605
731
  });
package/dist/index.js CHANGED
@@ -48,23 +48,32 @@ var import_util = require("./util");
48
48
  var import_pack = require("@utoo/pack");
49
49
  __reExport(src_exports, require("./config"), module.exports);
50
50
  function getUtoopackRootDir(cwd, utoopackConfig, findRootDir2) {
51
+ const normalizedCwd = (0, import_config.normalizeUtoopackPath)(cwd);
51
52
  if (typeof (utoopackConfig == null ? void 0 : utoopackConfig.root) === "string") {
52
- return import_path.default.resolve(cwd, utoopackConfig.root);
53
+ return (0, import_config.normalizeUtoopackPath)(
54
+ import_path.default.resolve(normalizedCwd, utoopackConfig.root)
55
+ );
53
56
  }
54
- return findRootDir2(cwd);
57
+ return (0, import_config.normalizeUtoopackPath)(findRootDir2(normalizedCwd));
55
58
  }
56
59
  async function build(opts) {
57
60
  var _a, _b, _c;
58
61
  const { cwd, onBuildComplete } = opts;
62
+ const normalizedCwd = (0, import_config.normalizeUtoopackPath)(cwd);
59
63
  const buildStartTime = Date.now();
60
64
  const { build: utooPackBuild, findRootDir: findRootDir2 } = require("@utoo/pack");
61
- const rootDir = getUtoopackRootDir(cwd, opts.config.utoopack, findRootDir2);
65
+ const rootDir = getUtoopackRootDir(
66
+ normalizedCwd,
67
+ opts.config.utoopack,
68
+ findRootDir2
69
+ );
62
70
  const utooPackConfig = await (0, import_config.getProdUtooPackConfig)({
63
71
  ...opts,
72
+ cwd: normalizedCwd,
64
73
  rootDir
65
74
  });
66
75
  try {
67
- await utooPackBuild(utooPackConfig, cwd, rootDir);
76
+ await utooPackBuild(utooPackConfig, normalizedCwd, rootDir);
68
77
  } catch (e) {
69
78
  console.error(e.message);
70
79
  const err = new Error("Build with utoopack failed.");
@@ -94,14 +103,14 @@ async function build(opts) {
94
103
  isFirstCompile: true
95
104
  }));
96
105
  const absOutputPath = import_path.default.resolve(
97
- cwd,
106
+ normalizedCwd,
98
107
  ((_b = utooPackConfig.config.output) == null ? void 0 : _b.path) || "dist"
99
108
  );
100
109
  console.log(
101
110
  (0, import_util.getBuildBanner)({
102
111
  packVersion: process.env.UTOOPACK_VERSION,
103
112
  duration: time,
104
- outputPath: import_path.default.relative(cwd, absOutputPath) || ".",
113
+ outputPath: import_path.default.relative(normalizedCwd, absOutputPath) || ".",
105
114
  assetCount: (_c = stats.assets) == null ? void 0 : _c.length
106
115
  })
107
116
  );
@@ -110,15 +119,21 @@ async function build(opts) {
110
119
  async function buildSSR(opts) {
111
120
  var _a;
112
121
  const { cwd } = opts;
122
+ const normalizedCwd = (0, import_config.normalizeUtoopackPath)(cwd);
113
123
  const buildStartTime = Date.now();
114
124
  const { build: utooPackBuild, findRootDir: findRootDir2 } = require("@utoo/pack");
115
- const rootDir = getUtoopackRootDir(cwd, opts.config.utoopack, findRootDir2);
125
+ const rootDir = getUtoopackRootDir(
126
+ normalizedCwd,
127
+ opts.config.utoopack,
128
+ findRootDir2
129
+ );
116
130
  const utooPackConfig = await (0, import_config.getSSRUtooPackConfig)({
117
131
  ...opts,
132
+ cwd: normalizedCwd,
118
133
  rootDir
119
134
  });
120
135
  try {
121
- await utooPackBuild(utooPackConfig, cwd, rootDir);
136
+ await utooPackBuild(utooPackConfig, normalizedCwd, rootDir);
122
137
  } catch (e) {
123
138
  console.error(e.message);
124
139
  const err = new Error("Build SSR with utoopack failed.");
@@ -147,15 +162,21 @@ async function buildSSR(opts) {
147
162
  }
148
163
  async function dev(opts) {
149
164
  const { cwd, onDevCompileDone } = opts;
165
+ const normalizedCwd = (0, import_config.normalizeUtoopackPath)(cwd);
150
166
  if (!opts) {
151
167
  throw new Error("opts should be supplied");
152
168
  }
153
169
  const devStartTime = Date.now();
154
170
  const protocol = opts.config.https ? "https:" : "http:";
155
171
  const { findRootDir: findRootDir2, serve: utooPackServe } = require("@utoo/pack");
156
- const rootDir = getUtoopackRootDir(cwd, opts.config.utoopack, findRootDir2);
172
+ const rootDir = getUtoopackRootDir(
173
+ normalizedCwd,
174
+ opts.config.utoopack,
175
+ findRootDir2
176
+ );
157
177
  const utooPackConfig = await (0, import_config.getDevUtooPackConfig)({
158
178
  ...opts,
179
+ cwd: normalizedCwd,
159
180
  rootDir
160
181
  });
161
182
  const app = (0, import_express.default)();
@@ -279,7 +300,7 @@ async function dev(opts) {
279
300
  try {
280
301
  await Promise.all([
281
302
  serverReady,
282
- utooPackServe(utooPackConfig, cwd, rootDir, {
303
+ utooPackServe(utooPackConfig, normalizedCwd, rootDir, {
283
304
  port: utooServePort,
284
305
  hostname: "127.0.0.1",
285
306
  logServerInfo: false
package/dist/types.d.ts CHANGED
@@ -20,4 +20,4 @@ export declare type IOpts = {
20
20
  clean?: boolean;
21
21
  watch?: boolean;
22
22
  disableCopy?: boolean;
23
- } & Pick<IConfigOpts, 'cache' | 'pkg'>;
23
+ } & Pick<IConfigOpts, 'cache' | 'pkg' | 'staticPathPrefix'>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/bundler-utoopack",
3
- "version": "4.6.56",
3
+ "version": "4.6.58",
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.8",
11
+ "@utoo/pack": "1.4.11",
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.56",
24
- "@umijs/bundler-webpack": "4.6.56"
23
+ "@umijs/bundler-utils": "4.6.58",
24
+ "@umijs/bundler-webpack": "4.6.58"
25
25
  },
26
26
  "devDependencies": {
27
27
  "father": "4.1.5"