main
js 22 lines 786 Bytes
Raw
1 'use strict';
2
3 const url = require('url');
4
5 // Webpack loader that runs in the RSC compilation.
6 // When a module starts with 'use client', it replaces the entire source
7 // with a client module proxy. This makes the RSC renderer serialize a
8 // client reference into the Flight stream instead of rendering the component.
9 module.exports = function rscClientRefLoader(source) {
10 const trimmed = source.trimStart();
11 if (
12 trimmed.startsWith("'use client'") ||
13 trimmed.startsWith('"use client"')
14 ) {
15 const href = url.pathToFileURL(this.resourcePath).href;
16 return [
17 `const { createClientModuleProxy } = require('react-server-dom-webpack/server');`,
18 `module.exports = createClientModuleProxy(${JSON.stringify(href)});`,
19 ].join('\n');
20 }
21 return source;
22 };