@module-federation/nextjs-mf 5.3.3 → 5.4.1-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.
Files changed (38) hide show
  1. package/lib/ModuleFederationPlugin.js +5 -1
  2. package/lib/NextFederationPlugin.js +39 -1086
  3. package/lib/_virtual/Template.js +7 -0
  4. package/lib/_virtual/UrlNode.js +9 -0
  5. package/lib/_virtual/_commonjsHelpers.js +44 -0
  6. package/lib/_virtual/_fast-glob.js +16 -0
  7. package/lib/_virtual/_fs.js +16 -0
  8. package/lib/_virtual/_path.js +16 -0
  9. package/lib/_virtual/_tslib.js +176 -0
  10. package/lib/_virtual/_webpack-sources.js +16 -0
  11. package/lib/_virtual/fs.js +4 -0
  12. package/lib/_virtual/fs2.js +7 -0
  13. package/lib/_virtual/helpers.js +7 -0
  14. package/lib/_virtual/nextPageMapLoader.js +7 -0
  15. package/lib/_virtual/options.js +7 -0
  16. package/lib/_virtual/utils.js +7 -0
  17. package/lib/build-utils.js +176 -0
  18. package/lib/client/MFClient.d.ts +4 -1
  19. package/lib/client/MFClient.d.ts.map +1 -1
  20. package/lib/client/MFClient.js +35 -19
  21. package/lib/client/RemoteContainer.js +1 -0
  22. package/lib/client/RemotePages.d.ts +2 -1
  23. package/lib/client/RemotePages.d.ts.map +1 -1
  24. package/lib/client/RemotePages.js +38 -12
  25. package/lib/dependencies/webpack/lib/Template.js +437 -0
  26. package/lib/dependencies/webpack/lib/container/options.js +102 -0
  27. package/lib/dependencies/webpack/lib/sharing/utils.js +104 -0
  28. package/lib/dependencies/webpack/lib/util/fs.js +359 -0
  29. package/lib/index.js +1 -1
  30. package/lib/internal.js +58 -28
  31. package/lib/loaders/UrlNode.js +11 -1
  32. package/lib/loaders/fixImageLoader.js +14 -8
  33. package/lib/loaders/helpers.js +16 -3
  34. package/lib/loaders/nextPageMapLoader.js +26 -5
  35. package/lib/loaders/patchNextClientPageLoader.js +1 -1
  36. package/lib/plugins/DevHmrFixInvalidPongPlugin.js +23 -1
  37. package/lib/utils.js +24 -7
  38. package/package.json +7 -4
@@ -18,7 +18,7 @@ var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
18
18
  * Instance of this class is a Singleton and stored in `window.mf_client` variable.
19
19
  */
20
20
  var MFClient = /** @class */ (function () {
21
- function MFClient(nextPageLoader) {
21
+ function MFClient(nextPageLoader, opts) {
22
22
  var _this = this;
23
23
  var _a, _b;
24
24
  /** List of registered remotes */
@@ -26,7 +26,7 @@ var MFClient = /** @class */ (function () {
26
26
  this._nextPageLoader = nextPageLoader;
27
27
  this.events = new EventEmitter__default["default"]();
28
28
  var cfg = ((_b = (_a = global === null || global === void 0 ? void 0 : global.__NEXT_DATA__) === null || _a === void 0 ? void 0 : _a.props) === null || _b === void 0 ? void 0 : _b.mfRoutes) || {};
29
- this.remotePages = new RemotePages.RemotePages();
29
+ this.remotePages = new RemotePages.RemotePages(opts === null || opts === void 0 ? void 0 : opts.mode);
30
30
  Object.keys(cfg).forEach(function (remoteStr) {
31
31
  var remote = _this.registerRemote(remoteStr);
32
32
  _this.remotePages.addRoutes(cfg[remoteStr], remote);
@@ -109,7 +109,7 @@ var MFClient = /** @class */ (function () {
109
109
  }
110
110
  // replace loadRoute logic
111
111
  routeLoader.loadRoute = function (route, prefetch) { return _tslib.__awaiter(_this, void 0, void 0, function () {
112
- var routeInfo;
112
+ var routeInfo, e_1;
113
113
  return _tslib.__generator(this, function (_a) {
114
114
  switch (_a.label) {
115
115
  case 0: return [4 /*yield*/, this.combinedPages.isLocalRoute(route)];
@@ -119,13 +119,22 @@ var MFClient = /** @class */ (function () {
119
119
  case 2:
120
120
  routeInfo = _a.sent();
121
121
  this.events.emit('loadedLocalRoute', routeInfo, prefetch);
122
- return [3 /*break*/, 5];
123
- case 3: return [4 /*yield*/, this.remotePages.getRouteInfo(route)];
122
+ return [3 /*break*/, 7];
123
+ case 3:
124
+ _a.trys.push([3, 5, , 7]);
125
+ return [4 /*yield*/, this.remotePages.getRouteInfo(route)];
124
126
  case 4:
125
127
  routeInfo = _a.sent();
126
128
  this.events.emit('loadedRemoteRoute', routeInfo, prefetch, this.remotePages.routeToRemote(route));
127
- _a.label = 5;
128
- case 5: return [2 /*return*/, routeInfo];
129
+ return [3 /*break*/, 7];
130
+ case 5:
131
+ e_1 = _a.sent();
132
+ return [4 /*yield*/, routeLoader._loadRouteOriginal(route)];
133
+ case 6:
134
+ // as fallback try to use original loadRoute for keeping nextjs logic for routes load errors
135
+ routeInfo = _a.sent();
136
+ return [3 /*break*/, 7];
137
+ case 7: return [2 /*return*/, routeInfo];
129
138
  }
130
139
  });
131
140
  }); };
@@ -148,16 +157,19 @@ var MFClient = /** @class */ (function () {
148
157
  }
149
158
  // replace routeLoader.whenEntrypoint logic
150
159
  routeLoader.whenEntrypoint = function (route) { return _tslib.__awaiter(_this, void 0, void 0, function () {
151
- var route_1, awaitRemotes_1, routeInfo_1, routeInfo;
160
+ var route_1, awaitRemotes_1, routeInfo_1, e_2, routeInfo;
152
161
  var _this = this;
153
162
  return _tslib.__generator(this, function (_a) {
154
163
  switch (_a.label) {
155
164
  case 0:
156
- if (!(route === '/_error')) return [3 /*break*/, 6];
157
- return [4 /*yield*/, this.pathnameToRoute(window.location.pathname)];
165
+ if (!(route === '/_error')) return [3 /*break*/, 9];
166
+ _a.label = 1;
158
167
  case 1:
168
+ _a.trys.push([1, 8, , 9]);
169
+ return [4 /*yield*/, this.pathnameToRoute(window.location.pathname)];
170
+ case 2:
159
171
  route_1 = _a.sent();
160
- if (!!route_1) return [3 /*break*/, 4];
172
+ if (!!route_1) return [3 /*break*/, 5];
161
173
  awaitRemotes_1 = [];
162
174
  Object.values(this.remotes).forEach(function (remote) {
163
175
  if (!remote.isLoaded()) {
@@ -168,23 +180,27 @@ var MFClient = /** @class */ (function () {
168
180
  }
169
181
  });
170
182
  return [4 /*yield*/, Promise.all(awaitRemotes_1)];
171
- case 2:
183
+ case 3:
172
184
  _a.sent();
173
185
  return [4 /*yield*/, this.pathnameToRoute(window.location.pathname)];
174
- case 3:
175
- route_1 = _a.sent();
176
- _a.label = 4;
177
186
  case 4:
178
- if (!route_1) return [3 /*break*/, 6];
187
+ route_1 = _a.sent();
188
+ _a.label = 5;
189
+ case 5:
190
+ if (!route_1) return [3 /*break*/, 7];
179
191
  // TODO: fix router properties for the first page load of federated page http://localhost:3000/shop/products/B
180
192
  console.warn('replace entrypoint /_error by', route_1);
181
193
  return [4 /*yield*/, this.remotePages.getRouteInfo(route_1)];
182
- case 5:
194
+ case 6:
183
195
  routeInfo_1 = _a.sent();
184
196
  this.events.emit('loadedRemoteRoute', routeInfo_1, false, this.remotePages.routeToRemote(route_1));
185
197
  return [2 /*return*/, routeInfo_1];
186
- case 6: return [4 /*yield*/, routeLoader._whenEntrypointOriginal(route)];
187
- case 7:
198
+ case 7: return [3 /*break*/, 9];
199
+ case 8:
200
+ e_2 = _a.sent();
201
+ return [3 /*break*/, 9];
202
+ case 9: return [4 /*yield*/, routeLoader._whenEntrypointOriginal(route)];
203
+ case 10:
188
204
  routeInfo = _a.sent();
189
205
  return [2 /*return*/, routeInfo];
190
206
  }
@@ -123,6 +123,7 @@ var RemoteContainer = /** @class */ (function () {
123
123
  else {
124
124
  return [2 /*return*/, mod];
125
125
  }
126
+ return [2 /*return*/];
126
127
  }
127
128
  });
128
129
  });
@@ -14,7 +14,8 @@ export declare class RemotePages {
14
14
  paths: Record<PathPrefix, RemoteContainer>;
15
15
  pageListCache: string[] | undefined;
16
16
  private asyncLoadedPageMaps;
17
- constructor(remoteToRoutes?: RemoteToRoutes);
17
+ private mode?;
18
+ constructor(mode?: 'production' | 'development');
18
19
  /**
19
20
  * Add remote routes for specific RemoteContainer which serves them.
20
21
  */
@@ -1 +1 @@
1
- {"version":3,"file":"RemotePages.d.ts","sourceRoot":"","sources":["../../src/client/RemotePages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAE7D,oBAAY,UAAU,GAAG,MAAM,CAAC;AAEhC,oBAAY,cAAc,GAAG,GAAG,CAAC,eAAe,EAAE,UAAU,GAAG,UAAU,EAAE,CAAC,CAAC;AAE7E,oBAAY,SAAS,GAAG;IACtB,SAAS,EAAE,GAAG,CAAC;IACf,OAAO,EAAE,GAAG,CAAC;IACb,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,CAAC;AAEF;;;GAGG;AACH,qBAAa,WAAW;IACtB,KAAK,EAAE,MAAM,CAAC,UAAU,EAAE,eAAe,CAAC,CAAM;IAChD,aAAa,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;IACpC,OAAO,CAAC,mBAAmB,CAAuB;gBAEtC,cAAc,CAAC,EAAE,cAAc;IAQ3C;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,MAAM,EAAE,eAAe;IAW5D;;OAEG;IACG,iBAAiB,CACrB,MAAM,EAAE,eAAe,GACtB,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;IAc/B;;;OAGG;IACG,iBAAiB,CAAC,KAAK,EAAE,MAAM;IAmBrC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIhC;;OAEG;IACH,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IAsBzD;;OAEG;IACH,WAAW;IAQX;;OAEG;IACG,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;CAuBlE"}
1
+ {"version":3,"file":"RemotePages.d.ts","sourceRoot":"","sources":["../../src/client/RemotePages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAG7D,oBAAY,UAAU,GAAG,MAAM,CAAC;AAEhC,oBAAY,cAAc,GAAG,GAAG,CAAC,eAAe,EAAE,UAAU,GAAG,UAAU,EAAE,CAAC,CAAC;AAE7E,oBAAY,SAAS,GAAG;IACtB,SAAS,EAAE,GAAG,CAAC;IACf,OAAO,EAAE,GAAG,CAAC;IACb,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,CAAC;AAEF;;;GAGG;AACH,qBAAa,WAAW;IACtB,KAAK,EAAE,MAAM,CAAC,UAAU,EAAE,eAAe,CAAC,CAAM;IAChD,aAAa,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;IACpC,OAAO,CAAC,mBAAmB,CAAuB;IAClD,OAAO,CAAC,IAAI,CAAC,CAA+B;gBAEhC,IAAI,CAAC,EAAE,YAAY,GAAG,aAAa;IAM/C;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,MAAM,EAAE,eAAe;IAW5D;;OAEG;IACG,iBAAiB,CACrB,MAAM,EAAE,eAAe,GACtB,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;IAc/B;;;OAGG;IACG,iBAAiB,CAAC,KAAK,EAAE,MAAM;IAmBrC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIhC;;OAEG;IACH,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IAsBzD;;OAEG;IACH,WAAW;IAQX;;OAEG;IACG,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;CAiDlE"}
@@ -3,20 +3,38 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('../_virtual/_tslib.js');
6
+ var React = require('react');
7
+
8
+ function _interopNamespace(e) {
9
+ if (e && e.__esModule) return e;
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n["default"] = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
6
27
 
7
28
  /**
8
29
  * A class which prepares/loads a list of remotes pages and knows how
9
30
  * to prepare NextJS pseudo-module of route data.
10
31
  */
11
32
  var RemotePages = /** @class */ (function () {
12
- function RemotePages(remoteToRoutes) {
13
- var _this = this;
33
+ function RemotePages(mode) {
14
34
  this.paths = {};
15
35
  this.asyncLoadedPageMaps = new Set();
16
36
  this.pageListCache = undefined;
17
- remoteToRoutes === null || remoteToRoutes === void 0 ? void 0 : remoteToRoutes.forEach(function (routes, remote) {
18
- _this.addRoutes(routes, remote);
19
- });
37
+ this.mode = mode;
20
38
  }
21
39
  /**
22
40
  * Add remote routes for specific RemoteContainer which serves them.
@@ -149,13 +167,21 @@ var RemotePages = /** @class */ (function () {
149
167
  return [3 /*break*/, 3];
150
168
  case 2:
151
169
  e_1 = _a.sent();
152
- routeInfo = {
153
- // TODO: provide ability to customize component with Error
154
- component: function () { return e_1.message; },
155
- exports: {},
156
- styles: [],
157
- };
158
- console.warn(e_1);
170
+ if (this.mode !== 'development') {
171
+ // in PROD throw error, nextjs will reload the page according to its internal logic
172
+ throw e_1;
173
+ }
174
+ else {
175
+ // in DEV mod show error in browser
176
+ console.warn(e_1);
177
+ routeInfo = {
178
+ component: function () {
179
+ return React__namespace.createElement('div', null, React__namespace.createElement('div', null, 'This page is shown only in DEVELOPMENT mode. In PRODUCTION NextJS will reload this page trying to obtain it again from the server.'), React__namespace.createElement('div', null, 'Federated page ', route, ' load error:'), React__namespace.createElement('div', null, React__namespace.createElement('b', null, e_1.message)));
180
+ },
181
+ exports: {},
182
+ styles: [],
183
+ };
184
+ }
159
185
  return [3 /*break*/, 3];
160
186
  case 3: return [2 /*return*/, routeInfo];
161
187
  }
@@ -0,0 +1,437 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ require('../../../_virtual/_commonjsHelpers.js');
6
+ var Template$1 = require('../../../_virtual/Template.js');
7
+ require('../../../_virtual/_webpack-sources.js');
8
+ var require$$0 = require('webpack-sources');
9
+
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
13
+
14
+ /*
15
+ MIT License http://www.opensource.org/licenses/mit-license.php
16
+ Author Tobias Koppers @sokra
17
+ */
18
+
19
+ (function (module) {
20
+ "use strict";
21
+
22
+ const { ConcatSource, PrefixSource } = require$$0__default["default"];
23
+
24
+ /** @typedef {import("webpack-sources").Source} Source */
25
+ /** @typedef {import("../declarations/WebpackOptions").Output} OutputOptions */
26
+ /** @typedef {import("./Chunk")} Chunk */
27
+ /** @typedef {import("./ChunkGraph")} ChunkGraph */
28
+ /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
29
+ /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
30
+ /** @typedef {import("./Compilation").PathData} PathData */
31
+ /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
32
+ /** @typedef {import("./Module")} Module */
33
+ /** @typedef {import("./ModuleGraph")} ModuleGraph */
34
+ /** @typedef {import("./ModuleTemplate")} ModuleTemplate */
35
+ /** @typedef {import("./RuntimeModule")} RuntimeModule */
36
+ /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
37
+ /** @typedef {import("./javascript/JavascriptModulesPlugin").ChunkRenderContext} ChunkRenderContext */
38
+ /** @typedef {import("./javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
39
+
40
+ const START_LOWERCASE_ALPHABET_CODE = "a".charCodeAt(0);
41
+ const START_UPPERCASE_ALPHABET_CODE = "A".charCodeAt(0);
42
+ const DELTA_A_TO_Z = "z".charCodeAt(0) - START_LOWERCASE_ALPHABET_CODE + 1;
43
+ const NUMBER_OF_IDENTIFIER_START_CHARS = DELTA_A_TO_Z * 2 + 2; // a-z A-Z _ $
44
+ const NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS =
45
+ NUMBER_OF_IDENTIFIER_START_CHARS + 10; // a-z A-Z _ $ 0-9
46
+ const FUNCTION_CONTENT_REGEX = /^function\s?\(\)\s?\{\r?\n?|\r?\n?\}$/g;
47
+ const INDENT_MULTILINE_REGEX = /^\t/gm;
48
+ const LINE_SEPARATOR_REGEX = /\r?\n/g;
49
+ const IDENTIFIER_NAME_REPLACE_REGEX = /^([^a-zA-Z$_])/;
50
+ const IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX = /[^a-zA-Z0-9$]+/g;
51
+ const COMMENT_END_REGEX = /\*\//g;
52
+ const PATH_NAME_NORMALIZE_REPLACE_REGEX = /[^a-zA-Z0-9_!§$()=\-^°]+/g;
53
+ const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
54
+
55
+ /**
56
+ * @typedef {Object} RenderManifestOptions
57
+ * @property {Chunk} chunk the chunk used to render
58
+ * @property {string} hash
59
+ * @property {string} fullHash
60
+ * @property {OutputOptions} outputOptions
61
+ * @property {CodeGenerationResults} codeGenerationResults
62
+ * @property {{javascript: ModuleTemplate}} moduleTemplates
63
+ * @property {DependencyTemplates} dependencyTemplates
64
+ * @property {RuntimeTemplate} runtimeTemplate
65
+ * @property {ModuleGraph} moduleGraph
66
+ * @property {ChunkGraph} chunkGraph
67
+ */
68
+
69
+ /** @typedef {RenderManifestEntryTemplated | RenderManifestEntryStatic} RenderManifestEntry */
70
+
71
+ /**
72
+ * @typedef {Object} RenderManifestEntryTemplated
73
+ * @property {function(): Source} render
74
+ * @property {string | function(PathData, AssetInfo=): string} filenameTemplate
75
+ * @property {PathData=} pathOptions
76
+ * @property {AssetInfo=} info
77
+ * @property {string} identifier
78
+ * @property {string=} hash
79
+ * @property {boolean=} auxiliary
80
+ */
81
+
82
+ /**
83
+ * @typedef {Object} RenderManifestEntryStatic
84
+ * @property {function(): Source} render
85
+ * @property {string} filename
86
+ * @property {AssetInfo} info
87
+ * @property {string} identifier
88
+ * @property {string=} hash
89
+ * @property {boolean=} auxiliary
90
+ */
91
+
92
+ /**
93
+ * @typedef {Object} HasId
94
+ * @property {number | string} id
95
+ */
96
+
97
+ /**
98
+ * @typedef {function(Module, number): boolean} ModuleFilterPredicate
99
+ */
100
+
101
+ class Template {
102
+ /**
103
+ *
104
+ * @param {Function} fn a runtime function (.runtime.js) "template"
105
+ * @returns {string} the updated and normalized function string
106
+ */
107
+ static getFunctionContent(fn) {
108
+ return fn
109
+ .toString()
110
+ .replace(FUNCTION_CONTENT_REGEX, "")
111
+ .replace(INDENT_MULTILINE_REGEX, "")
112
+ .replace(LINE_SEPARATOR_REGEX, "\n");
113
+ }
114
+
115
+ /**
116
+ * @param {string} str the string converted to identifier
117
+ * @returns {string} created identifier
118
+ */
119
+ static toIdentifier(str) {
120
+ if (typeof str !== "string") return "";
121
+ return str
122
+ .replace(IDENTIFIER_NAME_REPLACE_REGEX, "_$1")
123
+ .replace(IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX, "_");
124
+ }
125
+ /**
126
+ *
127
+ * @param {string} str string to be converted to commented in bundle code
128
+ * @returns {string} returns a commented version of string
129
+ */
130
+ static toComment(str) {
131
+ if (!str) return "";
132
+ return `/*! ${str.replace(COMMENT_END_REGEX, "* /")} */`;
133
+ }
134
+
135
+ /**
136
+ *
137
+ * @param {string} str string to be converted to "normal comment"
138
+ * @returns {string} returns a commented version of string
139
+ */
140
+ static toNormalComment(str) {
141
+ if (!str) return "";
142
+ return `/* ${str.replace(COMMENT_END_REGEX, "* /")} */`;
143
+ }
144
+
145
+ /**
146
+ * @param {string} str string path to be normalized
147
+ * @returns {string} normalized bundle-safe path
148
+ */
149
+ static toPath(str) {
150
+ if (typeof str !== "string") return "";
151
+ return str
152
+ .replace(PATH_NAME_NORMALIZE_REPLACE_REGEX, "-")
153
+ .replace(MATCH_PADDED_HYPHENS_REPLACE_REGEX, "");
154
+ }
155
+
156
+ // map number to a single character a-z, A-Z or multiple characters if number is too big
157
+ /**
158
+ * @param {number} n number to convert to ident
159
+ * @returns {string} returns single character ident
160
+ */
161
+ static numberToIdentifier(n) {
162
+ if (n >= NUMBER_OF_IDENTIFIER_START_CHARS) {
163
+ // use multiple letters
164
+ return (
165
+ Template.numberToIdentifier(n % NUMBER_OF_IDENTIFIER_START_CHARS) +
166
+ Template.numberToIdentifierContinuation(
167
+ Math.floor(n / NUMBER_OF_IDENTIFIER_START_CHARS)
168
+ )
169
+ );
170
+ }
171
+
172
+ // lower case
173
+ if (n < DELTA_A_TO_Z) {
174
+ return String.fromCharCode(START_LOWERCASE_ALPHABET_CODE + n);
175
+ }
176
+ n -= DELTA_A_TO_Z;
177
+
178
+ // upper case
179
+ if (n < DELTA_A_TO_Z) {
180
+ return String.fromCharCode(START_UPPERCASE_ALPHABET_CODE + n);
181
+ }
182
+
183
+ if (n === DELTA_A_TO_Z) return "_";
184
+ return "$";
185
+ }
186
+
187
+ /**
188
+ * @param {number} n number to convert to ident
189
+ * @returns {string} returns single character ident
190
+ */
191
+ static numberToIdentifierContinuation(n) {
192
+ if (n >= NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS) {
193
+ // use multiple letters
194
+ return (
195
+ Template.numberToIdentifierContinuation(
196
+ n % NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS
197
+ ) +
198
+ Template.numberToIdentifierContinuation(
199
+ Math.floor(n / NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS)
200
+ )
201
+ );
202
+ }
203
+
204
+ // lower case
205
+ if (n < DELTA_A_TO_Z) {
206
+ return String.fromCharCode(START_LOWERCASE_ALPHABET_CODE + n);
207
+ }
208
+ n -= DELTA_A_TO_Z;
209
+
210
+ // upper case
211
+ if (n < DELTA_A_TO_Z) {
212
+ return String.fromCharCode(START_UPPERCASE_ALPHABET_CODE + n);
213
+ }
214
+ n -= DELTA_A_TO_Z;
215
+
216
+ // numbers
217
+ if (n < 10) {
218
+ return `${n}`;
219
+ }
220
+
221
+ if (n === 10) return "_";
222
+ return "$";
223
+ }
224
+
225
+ /**
226
+ *
227
+ * @param {string | string[]} s string to convert to identity
228
+ * @returns {string} converted identity
229
+ */
230
+ static indent(s) {
231
+ if (Array.isArray(s)) {
232
+ return s.map(Template.indent).join("\n");
233
+ } else {
234
+ const str = s.trimRight();
235
+ if (!str) return "";
236
+ const ind = str[0] === "\n" ? "" : "\t";
237
+ return ind + str.replace(/\n([^\n])/g, "\n\t$1");
238
+ }
239
+ }
240
+
241
+ /**
242
+ *
243
+ * @param {string|string[]} s string to create prefix for
244
+ * @param {string} prefix prefix to compose
245
+ * @returns {string} returns new prefix string
246
+ */
247
+ static prefix(s, prefix) {
248
+ const str = Template.asString(s).trim();
249
+ if (!str) return "";
250
+ const ind = str[0] === "\n" ? "" : prefix;
251
+ return ind + str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
252
+ }
253
+
254
+ /**
255
+ *
256
+ * @param {string|string[]} str string or string collection
257
+ * @returns {string} returns a single string from array
258
+ */
259
+ static asString(str) {
260
+ if (Array.isArray(str)) {
261
+ return str.join("\n");
262
+ }
263
+ return str;
264
+ }
265
+
266
+ /**
267
+ * @typedef {Object} WithId
268
+ * @property {string|number} id
269
+ */
270
+
271
+ /**
272
+ * @param {WithId[]} modules a collection of modules to get array bounds for
273
+ * @returns {[number, number] | false} returns the upper and lower array bounds
274
+ * or false if not every module has a number based id
275
+ */
276
+ static getModulesArrayBounds(modules) {
277
+ let maxId = -Infinity;
278
+ let minId = Infinity;
279
+ for (const module of modules) {
280
+ const moduleId = module.id;
281
+ if (typeof moduleId !== "number") return false;
282
+ if (maxId < moduleId) maxId = moduleId;
283
+ if (minId > moduleId) minId = moduleId;
284
+ }
285
+ if (minId < 16 + ("" + minId).length) {
286
+ // add minId x ',' instead of 'Array(minId).concat(…)'
287
+ minId = 0;
288
+ }
289
+ // start with -1 because the first module needs no comma
290
+ let objectOverhead = -1;
291
+ for (const module of modules) {
292
+ // module id + colon + comma
293
+ objectOverhead += `${module.id}`.length + 2;
294
+ }
295
+ // number of commas, or when starting non-zero the length of Array(minId).concat()
296
+ const arrayOverhead = minId === 0 ? maxId : 16 + `${minId}`.length + maxId;
297
+ return arrayOverhead < objectOverhead ? [minId, maxId] : false;
298
+ }
299
+
300
+ /**
301
+ * @param {ChunkRenderContext} renderContext render context
302
+ * @param {Module[]} modules modules to render (should be ordered by identifier)
303
+ * @param {function(Module): Source} renderModule function to render a module
304
+ * @param {string=} prefix applying prefix strings
305
+ * @returns {Source} rendered chunk modules in a Source object
306
+ */
307
+ static renderChunkModules(renderContext, modules, renderModule, prefix = "") {
308
+ const { chunkGraph } = renderContext;
309
+ var source = new ConcatSource();
310
+ if (modules.length === 0) {
311
+ return null;
312
+ }
313
+ /** @type {{id: string|number, source: Source|string}[]} */
314
+ const allModules = modules.map(module => {
315
+ return {
316
+ id: chunkGraph.getModuleId(module),
317
+ source: renderModule(module) || "false"
318
+ };
319
+ });
320
+ const bounds = Template.getModulesArrayBounds(allModules);
321
+ if (bounds) {
322
+ // Render a spare array
323
+ const minId = bounds[0];
324
+ const maxId = bounds[1];
325
+ if (minId !== 0) {
326
+ source.add(`Array(${minId}).concat(`);
327
+ }
328
+ source.add("[\n");
329
+ /** @type {Map<string|number, {id: string|number, source: Source|string}>} */
330
+ const modules = new Map();
331
+ for (const module of allModules) {
332
+ modules.set(module.id, module);
333
+ }
334
+ for (let idx = minId; idx <= maxId; idx++) {
335
+ const module = modules.get(idx);
336
+ if (idx !== minId) {
337
+ source.add(",\n");
338
+ }
339
+ source.add(`/* ${idx} */`);
340
+ if (module) {
341
+ source.add("\n");
342
+ source.add(module.source);
343
+ }
344
+ }
345
+ source.add("\n" + prefix + "]");
346
+ if (minId !== 0) {
347
+ source.add(")");
348
+ }
349
+ } else {
350
+ // Render an object
351
+ source.add("{\n");
352
+ for (let i = 0; i < allModules.length; i++) {
353
+ const module = allModules[i];
354
+ if (i !== 0) {
355
+ source.add(",\n");
356
+ }
357
+ source.add(`\n/***/ ${JSON.stringify(module.id)}:\n`);
358
+ source.add(module.source);
359
+ }
360
+ source.add(`\n\n${prefix}}`);
361
+ }
362
+ return source;
363
+ }
364
+
365
+ /**
366
+ * @param {RuntimeModule[]} runtimeModules array of runtime modules in order
367
+ * @param {RenderContext & { codeGenerationResults?: CodeGenerationResults }} renderContext render context
368
+ * @returns {Source} rendered runtime modules in a Source object
369
+ */
370
+ static renderRuntimeModules(runtimeModules, renderContext) {
371
+ const source = new ConcatSource();
372
+ for (const module of runtimeModules) {
373
+ const codeGenerationResults = renderContext.codeGenerationResults;
374
+ let runtimeSource;
375
+ if (codeGenerationResults) {
376
+ runtimeSource = codeGenerationResults.getSource(
377
+ module,
378
+ renderContext.chunk.runtime,
379
+ "runtime"
380
+ );
381
+ } else {
382
+ const codeGenResult = module.codeGeneration({
383
+ chunkGraph: renderContext.chunkGraph,
384
+ dependencyTemplates: renderContext.dependencyTemplates,
385
+ moduleGraph: renderContext.moduleGraph,
386
+ runtimeTemplate: renderContext.runtimeTemplate,
387
+ runtime: renderContext.chunk.runtime
388
+ });
389
+ if (!codeGenResult) continue;
390
+ runtimeSource = codeGenResult.sources.get("runtime");
391
+ }
392
+ if (runtimeSource) {
393
+ source.add(Template.toNormalComment(module.identifier()) + "\n");
394
+ if (!module.shouldIsolate()) {
395
+ source.add(runtimeSource);
396
+ source.add("\n\n");
397
+ } else if (renderContext.runtimeTemplate.supportsArrowFunction()) {
398
+ source.add("(() => {\n");
399
+ source.add(new PrefixSource("\t", runtimeSource));
400
+ source.add("\n})();\n\n");
401
+ } else {
402
+ source.add("!function() {\n");
403
+ source.add(new PrefixSource("\t", runtimeSource));
404
+ source.add("\n}();\n\n");
405
+ }
406
+ }
407
+ }
408
+ return source;
409
+ }
410
+
411
+ /**
412
+ * @param {RuntimeModule[]} runtimeModules array of runtime modules in order
413
+ * @param {RenderContext} renderContext render context
414
+ * @returns {Source} rendered chunk runtime modules in a Source object
415
+ */
416
+ static renderChunkRuntimeModules(runtimeModules, renderContext) {
417
+ return new PrefixSource(
418
+ "/******/ ",
419
+ new ConcatSource(
420
+ "function(__webpack_require__) { // webpackRuntimeModules\n",
421
+ this.renderRuntimeModules(runtimeModules, renderContext),
422
+ "}\n"
423
+ )
424
+ );
425
+ }
426
+ }
427
+
428
+ module.exports = Template;
429
+ module.exports.NUMBER_OF_IDENTIFIER_START_CHARS =
430
+ NUMBER_OF_IDENTIFIER_START_CHARS;
431
+ module.exports.NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS =
432
+ NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS;
433
+ } (Template$1.Template));
434
+
435
+ var Template = Template$1.Template.exports;
436
+
437
+ exports["default"] = Template;