@parcel/packager-react-static 2.13.4-canary.3388
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/LICENSE +21 -0
- package/lib/ReactStaticPackager.js +364 -0
- package/package.json +31 -0
- package/src/ReactStaticPackager.js +407 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present Devon Govett
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
function _utils() {
|
|
8
|
+
const data = require("@parcel/utils");
|
|
9
|
+
_utils = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _fs() {
|
|
15
|
+
const data = _interopRequireDefault(require("fs"));
|
|
16
|
+
_fs = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _module() {
|
|
22
|
+
const data = _interopRequireDefault(require("module"));
|
|
23
|
+
_module = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _plugin() {
|
|
29
|
+
const data = require("@parcel/plugin");
|
|
30
|
+
_plugin = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function _path() {
|
|
36
|
+
const data = _interopRequireDefault(require("path"));
|
|
37
|
+
_path = function () {
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
function _nodeResolverCore() {
|
|
43
|
+
const data = require("@parcel/node-resolver-core");
|
|
44
|
+
_nodeResolverCore = function () {
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
function _vm() {
|
|
50
|
+
const data = _interopRequireDefault(require("vm"));
|
|
51
|
+
_vm = function () {
|
|
52
|
+
return data;
|
|
53
|
+
};
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
function _nullthrows() {
|
|
57
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
58
|
+
_nullthrows = function () {
|
|
59
|
+
return data;
|
|
60
|
+
};
|
|
61
|
+
return data;
|
|
62
|
+
}
|
|
63
|
+
function _stream() {
|
|
64
|
+
const data = require("stream");
|
|
65
|
+
_stream = function () {
|
|
66
|
+
return data;
|
|
67
|
+
};
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
70
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
71
|
+
let clientResolver;
|
|
72
|
+
let serverResolver;
|
|
73
|
+
let packagingBundles = new Map();
|
|
74
|
+
let moduleCache = new Map();
|
|
75
|
+
let loadedBundles = new Map();
|
|
76
|
+
var _default = exports.default = new (_plugin().Packager)({
|
|
77
|
+
loadConfig({
|
|
78
|
+
options,
|
|
79
|
+
config
|
|
80
|
+
}) {
|
|
81
|
+
config.invalidateOnBuild();
|
|
82
|
+
packagingBundles.clear();
|
|
83
|
+
moduleCache.clear();
|
|
84
|
+
loadedBundles.clear();
|
|
85
|
+
clientResolver = new (_nodeResolverCore().ResolverBase)(options.projectRoot, {
|
|
86
|
+
mode: 2,
|
|
87
|
+
packageExports: true
|
|
88
|
+
});
|
|
89
|
+
serverResolver = new (_nodeResolverCore().ResolverBase)(options.projectRoot, {
|
|
90
|
+
mode: 2,
|
|
91
|
+
packageExports: true,
|
|
92
|
+
conditions: 1 << 16 // "react-server"
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
async package({
|
|
96
|
+
bundle,
|
|
97
|
+
bundleGraph,
|
|
98
|
+
getInlineBundleContents
|
|
99
|
+
}) {
|
|
100
|
+
if (bundle.env.shouldScopeHoist) {
|
|
101
|
+
throw new Error('Scope hoisting is not supported with SSG');
|
|
102
|
+
}
|
|
103
|
+
let {
|
|
104
|
+
load,
|
|
105
|
+
loadModule
|
|
106
|
+
} = await loadBundle(bundle, bundleGraph, getInlineBundleContents);
|
|
107
|
+
let Component = load((0, _nullthrows().default)(bundle.getMainEntry()).id).default;
|
|
108
|
+
let {
|
|
109
|
+
renderToReadableStream
|
|
110
|
+
} = loadModule('react-server-dom-parcel/server.edge', __filename, 'react-server');
|
|
111
|
+
let {
|
|
112
|
+
prerender
|
|
113
|
+
} = loadModule('react-dom/static.edge', __filename, 'react-client');
|
|
114
|
+
let React = loadModule('react', __filename, 'react-client');
|
|
115
|
+
let {
|
|
116
|
+
createFromReadableStream
|
|
117
|
+
} = loadModule('react-server-dom-parcel/client.edge', __filename, 'react-client');
|
|
118
|
+
let {
|
|
119
|
+
injectRSCPayload
|
|
120
|
+
} = await import('rsc-html-stream/server');
|
|
121
|
+
let pages = [];
|
|
122
|
+
for (let b of bundleGraph.getEntryBundles()) {
|
|
123
|
+
let main = b.getMainEntry();
|
|
124
|
+
if (main && b.type === 'js' && b.needsStableName) {
|
|
125
|
+
pages.push({
|
|
126
|
+
url: (0, _utils().urlJoin)(b.target.publicUrl, b.name),
|
|
127
|
+
name: b.name,
|
|
128
|
+
meta: pageMeta(main.meta)
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
let props = {
|
|
133
|
+
pages,
|
|
134
|
+
currentPage: {
|
|
135
|
+
url: (0, _utils().urlJoin)(bundle.target.publicUrl, bundle.name),
|
|
136
|
+
name: bundle.name,
|
|
137
|
+
meta: pageMeta((0, _nullthrows().default)(bundle.getMainEntry()).meta)
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
let stream = renderToReadableStream(React.createElement(Component, props));
|
|
141
|
+
let [s1, renderStream] = stream.tee();
|
|
142
|
+
let [injectStream, rscStream] = s1.tee();
|
|
143
|
+
let data = createFromReadableStream(renderStream);
|
|
144
|
+
let {
|
|
145
|
+
prelude
|
|
146
|
+
} = await prerender(React.createElement(function () {
|
|
147
|
+
return React.use(data);
|
|
148
|
+
}));
|
|
149
|
+
let response = prelude.pipeThrough(injectRSCPayload(injectStream));
|
|
150
|
+
return [{
|
|
151
|
+
type: 'html',
|
|
152
|
+
contents: _stream().Readable.from(response)
|
|
153
|
+
}, {
|
|
154
|
+
type: 'rsc',
|
|
155
|
+
contents: _stream().Readable.from(rscStream)
|
|
156
|
+
}];
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
function loadBundle(bundle, bundleGraph, getInlineBundleContents) {
|
|
160
|
+
let cached = loadedBundles.get(bundle);
|
|
161
|
+
if (!cached) {
|
|
162
|
+
cached = loadBundleUncached(bundle, bundleGraph, getInlineBundleContents);
|
|
163
|
+
loadedBundles.set(bundle, cached);
|
|
164
|
+
}
|
|
165
|
+
return cached;
|
|
166
|
+
}
|
|
167
|
+
async function loadBundleUncached(bundle, bundleGraph, getInlineBundleContents) {
|
|
168
|
+
// Load all asset contents.
|
|
169
|
+
let queue = new (_utils().PromiseQueue)({
|
|
170
|
+
maxConcurrent: 32
|
|
171
|
+
});
|
|
172
|
+
bundle.traverse(node => {
|
|
173
|
+
if (node.type === 'dependency') {
|
|
174
|
+
let dep = node.value;
|
|
175
|
+
let entryBundle = bundleGraph.getReferencedBundle(dep, bundle);
|
|
176
|
+
if ((entryBundle === null || entryBundle === void 0 ? void 0 : entryBundle.bundleBehavior) === 'inline') {
|
|
177
|
+
queue.add(async () => {
|
|
178
|
+
if (!packagingBundles.has(entryBundle)) {
|
|
179
|
+
packagingBundles.set(entryBundle, getInlineBundleContents(entryBundle, bundleGraph));
|
|
180
|
+
}
|
|
181
|
+
let packagedBundle = await (0, _nullthrows().default)(packagingBundles.get(entryBundle));
|
|
182
|
+
let contents = await (0, _utils().blobToString)(packagedBundle.contents);
|
|
183
|
+
contents = `module.exports = ${contents}`;
|
|
184
|
+
return [entryBundle.id, [(0, _nullthrows().default)(entryBundle.getMainEntry()), contents]];
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
} else if (node.type === 'asset') {
|
|
188
|
+
let asset = node.value;
|
|
189
|
+
queue.add(async () => [asset.id, [asset, await asset.getCode()]]);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
let assets = new Map(await queue.run());
|
|
193
|
+
let assetsByFilePath = new Map();
|
|
194
|
+
let assetsByPublicId = new Map();
|
|
195
|
+
for (let [asset] of assets.values()) {
|
|
196
|
+
assetsByFilePath.set(getCacheKey(asset), asset.id);
|
|
197
|
+
assetsByPublicId.set(bundleGraph.getAssetPublicId(asset), asset.id);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Load an asset into the module system by id.
|
|
201
|
+
let loadAsset = id => {
|
|
202
|
+
let [asset, code] = (0, _nullthrows().default)(assets.get(id));
|
|
203
|
+
let cacheKey = getCacheKey(asset);
|
|
204
|
+
let cachedModule = moduleCache.get(cacheKey);
|
|
205
|
+
if (cachedModule) {
|
|
206
|
+
return cachedModule.exports;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Build a mapping of dependencies to their resolved assets.
|
|
210
|
+
let deps = new Map();
|
|
211
|
+
for (let dep of bundleGraph.getDependencies(asset)) {
|
|
212
|
+
if (bundleGraph.isDependencySkipped(dep)) {
|
|
213
|
+
deps.set(getSpecifier(dep), {
|
|
214
|
+
skipped: true
|
|
215
|
+
});
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
let entryBundle = bundleGraph.getReferencedBundle(dep, bundle);
|
|
219
|
+
if ((entryBundle === null || entryBundle === void 0 ? void 0 : entryBundle.bundleBehavior) === 'inline') {
|
|
220
|
+
deps.set(getSpecifier(dep), {
|
|
221
|
+
id: entryBundle.id
|
|
222
|
+
});
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
let resolved = bundleGraph.getResolvedAsset(dep, bundle);
|
|
226
|
+
if (resolved) {
|
|
227
|
+
if (resolved.type !== 'js') {
|
|
228
|
+
deps.set(getSpecifier(dep), {
|
|
229
|
+
skipped: true
|
|
230
|
+
});
|
|
231
|
+
} else {
|
|
232
|
+
deps.set(getSpecifier(dep), {
|
|
233
|
+
id: resolved.id
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
} else {
|
|
237
|
+
deps.set(getSpecifier(dep), {
|
|
238
|
+
specifier: dep.specifier
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
let defaultRequire = _module().default.createRequire(asset.filePath);
|
|
243
|
+
let require = id => {
|
|
244
|
+
let resolution = deps.get(id);
|
|
245
|
+
if (resolution !== null && resolution !== void 0 && resolution.skipped) {
|
|
246
|
+
return {};
|
|
247
|
+
}
|
|
248
|
+
if (resolution !== null && resolution !== void 0 && resolution.id) {
|
|
249
|
+
return loadAsset(resolution.id);
|
|
250
|
+
}
|
|
251
|
+
if (resolution !== null && resolution !== void 0 && resolution.specifier) {
|
|
252
|
+
id = resolution.specifier;
|
|
253
|
+
}
|
|
254
|
+
return defaultRequire(id);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// @ts-ignore
|
|
258
|
+
require.resolve = defaultRequire.resolve;
|
|
259
|
+
return runModule(code, asset.filePath, cacheKey, require, parcelRequire);
|
|
260
|
+
};
|
|
261
|
+
let parcelRequire = publicId => {
|
|
262
|
+
return loadAsset((0, _nullthrows().default)(assetsByPublicId.get(publicId)));
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// @ts-ignore
|
|
266
|
+
parcelRequire.meta = {
|
|
267
|
+
distDir: bundle.target.distDir,
|
|
268
|
+
publicUrl: bundle.target.publicUrl
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// @ts-ignore
|
|
272
|
+
parcelRequire.load = async filePath => {
|
|
273
|
+
let bundle = bundleGraph.getBundles().find(b => b.name === filePath);
|
|
274
|
+
if (bundle) {
|
|
275
|
+
let {
|
|
276
|
+
assets: subAssets
|
|
277
|
+
} = await loadBundle(bundle, bundleGraph, getInlineBundleContents);
|
|
278
|
+
for (let [id, [asset, code]] of subAssets) {
|
|
279
|
+
if (!assets.has(id)) {
|
|
280
|
+
assets.set(id, [asset, code]);
|
|
281
|
+
assetsByFilePath.set(getCacheKey(asset), asset.id);
|
|
282
|
+
assetsByPublicId.set(bundleGraph.getAssetPublicId(asset), asset.id);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
} else {
|
|
286
|
+
throw new Error('Bundle not found');
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// Resolve and load a module by specifier.
|
|
291
|
+
let loadModule = (id, from, env = 'react-client') => {
|
|
292
|
+
let resolver = env === 'react-server' ? serverResolver : clientResolver;
|
|
293
|
+
let res = resolver.resolve({
|
|
294
|
+
filename: id,
|
|
295
|
+
specifierType: 'commonjs',
|
|
296
|
+
parent: from
|
|
297
|
+
});
|
|
298
|
+
if (res.error) {
|
|
299
|
+
throw new Error(`Could not resolve module "${id}" from "${from}"`);
|
|
300
|
+
}
|
|
301
|
+
let defaultRequire = _module().default.createRequire(from);
|
|
302
|
+
if (res.resolution.type === 'Builtin') {
|
|
303
|
+
return defaultRequire(res.resolution.value);
|
|
304
|
+
}
|
|
305
|
+
if (res.resolution.type === 'Path') {
|
|
306
|
+
let cacheKey = res.resolution.value + '#' + env;
|
|
307
|
+
const cachedModule = moduleCache.get(cacheKey);
|
|
308
|
+
if (cachedModule) {
|
|
309
|
+
return cachedModule.exports;
|
|
310
|
+
}
|
|
311
|
+
let assetId = assetsByFilePath.get(cacheKey);
|
|
312
|
+
if (assetId) {
|
|
313
|
+
return loadAsset(assetId);
|
|
314
|
+
}
|
|
315
|
+
let filePath = (0, _nullthrows().default)(res.resolution.value);
|
|
316
|
+
let code = _fs().default.readFileSync(filePath, 'utf8');
|
|
317
|
+
let require = id => {
|
|
318
|
+
return loadModule(id, filePath, env);
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// @ts-ignore
|
|
322
|
+
require.resolve = defaultRequire.resolve;
|
|
323
|
+
return runModule(code, filePath, cacheKey, require, parcelRequire);
|
|
324
|
+
}
|
|
325
|
+
throw new Error('Unknown resolution');
|
|
326
|
+
};
|
|
327
|
+
return {
|
|
328
|
+
load: loadAsset,
|
|
329
|
+
loadModule,
|
|
330
|
+
assets
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function runModule(code, filename, id, require, parcelRequire) {
|
|
334
|
+
let moduleFunction = _vm().default.compileFunction(code, ['exports', 'require', 'module', '__dirname', '__filename', 'parcelRequire'], {
|
|
335
|
+
filename
|
|
336
|
+
});
|
|
337
|
+
let dirname = _path().default.dirname(filename);
|
|
338
|
+
let module = {
|
|
339
|
+
exports: {},
|
|
340
|
+
require,
|
|
341
|
+
children: [],
|
|
342
|
+
filename,
|
|
343
|
+
id,
|
|
344
|
+
path: dirname
|
|
345
|
+
};
|
|
346
|
+
moduleCache.set(id, module);
|
|
347
|
+
moduleFunction(module.exports, require, module, dirname, filename, parcelRequire);
|
|
348
|
+
return module.exports;
|
|
349
|
+
}
|
|
350
|
+
function getCacheKey(asset) {
|
|
351
|
+
return asset.filePath + '#' + asset.env.context;
|
|
352
|
+
}
|
|
353
|
+
function getSpecifier(dep) {
|
|
354
|
+
if (typeof dep.meta.placeholder === 'string') {
|
|
355
|
+
return dep.meta.placeholder;
|
|
356
|
+
}
|
|
357
|
+
return dep.specifier;
|
|
358
|
+
}
|
|
359
|
+
function pageMeta(meta) {
|
|
360
|
+
if (meta.ssgMeta && typeof (meta === null || meta === void 0 ? void 0 : meta.ssgMeta) === 'object' && !Array.isArray(meta.ssgMeta)) {
|
|
361
|
+
return meta.ssgMeta;
|
|
362
|
+
}
|
|
363
|
+
return {};
|
|
364
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@parcel/packager-react-static",
|
|
3
|
+
"version": "2.13.4-canary.3388+393f497bd",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"funding": {
|
|
9
|
+
"type": "opencollective",
|
|
10
|
+
"url": "https://opencollective.com/parcel"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/parcel-bundler/parcel.git"
|
|
15
|
+
},
|
|
16
|
+
"main": "lib/ReactStaticPackager.js",
|
|
17
|
+
"source": "src/ReactStaticPackager.js",
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">= 16.0.0",
|
|
20
|
+
"parcel": "^2.0.0-canary.1763+393f497bd"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@parcel/node-resolver-core": "3.4.4-canary.3388+393f497bd",
|
|
24
|
+
"@parcel/plugin": "2.0.0-canary.1765+393f497bd",
|
|
25
|
+
"@parcel/types": "2.0.0-canary.1765+393f497bd",
|
|
26
|
+
"@parcel/utils": "2.0.0-canary.1765+393f497bd",
|
|
27
|
+
"nullthrows": "^1.1.1",
|
|
28
|
+
"rsc-html-stream": "^0.0.4"
|
|
29
|
+
},
|
|
30
|
+
"gitHead": "393f497bd438d2c06b27d3ca59e5d5aa5eaeaf5b"
|
|
31
|
+
}
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import type {
|
|
3
|
+
Asset,
|
|
4
|
+
Async,
|
|
5
|
+
Blob,
|
|
6
|
+
BundleGraph,
|
|
7
|
+
Dependency,
|
|
8
|
+
NamedBundle,
|
|
9
|
+
Meta,
|
|
10
|
+
JSONObject,
|
|
11
|
+
} from '@parcel/types';
|
|
12
|
+
import {blobToString, PromiseQueue, urlJoin} from '@parcel/utils';
|
|
13
|
+
import fs from 'fs';
|
|
14
|
+
import Module from 'module';
|
|
15
|
+
import {Packager} from '@parcel/plugin';
|
|
16
|
+
import path from 'path';
|
|
17
|
+
import {ResolverBase} from '@parcel/node-resolver-core';
|
|
18
|
+
import vm from 'vm';
|
|
19
|
+
import nullthrows from 'nullthrows';
|
|
20
|
+
import {Readable} from 'stream';
|
|
21
|
+
|
|
22
|
+
export interface Page {
|
|
23
|
+
url: string;
|
|
24
|
+
name: string;
|
|
25
|
+
meta: any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface PageProps {
|
|
29
|
+
pages: Page[];
|
|
30
|
+
currentPage: Page;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface ParcelModule {
|
|
34
|
+
exports: any;
|
|
35
|
+
children: ParcelModule[];
|
|
36
|
+
filename: string;
|
|
37
|
+
id: string;
|
|
38
|
+
path: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let clientResolver: ResolverBase;
|
|
42
|
+
let serverResolver: ResolverBase;
|
|
43
|
+
let packagingBundles = new Map<NamedBundle, Async<{|contents: Blob|}>>();
|
|
44
|
+
let moduleCache = new Map<string, ParcelModule>();
|
|
45
|
+
let loadedBundles = new Map<NamedBundle, any>();
|
|
46
|
+
|
|
47
|
+
export default (new Packager({
|
|
48
|
+
loadConfig({options, config}) {
|
|
49
|
+
config.invalidateOnBuild();
|
|
50
|
+
packagingBundles.clear();
|
|
51
|
+
moduleCache.clear();
|
|
52
|
+
loadedBundles.clear();
|
|
53
|
+
clientResolver = new ResolverBase(options.projectRoot, {
|
|
54
|
+
mode: 2,
|
|
55
|
+
packageExports: true,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
serverResolver = new ResolverBase(options.projectRoot, {
|
|
59
|
+
mode: 2,
|
|
60
|
+
packageExports: true,
|
|
61
|
+
conditions: 1 << 16, // "react-server"
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
async package({bundle, bundleGraph, getInlineBundleContents}) {
|
|
65
|
+
if (bundle.env.shouldScopeHoist) {
|
|
66
|
+
throw new Error('Scope hoisting is not supported with SSG');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let {load, loadModule} = await loadBundle(
|
|
70
|
+
bundle,
|
|
71
|
+
bundleGraph,
|
|
72
|
+
getInlineBundleContents,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
let Component = load(nullthrows(bundle.getMainEntry()).id).default;
|
|
76
|
+
let {renderToReadableStream} = loadModule(
|
|
77
|
+
'react-server-dom-parcel/server.edge',
|
|
78
|
+
__filename,
|
|
79
|
+
'react-server',
|
|
80
|
+
);
|
|
81
|
+
let {prerender} = loadModule(
|
|
82
|
+
'react-dom/static.edge',
|
|
83
|
+
__filename,
|
|
84
|
+
'react-client',
|
|
85
|
+
);
|
|
86
|
+
let React = loadModule('react', __filename, 'react-client');
|
|
87
|
+
let {createFromReadableStream} = loadModule(
|
|
88
|
+
'react-server-dom-parcel/client.edge',
|
|
89
|
+
__filename,
|
|
90
|
+
'react-client',
|
|
91
|
+
);
|
|
92
|
+
let {injectRSCPayload} = await import('rsc-html-stream/server');
|
|
93
|
+
|
|
94
|
+
let pages: Page[] = [];
|
|
95
|
+
for (let b of bundleGraph.getEntryBundles()) {
|
|
96
|
+
let main = b.getMainEntry();
|
|
97
|
+
if (main && b.type === 'js' && b.needsStableName) {
|
|
98
|
+
pages.push({
|
|
99
|
+
url: urlJoin(b.target.publicUrl, b.name),
|
|
100
|
+
name: b.name,
|
|
101
|
+
meta: pageMeta(main.meta),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let props: PageProps = {
|
|
107
|
+
pages,
|
|
108
|
+
currentPage: {
|
|
109
|
+
url: urlJoin(bundle.target.publicUrl, bundle.name),
|
|
110
|
+
name: bundle.name,
|
|
111
|
+
meta: pageMeta(nullthrows(bundle.getMainEntry()).meta),
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
let stream = renderToReadableStream(React.createElement(Component, props));
|
|
116
|
+
let [s1, renderStream] = stream.tee();
|
|
117
|
+
let [injectStream, rscStream] = s1.tee();
|
|
118
|
+
let data = createFromReadableStream(renderStream);
|
|
119
|
+
function Content() {
|
|
120
|
+
return React.use(data);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let {prelude} = await prerender(React.createElement(Content));
|
|
124
|
+
let response = prelude.pipeThrough(injectRSCPayload(injectStream));
|
|
125
|
+
|
|
126
|
+
return [
|
|
127
|
+
{
|
|
128
|
+
type: 'html',
|
|
129
|
+
contents: Readable.from(response),
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
type: 'rsc',
|
|
133
|
+
contents: Readable.from(rscStream),
|
|
134
|
+
},
|
|
135
|
+
];
|
|
136
|
+
},
|
|
137
|
+
}): Packager);
|
|
138
|
+
|
|
139
|
+
function loadBundle(
|
|
140
|
+
bundle: NamedBundle,
|
|
141
|
+
bundleGraph: BundleGraph<NamedBundle>,
|
|
142
|
+
getInlineBundleContents: (
|
|
143
|
+
bundle: NamedBundle,
|
|
144
|
+
bundleGraph: BundleGraph<NamedBundle>,
|
|
145
|
+
) => Async<{|contents: Blob|}>,
|
|
146
|
+
) {
|
|
147
|
+
let cached = loadedBundles.get(bundle);
|
|
148
|
+
if (!cached) {
|
|
149
|
+
cached = loadBundleUncached(bundle, bundleGraph, getInlineBundleContents);
|
|
150
|
+
loadedBundles.set(bundle, cached);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return cached;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function loadBundleUncached(
|
|
157
|
+
bundle: NamedBundle,
|
|
158
|
+
bundleGraph: BundleGraph<NamedBundle>,
|
|
159
|
+
getInlineBundleContents: (
|
|
160
|
+
bundle: NamedBundle,
|
|
161
|
+
bundleGraph: BundleGraph<NamedBundle>,
|
|
162
|
+
) => Async<{|contents: Blob|}>,
|
|
163
|
+
) {
|
|
164
|
+
// Load all asset contents.
|
|
165
|
+
let queue = new PromiseQueue({maxConcurrent: 32});
|
|
166
|
+
bundle.traverse(node => {
|
|
167
|
+
if (node.type === 'dependency') {
|
|
168
|
+
let dep = node.value;
|
|
169
|
+
let entryBundle = bundleGraph.getReferencedBundle(dep, bundle);
|
|
170
|
+
if (entryBundle?.bundleBehavior === 'inline') {
|
|
171
|
+
queue.add(async () => {
|
|
172
|
+
if (!packagingBundles.has(entryBundle)) {
|
|
173
|
+
packagingBundles.set(
|
|
174
|
+
entryBundle,
|
|
175
|
+
getInlineBundleContents(entryBundle, bundleGraph),
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
let packagedBundle = await nullthrows(
|
|
180
|
+
packagingBundles.get(entryBundle),
|
|
181
|
+
);
|
|
182
|
+
let contents = await blobToString(packagedBundle.contents);
|
|
183
|
+
contents = `module.exports = ${contents}`;
|
|
184
|
+
return [
|
|
185
|
+
entryBundle.id,
|
|
186
|
+
[nullthrows(entryBundle.getMainEntry()), contents],
|
|
187
|
+
];
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
} else if (node.type === 'asset') {
|
|
191
|
+
let asset = node.value;
|
|
192
|
+
queue.add(async () => [asset.id, [asset, await asset.getCode()]]);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
let assets = new Map<string, [Asset, string]>(await queue.run());
|
|
197
|
+
let assetsByFilePath = new Map<string, string>();
|
|
198
|
+
let assetsByPublicId = new Map<string, string>();
|
|
199
|
+
for (let [asset] of assets.values()) {
|
|
200
|
+
assetsByFilePath.set(getCacheKey(asset), asset.id);
|
|
201
|
+
assetsByPublicId.set(bundleGraph.getAssetPublicId(asset), asset.id);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Load an asset into the module system by id.
|
|
205
|
+
let loadAsset = (id: string) => {
|
|
206
|
+
let [asset, code] = nullthrows(assets.get(id));
|
|
207
|
+
let cacheKey = getCacheKey(asset);
|
|
208
|
+
let cachedModule = moduleCache.get(cacheKey);
|
|
209
|
+
if (cachedModule) {
|
|
210
|
+
return cachedModule.exports;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Build a mapping of dependencies to their resolved assets.
|
|
214
|
+
let deps = new Map();
|
|
215
|
+
for (let dep of bundleGraph.getDependencies(asset)) {
|
|
216
|
+
if (bundleGraph.isDependencySkipped(dep)) {
|
|
217
|
+
deps.set(getSpecifier(dep), {skipped: true});
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
let entryBundle = bundleGraph.getReferencedBundle(dep, bundle);
|
|
222
|
+
if (entryBundle?.bundleBehavior === 'inline') {
|
|
223
|
+
deps.set(getSpecifier(dep), {id: entryBundle.id});
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let resolved = bundleGraph.getResolvedAsset(dep, bundle);
|
|
228
|
+
if (resolved) {
|
|
229
|
+
if (resolved.type !== 'js') {
|
|
230
|
+
deps.set(getSpecifier(dep), {skipped: true});
|
|
231
|
+
} else {
|
|
232
|
+
deps.set(getSpecifier(dep), {id: resolved.id});
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
deps.set(getSpecifier(dep), {specifier: dep.specifier});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let defaultRequire = Module.createRequire(asset.filePath);
|
|
240
|
+
let require = (id: string) => {
|
|
241
|
+
let resolution = deps.get(id);
|
|
242
|
+
if (resolution?.skipped) {
|
|
243
|
+
return {};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (resolution?.id) {
|
|
247
|
+
return loadAsset(resolution.id);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (resolution?.specifier) {
|
|
251
|
+
id = resolution.specifier;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return defaultRequire(id);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// @ts-ignore
|
|
258
|
+
require.resolve = defaultRequire.resolve;
|
|
259
|
+
|
|
260
|
+
return runModule(code, asset.filePath, cacheKey, require, parcelRequire);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
let parcelRequire = (publicId: string) => {
|
|
264
|
+
return loadAsset(nullthrows(assetsByPublicId.get(publicId)));
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// @ts-ignore
|
|
268
|
+
parcelRequire.meta = {
|
|
269
|
+
distDir: bundle.target.distDir,
|
|
270
|
+
publicUrl: bundle.target.publicUrl,
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// @ts-ignore
|
|
274
|
+
parcelRequire.load = async (filePath: string) => {
|
|
275
|
+
let bundle = bundleGraph.getBundles().find(b => b.name === filePath);
|
|
276
|
+
if (bundle) {
|
|
277
|
+
let {assets: subAssets} = await loadBundle(
|
|
278
|
+
bundle,
|
|
279
|
+
bundleGraph,
|
|
280
|
+
getInlineBundleContents,
|
|
281
|
+
);
|
|
282
|
+
for (let [id, [asset, code]] of subAssets) {
|
|
283
|
+
if (!assets.has(id)) {
|
|
284
|
+
assets.set(id, [asset, code]);
|
|
285
|
+
assetsByFilePath.set(getCacheKey(asset), asset.id);
|
|
286
|
+
assetsByPublicId.set(bundleGraph.getAssetPublicId(asset), asset.id);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
} else {
|
|
290
|
+
throw new Error('Bundle not found');
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
// Resolve and load a module by specifier.
|
|
295
|
+
let loadModule = (id: string, from: string, env = 'react-client') => {
|
|
296
|
+
let resolver = env === 'react-server' ? serverResolver : clientResolver;
|
|
297
|
+
let res = resolver.resolve({
|
|
298
|
+
filename: id,
|
|
299
|
+
specifierType: 'commonjs',
|
|
300
|
+
parent: from,
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
if (res.error) {
|
|
304
|
+
throw new Error(`Could not resolve module "${id}" from "${from}"`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
let defaultRequire = Module.createRequire(from);
|
|
308
|
+
if (res.resolution.type === 'Builtin') {
|
|
309
|
+
return defaultRequire(res.resolution.value);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (res.resolution.type === 'Path') {
|
|
313
|
+
let cacheKey = res.resolution.value + '#' + env;
|
|
314
|
+
const cachedModule = moduleCache.get(cacheKey);
|
|
315
|
+
if (cachedModule) {
|
|
316
|
+
return cachedModule.exports;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
let assetId = assetsByFilePath.get(cacheKey);
|
|
320
|
+
if (assetId) {
|
|
321
|
+
return loadAsset(assetId);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
let filePath = nullthrows(res.resolution.value);
|
|
325
|
+
let code = fs.readFileSync(filePath, 'utf8');
|
|
326
|
+
let require = (id: string) => {
|
|
327
|
+
return loadModule(id, filePath, env);
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
// @ts-ignore
|
|
331
|
+
require.resolve = defaultRequire.resolve;
|
|
332
|
+
|
|
333
|
+
return runModule(code, filePath, cacheKey, require, parcelRequire);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
throw new Error('Unknown resolution');
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
return {load: loadAsset, loadModule, assets};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function runModule(
|
|
343
|
+
code: string,
|
|
344
|
+
filename: string,
|
|
345
|
+
id: string,
|
|
346
|
+
require: (id: string) => any,
|
|
347
|
+
parcelRequire: (id: string) => any,
|
|
348
|
+
) {
|
|
349
|
+
let moduleFunction = vm.compileFunction(
|
|
350
|
+
code,
|
|
351
|
+
[
|
|
352
|
+
'exports',
|
|
353
|
+
'require',
|
|
354
|
+
'module',
|
|
355
|
+
'__dirname',
|
|
356
|
+
'__filename',
|
|
357
|
+
'parcelRequire',
|
|
358
|
+
],
|
|
359
|
+
{
|
|
360
|
+
filename,
|
|
361
|
+
},
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
let dirname = path.dirname(filename);
|
|
365
|
+
let module = {
|
|
366
|
+
exports: {},
|
|
367
|
+
require,
|
|
368
|
+
children: [],
|
|
369
|
+
filename,
|
|
370
|
+
id,
|
|
371
|
+
path: dirname,
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
moduleCache.set(id, module);
|
|
375
|
+
moduleFunction(
|
|
376
|
+
module.exports,
|
|
377
|
+
require,
|
|
378
|
+
module,
|
|
379
|
+
dirname,
|
|
380
|
+
filename,
|
|
381
|
+
parcelRequire,
|
|
382
|
+
);
|
|
383
|
+
return module.exports;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function getCacheKey(asset: Asset) {
|
|
387
|
+
return asset.filePath + '#' + asset.env.context;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function getSpecifier(dep: Dependency) {
|
|
391
|
+
if (typeof dep.meta.placeholder === 'string') {
|
|
392
|
+
return dep.meta.placeholder;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return dep.specifier;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function pageMeta(meta: Meta): JSONObject {
|
|
399
|
+
if (
|
|
400
|
+
meta.ssgMeta &&
|
|
401
|
+
typeof meta?.ssgMeta === 'object' &&
|
|
402
|
+
!Array.isArray(meta.ssgMeta)
|
|
403
|
+
) {
|
|
404
|
+
return meta.ssgMeta;
|
|
405
|
+
}
|
|
406
|
+
return {};
|
|
407
|
+
}
|