-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add client loader to enable importing server actions
Made easier by facebook/react#26632
- Loading branch information
1 parent
0b92a75
commit dabd846
Showing
9 changed files
with
389 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
import {createRequire} from 'module'; | ||
import type {RuleSetUseItem} from 'webpack'; | ||
import type {WebpackRscClientLoaderOptions} from './webpack-rsc-client-loader.cjs'; | ||
import type {WebpackRscServerLoaderOptions} from './webpack-rsc-server-loader.cjs'; | ||
|
||
export * from './webpack-rsc-client-loader.cjs'; | ||
export * from './webpack-rsc-client-plugin.js'; | ||
export * from './webpack-rsc-server-loader.cjs'; | ||
export * from './webpack-rsc-server-plugin.js'; | ||
|
||
const require = createRequire(import.meta.url); | ||
const loader = require.resolve(`./webpack-rsc-server-loader.cjs`); | ||
const serverLoader = require.resolve(`./webpack-rsc-server-loader.cjs`); | ||
const clientLoader = require.resolve(`./webpack-rsc-client-loader.cjs`); | ||
|
||
export function createWebpackRscServerLoader( | ||
options: WebpackRscServerLoaderOptions, | ||
): RuleSetUseItem { | ||
return {loader, options}; | ||
return {loader: serverLoader, options}; | ||
} | ||
|
||
export function createWebpackRscClientLoader( | ||
options: WebpackRscClientLoaderOptions, | ||
): RuleSetUseItem { | ||
return {loader: clientLoader, options}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import generate from '@babel/generator'; | ||
import {parse} from '@babel/parser'; | ||
import traverse from '@babel/traverse'; | ||
import * as t from '@babel/types'; | ||
import type {LoaderContext} from 'webpack'; | ||
|
||
export interface WebpackRscClientLoaderOptions { | ||
readonly serverReferencesMap: ServerReferencesMap; | ||
readonly callServerImportSource?: string; | ||
} | ||
|
||
export type ServerReferencesMap = Map<string, ServerReferencesModuleInfo>; | ||
|
||
export interface ServerReferencesModuleInfo { | ||
readonly moduleId: string | number; | ||
readonly exportNames: string[]; | ||
} | ||
|
||
export default function webpackRscClientLoader( | ||
this: LoaderContext<WebpackRscClientLoaderOptions>, | ||
source: string, | ||
): void { | ||
this.cacheable(true); | ||
|
||
const {serverReferencesMap, callServerImportSource = `@mfng/core/client`} = | ||
this.getOptions(); | ||
|
||
const loaderContext = this; | ||
const resourcePath = this.resourcePath; | ||
|
||
const ast = parse(source, { | ||
sourceType: `module`, | ||
sourceFilename: resourcePath, | ||
}); | ||
|
||
traverse(ast, { | ||
enter(path) { | ||
const {node} = path; | ||
|
||
if (!t.isProgram(node)) { | ||
return; | ||
} | ||
|
||
if (!node.directives.some(isUseServerDirective)) { | ||
return; | ||
} | ||
|
||
const moduleInfo = serverReferencesMap.get(resourcePath); | ||
|
||
if (!moduleInfo) { | ||
loaderContext.emitError( | ||
new Error( | ||
`Could not find server references module info in \`serverReferencesMap\` for ${resourcePath}.`, | ||
), | ||
); | ||
|
||
path.replaceWith(t.program([])); | ||
|
||
return; | ||
} | ||
|
||
const {moduleId, exportNames} = moduleInfo; | ||
|
||
path.replaceWith( | ||
t.program([ | ||
t.importDeclaration( | ||
[ | ||
t.importSpecifier( | ||
t.identifier(`createServerReference`), | ||
t.identifier(`createServerReference`), | ||
), | ||
], | ||
t.stringLiteral(`react-server-dom-webpack/client`), | ||
), | ||
t.importDeclaration( | ||
[ | ||
t.importSpecifier( | ||
t.identifier(`callServer`), | ||
t.identifier(`callServer`), | ||
), | ||
], | ||
t.stringLiteral(callServerImportSource), | ||
), | ||
...exportNames.map((exportName) => | ||
t.exportNamedDeclaration( | ||
t.variableDeclaration(`const`, [ | ||
t.variableDeclarator( | ||
t.identifier(exportName), | ||
t.callExpression(t.identifier(`createServerReference`), [ | ||
t.stringLiteral(`${moduleId}#${exportName}`), | ||
t.identifier(`callServer`), | ||
]), | ||
), | ||
]), | ||
), | ||
), | ||
]), | ||
); | ||
}, | ||
}); | ||
|
||
const {code} = generate(ast, {sourceFileName: this.resourcePath}); | ||
|
||
// TODO: Handle source maps. | ||
|
||
this.callback(null, code); | ||
} | ||
|
||
function isUseServerDirective(directive: t.Directive): boolean { | ||
return ( | ||
t.isDirectiveLiteral(directive.value) && | ||
directive.value.value === `use server` | ||
); | ||
} |
Oops, something went wrong.