@parcel/packager-react-static 2.13.4-canary.3392 → 2.13.4-canary.3393

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.
@@ -67,14 +67,29 @@ function _stream() {
67
67
  };
68
68
  return data;
69
69
  }
70
+ function _rust() {
71
+ const data = require("@parcel/rust");
72
+ _rust = function () {
73
+ return data;
74
+ };
75
+ return data;
76
+ }
77
+ function _nodeAsync_hooks() {
78
+ const data = require("node:async_hooks");
79
+ _nodeAsync_hooks = function () {
80
+ return data;
81
+ };
82
+ return data;
83
+ }
70
84
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
85
+ // $FlowFixMe
71
86
  let clientResolver;
72
87
  let serverResolver;
73
88
  let packagingBundles = new Map();
74
89
  let moduleCache = new Map();
75
90
  let loadedBundles = new Map();
76
91
  var _default = exports.default = new (_plugin().Packager)({
77
- loadConfig({
92
+ async loadConfig({
78
93
  options,
79
94
  config
80
95
  }) {
@@ -91,15 +106,28 @@ var _default = exports.default = new (_plugin().Packager)({
91
106
  packageExports: true,
92
107
  conditions: 1 << 16 // "react-server"
93
108
  });
109
+
110
+ // This logic must be synced with the packager...
111
+ let packageName = await config.getConfigFrom(options.projectRoot + '/index', [], {
112
+ packageKey: 'name'
113
+ });
114
+ let name = (packageName === null || packageName === void 0 ? void 0 : packageName.contents) ?? '';
115
+ return {
116
+ parcelRequireName: 'parcelRequire' + (0, _rust().hashString)(name).slice(-4)
117
+ };
94
118
  },
95
119
  async package({
96
120
  bundle,
97
121
  bundleGraph,
98
- getInlineBundleContents
122
+ getInlineBundleContents,
123
+ config
99
124
  }) {
100
125
  if (bundle.env.shouldScopeHoist) {
101
126
  throw new Error('Scope hoisting is not supported with SSG');
102
127
  }
128
+
129
+ // $FlowFixMe
130
+ globalThis.AsyncLocalStorage ??= _nodeAsync_hooks().AsyncLocalStorage;
103
131
  let {
104
132
  load,
105
133
  loadModule
@@ -137,15 +165,52 @@ var _default = exports.default = new (_plugin().Packager)({
137
165
  meta: pageMeta((0, _nullthrows().default)(bundle.getMainEntry()).meta)
138
166
  }
139
167
  };
140
- let stream = renderToReadableStream(React.createElement(Component, props));
168
+ let resources = [];
169
+ let bootstrapModules = [];
170
+ let entry;
171
+ for (let b of bundleGraph.getReferencedBundles(bundle, {
172
+ includeInline: false
173
+ })) {
174
+ if (b.type === 'css') {
175
+ resources.push(React.createElement('link', {
176
+ rel: 'stylesheet',
177
+ href: (0, _utils().urlJoin)(b.target.publicUrl, b.name),
178
+ precedence: 'default'
179
+ }));
180
+ } else if (b.type === 'js' && b.env.isBrowser()) {
181
+ bootstrapModules.push((0, _utils().urlJoin)(b.target.publicUrl, b.name));
182
+ resources.push(React.createElement('script', {
183
+ type: 'module',
184
+ async: true,
185
+ src: (0, _utils().urlJoin)(b.target.publicUrl, b.name)
186
+ }));
187
+ if (!entry) {
188
+ b.traverseAssets((a, ctx, actions) => {
189
+ if (Array.isArray(a.meta.directives) && a.meta.directives.includes('use client-entry')) {
190
+ entry = a;
191
+ actions.stop();
192
+ }
193
+ });
194
+ }
195
+ }
196
+ }
197
+ let stream = renderToReadableStream([...resources, React.createElement(Component, props)]);
141
198
  let [s1, renderStream] = stream.tee();
142
199
  let [injectStream, rscStream] = s1.tee();
143
- let data = createFromReadableStream(renderStream);
200
+ let data;
201
+ function Content() {
202
+ data ??= createFromReadableStream(renderStream);
203
+ return React.use(data);
204
+ }
205
+ let bootstrapScriptContent;
206
+ if (entry) {
207
+ bootstrapScriptContent = `Promise.all([${bootstrapModules.map(m => `import("${m}")`).join(',')}]).then(()=>${(0, _nullthrows().default)(config).parcelRequireName}(${JSON.stringify(bundleGraph.getAssetPublicId(entry))}))`;
208
+ }
144
209
  let {
145
210
  prelude
146
- } = await prerender(React.createElement(function () {
147
- return React.use(data);
148
- }));
211
+ } = await prerender(React.createElement(Content), {
212
+ bootstrapScriptContent
213
+ });
149
214
  let response = prelude.pipeThrough(injectRSCPayload(injectStream));
150
215
  return [{
151
216
  type: 'html',
@@ -181,15 +246,23 @@ async function loadBundleUncached(bundle, bundleGraph, getInlineBundleContents)
181
246
  let packagedBundle = await (0, _nullthrows().default)(packagingBundles.get(entryBundle));
182
247
  let contents = await (0, _utils().blobToString)(packagedBundle.contents);
183
248
  contents = `module.exports = ${contents}`;
184
- return [entryBundle.id, [(0, _nullthrows().default)(entryBundle.getMainEntry()), contents]];
249
+ return [[entryBundle.id, [(0, _nullthrows().default)(entryBundle.getMainEntry()), contents]]];
250
+ });
251
+ } else if (entryBundle) {
252
+ // console.log('here', entryBundle)
253
+ queue.add(async () => {
254
+ let {
255
+ assets: subAssets
256
+ } = await loadBundle(entryBundle, bundleGraph, getInlineBundleContents);
257
+ return Array.from(subAssets);
185
258
  });
186
259
  }
187
260
  } else if (node.type === 'asset') {
188
261
  let asset = node.value;
189
- queue.add(async () => [asset.id, [asset, await asset.getCode()]]);
262
+ queue.add(async () => [[asset.id, [asset, await asset.getCode()]]]);
190
263
  }
191
264
  });
192
- let assets = new Map(await queue.run());
265
+ let assets = new Map((await queue.run()).flatMap(v => v));
193
266
  let assetsByFilePath = new Map();
194
267
  let assetsByPublicId = new Map();
195
268
  for (let [asset] of assets.values()) {
@@ -251,24 +324,23 @@ async function loadBundleUncached(bundle, bundleGraph, getInlineBundleContents)
251
324
  if (resolution !== null && resolution !== void 0 && resolution.specifier) {
252
325
  id = resolution.specifier;
253
326
  }
327
+ if (id.startsWith('.')) {
328
+ // Another bundle. Should already be loaded.
329
+ return {};
330
+ }
254
331
  return defaultRequire(id);
255
332
  };
256
-
257
- // @ts-ignore
258
333
  require.resolve = defaultRequire.resolve;
259
334
  return runModule(code, asset.filePath, cacheKey, require, parcelRequire);
260
335
  };
261
336
  let parcelRequire = publicId => {
262
337
  return loadAsset((0, _nullthrows().default)(assetsByPublicId.get(publicId)));
263
338
  };
264
-
265
- // @ts-ignore
339
+ parcelRequire.root = parcelRequire;
266
340
  parcelRequire.meta = {
267
341
  distDir: bundle.target.distDir,
268
342
  publicUrl: bundle.target.publicUrl
269
343
  };
270
-
271
- // @ts-ignore
272
344
  parcelRequire.load = async filePath => {
273
345
  let bundle = bundleGraph.getBundles().find(b => b.name === filePath);
274
346
  if (bundle) {
@@ -317,8 +389,6 @@ async function loadBundleUncached(bundle, bundleGraph, getInlineBundleContents)
317
389
  let require = id => {
318
390
  return loadModule(id, filePath, env);
319
391
  };
320
-
321
- // @ts-ignore
322
392
  require.resolve = defaultRequire.resolve;
323
393
  return runModule(code, filePath, cacheKey, require, parcelRequire);
324
394
  }
@@ -331,6 +401,7 @@ async function loadBundleUncached(bundle, bundleGraph, getInlineBundleContents)
331
401
  };
332
402
  }
333
403
  function runModule(code, filename, id, require, parcelRequire) {
404
+ // code = code.replace(/import\((['"].*?['"])\)/g, (_, m) => `parcelRequire.load(${m[0] + m.slice(3)})`);
334
405
  let moduleFunction = _vm().default.compileFunction(code, ['exports', 'require', 'module', '__dirname', '__filename', 'parcelRequire'], {
335
406
  filename
336
407
  });
@@ -341,7 +412,8 @@ function runModule(code, filename, id, require, parcelRequire) {
341
412
  children: [],
342
413
  filename,
343
414
  id,
344
- path: dirname
415
+ path: dirname,
416
+ bundle: parcelRequire
345
417
  };
346
418
  moduleCache.set(id, module);
347
419
  moduleFunction(module.exports, require, module, dirname, filename, parcelRequire);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/packager-react-static",
3
- "version": "2.13.4-canary.3392+337c31c68",
3
+ "version": "2.13.4-canary.3393+e3a2b726d",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,15 +17,16 @@
17
17
  "source": "src/ReactStaticPackager.js",
18
18
  "engines": {
19
19
  "node": ">= 16.0.0",
20
- "parcel": "^2.0.0-canary.1767+337c31c68"
20
+ "parcel": "^2.0.0-canary.1768+e3a2b726d"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/node-resolver-core": "3.4.4-canary.3392+337c31c68",
24
- "@parcel/plugin": "2.0.0-canary.1769+337c31c68",
25
- "@parcel/types": "2.0.0-canary.1769+337c31c68",
26
- "@parcel/utils": "2.0.0-canary.1769+337c31c68",
23
+ "@parcel/node-resolver-core": "3.4.4-canary.3393+e3a2b726d",
24
+ "@parcel/plugin": "2.0.0-canary.1770+e3a2b726d",
25
+ "@parcel/rust": "2.13.4-canary.3393+e3a2b726d",
26
+ "@parcel/types": "2.0.0-canary.1770+e3a2b726d",
27
+ "@parcel/utils": "2.0.0-canary.1770+e3a2b726d",
27
28
  "nullthrows": "^1.1.1",
28
29
  "rsc-html-stream": "^0.0.4"
29
30
  },
30
- "gitHead": "337c31c68ff350f1a971752746bb0e01583f5ef4"
31
+ "gitHead": "e3a2b726db9382a873834da82746671d54557d2b"
31
32
  }
@@ -18,6 +18,9 @@ import {ResolverBase} from '@parcel/node-resolver-core';
18
18
  import vm from 'vm';
19
19
  import nullthrows from 'nullthrows';
20
20
  import {Readable} from 'stream';
21
+ import {hashString} from '@parcel/rust';
22
+ // $FlowFixMe
23
+ import {AsyncLocalStorage} from 'node:async_hooks';
21
24
 
22
25
  export interface Page {
23
26
  url: string;
@@ -45,7 +48,7 @@ let moduleCache = new Map<string, ParcelModule>();
45
48
  let loadedBundles = new Map<NamedBundle, any>();
46
49
 
47
50
  export default (new Packager({
48
- loadConfig({options, config}) {
51
+ async loadConfig({options, config}) {
49
52
  config.invalidateOnBuild();
50
53
  packagingBundles.clear();
51
54
  moduleCache.clear();
@@ -60,12 +63,29 @@ export default (new Packager({
60
63
  packageExports: true,
61
64
  conditions: 1 << 16, // "react-server"
62
65
  });
66
+
67
+ // This logic must be synced with the packager...
68
+ let packageName = await config.getConfigFrom(
69
+ options.projectRoot + '/index',
70
+ [],
71
+ {
72
+ packageKey: 'name',
73
+ },
74
+ );
75
+
76
+ let name = packageName?.contents ?? '';
77
+ return {
78
+ parcelRequireName: 'parcelRequire' + hashString(name).slice(-4),
79
+ };
63
80
  },
64
- async package({bundle, bundleGraph, getInlineBundleContents}) {
81
+ async package({bundle, bundleGraph, getInlineBundleContents, config}) {
65
82
  if (bundle.env.shouldScopeHoist) {
66
83
  throw new Error('Scope hoisting is not supported with SSG');
67
84
  }
68
85
 
86
+ // $FlowFixMe
87
+ globalThis.AsyncLocalStorage ??= AsyncLocalStorage;
88
+
69
89
  let {load, loadModule} = await loadBundle(
70
90
  bundle,
71
91
  bundleGraph,
@@ -112,15 +132,68 @@ export default (new Packager({
112
132
  },
113
133
  };
114
134
 
115
- let stream = renderToReadableStream(React.createElement(Component, props));
135
+ let resources = [];
136
+ let bootstrapModules = [];
137
+ let entry;
138
+ for (let b of bundleGraph.getReferencedBundles(bundle, {
139
+ includeInline: false,
140
+ })) {
141
+ if (b.type === 'css') {
142
+ resources.push(
143
+ React.createElement('link', {
144
+ rel: 'stylesheet',
145
+ href: urlJoin(b.target.publicUrl, b.name),
146
+ precedence: 'default',
147
+ }),
148
+ );
149
+ } else if (b.type === 'js' && b.env.isBrowser()) {
150
+ bootstrapModules.push(urlJoin(b.target.publicUrl, b.name));
151
+ resources.push(
152
+ React.createElement('script', {
153
+ type: 'module',
154
+ async: true,
155
+ src: urlJoin(b.target.publicUrl, b.name),
156
+ }),
157
+ );
158
+
159
+ if (!entry) {
160
+ b.traverseAssets((a, ctx, actions) => {
161
+ if (
162
+ Array.isArray(a.meta.directives) &&
163
+ a.meta.directives.includes('use client-entry')
164
+ ) {
165
+ entry = a;
166
+ actions.stop();
167
+ }
168
+ });
169
+ }
170
+ }
171
+ }
172
+
173
+ let stream = renderToReadableStream([
174
+ ...resources,
175
+ React.createElement(Component, props),
176
+ ]);
116
177
  let [s1, renderStream] = stream.tee();
117
178
  let [injectStream, rscStream] = s1.tee();
118
- let data = createFromReadableStream(renderStream);
179
+ let data;
119
180
  function Content() {
181
+ data ??= createFromReadableStream(renderStream);
120
182
  return React.use(data);
121
183
  }
122
184
 
123
- let {prelude} = await prerender(React.createElement(Content));
185
+ let bootstrapScriptContent;
186
+ if (entry) {
187
+ bootstrapScriptContent = `Promise.all([${bootstrapModules
188
+ .map(m => `import("${m}")`)
189
+ .join(',')}]).then(()=>${
190
+ nullthrows(config).parcelRequireName
191
+ }(${JSON.stringify(bundleGraph.getAssetPublicId(entry))}))`;
192
+ }
193
+
194
+ let {prelude} = await prerender(React.createElement(Content), {
195
+ bootstrapScriptContent,
196
+ });
124
197
  let response = prelude.pipeThrough(injectRSCPayload(injectStream));
125
198
 
126
199
  return [
@@ -162,7 +235,9 @@ async function loadBundleUncached(
162
235
  ) => Async<{|contents: Blob|}>,
163
236
  ) {
164
237
  // Load all asset contents.
165
- let queue = new PromiseQueue({maxConcurrent: 32});
238
+ let queue = new PromiseQueue<Array<[string, [Asset, string]]>>({
239
+ maxConcurrent: 32,
240
+ });
166
241
  bundle.traverse(node => {
167
242
  if (node.type === 'dependency') {
168
243
  let dep = node.value;
@@ -182,18 +257,32 @@ async function loadBundleUncached(
182
257
  let contents = await blobToString(packagedBundle.contents);
183
258
  contents = `module.exports = ${contents}`;
184
259
  return [
185
- entryBundle.id,
186
- [nullthrows(entryBundle.getMainEntry()), contents],
260
+ [
261
+ entryBundle.id,
262
+ [nullthrows(entryBundle.getMainEntry()), contents],
263
+ ],
187
264
  ];
188
265
  });
266
+ } else if (entryBundle) {
267
+ // console.log('here', entryBundle)
268
+ queue.add(async () => {
269
+ let {assets: subAssets} = await loadBundle(
270
+ entryBundle,
271
+ bundleGraph,
272
+ getInlineBundleContents,
273
+ );
274
+ return Array.from(subAssets);
275
+ });
189
276
  }
190
277
  } else if (node.type === 'asset') {
191
278
  let asset = node.value;
192
- queue.add(async () => [asset.id, [asset, await asset.getCode()]]);
279
+ queue.add(async () => [[asset.id, [asset, await asset.getCode()]]]);
193
280
  }
194
281
  });
195
282
 
196
- let assets = new Map<string, [Asset, string]>(await queue.run());
283
+ let assets = new Map<string, [Asset, string]>(
284
+ (await queue.run()).flatMap(v => v),
285
+ );
197
286
  let assetsByFilePath = new Map<string, string>();
198
287
  let assetsByPublicId = new Map<string, string>();
199
288
  for (let [asset] of assets.values()) {
@@ -251,10 +340,14 @@ async function loadBundleUncached(
251
340
  id = resolution.specifier;
252
341
  }
253
342
 
343
+ if (id.startsWith('.')) {
344
+ // Another bundle. Should already be loaded.
345
+ return {};
346
+ }
347
+
254
348
  return defaultRequire(id);
255
349
  };
256
350
 
257
- // @ts-ignore
258
351
  require.resolve = defaultRequire.resolve;
259
352
 
260
353
  return runModule(code, asset.filePath, cacheKey, require, parcelRequire);
@@ -264,13 +357,13 @@ async function loadBundleUncached(
264
357
  return loadAsset(nullthrows(assetsByPublicId.get(publicId)));
265
358
  };
266
359
 
267
- // @ts-ignore
360
+ parcelRequire.root = parcelRequire;
361
+
268
362
  parcelRequire.meta = {
269
363
  distDir: bundle.target.distDir,
270
364
  publicUrl: bundle.target.publicUrl,
271
365
  };
272
366
 
273
- // @ts-ignore
274
367
  parcelRequire.load = async (filePath: string) => {
275
368
  let bundle = bundleGraph.getBundles().find(b => b.name === filePath);
276
369
  if (bundle) {
@@ -327,7 +420,6 @@ async function loadBundleUncached(
327
420
  return loadModule(id, filePath, env);
328
421
  };
329
422
 
330
- // @ts-ignore
331
423
  require.resolve = defaultRequire.resolve;
332
424
 
333
425
  return runModule(code, filePath, cacheKey, require, parcelRequire);
@@ -346,6 +438,7 @@ function runModule(
346
438
  require: (id: string) => any,
347
439
  parcelRequire: (id: string) => any,
348
440
  ) {
441
+ // code = code.replace(/import\((['"].*?['"])\)/g, (_, m) => `parcelRequire.load(${m[0] + m.slice(3)})`);
349
442
  let moduleFunction = vm.compileFunction(
350
443
  code,
351
444
  [
@@ -369,6 +462,7 @@ function runModule(
369
462
  filename,
370
463
  id,
371
464
  path: dirname,
465
+ bundle: parcelRequire,
372
466
  };
373
467
 
374
468
  moduleCache.set(id, module);