@modern-js/builder 3.0.1 → 3.0.2

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 (46) hide show
  1. package/dist/cjs/createBuilder.js +3 -8
  2. package/dist/cjs/plugins/environmentDefaults.js +4 -4
  3. package/dist/cjs/plugins/rscConfig.js +159 -0
  4. package/dist/cjs/{rsc/rsc-css-loader.js → shared/rsc/rsc-server-entry-loader.js} +6 -7
  5. package/dist/esm/createBuilder.mjs +3 -8
  6. package/dist/esm/plugins/environmentDefaults.mjs +4 -4
  7. package/dist/esm/plugins/rscConfig.mjs +112 -0
  8. package/dist/esm/shared/rsc/rsc-server-entry-loader.mjs +7 -0
  9. package/dist/esm-node/createBuilder.mjs +3 -8
  10. package/dist/esm-node/plugins/environmentDefaults.mjs +4 -4
  11. package/dist/esm-node/plugins/rscConfig.mjs +117 -0
  12. package/dist/esm-node/shared/rsc/rsc-server-entry-loader.mjs +8 -0
  13. package/dist/types/plugins/rscConfig.d.ts +18 -0
  14. package/dist/types/shared/rsc/rsc-server-entry-loader.d.ts +5 -0
  15. package/package.json +7 -7
  16. package/dist/cjs/rsc/common.js +0 -157
  17. package/dist/cjs/rsc/plugins/rsbuild-rsc-plugin.js +0 -169
  18. package/dist/cjs/rsc/plugins/rspack-rsc-client-plugin.js +0 -187
  19. package/dist/cjs/rsc/plugins/rspack-rsc-server-plugin.js +0 -245
  20. package/dist/cjs/rsc/rsc-client-loader.js +0 -71
  21. package/dist/cjs/rsc/rsc-server-loader.js +0 -102
  22. package/dist/cjs/rsc/rsc-ssr-loader.js +0 -60
  23. package/dist/esm/rsc/common.mjs +0 -87
  24. package/dist/esm/rsc/plugins/rsbuild-rsc-plugin.mjs +0 -124
  25. package/dist/esm/rsc/plugins/rspack-rsc-client-plugin.mjs +0 -143
  26. package/dist/esm/rsc/plugins/rspack-rsc-server-plugin.mjs +0 -211
  27. package/dist/esm/rsc/rsc-client-loader.mjs +0 -37
  28. package/dist/esm/rsc/rsc-css-loader.mjs +0 -8
  29. package/dist/esm/rsc/rsc-server-loader.mjs +0 -58
  30. package/dist/esm/rsc/rsc-ssr-loader.mjs +0 -26
  31. package/dist/esm-node/rsc/common.mjs +0 -88
  32. package/dist/esm-node/rsc/plugins/rsbuild-rsc-plugin.mjs +0 -126
  33. package/dist/esm-node/rsc/plugins/rspack-rsc-client-plugin.mjs +0 -144
  34. package/dist/esm-node/rsc/plugins/rspack-rsc-server-plugin.mjs +0 -212
  35. package/dist/esm-node/rsc/rsc-client-loader.mjs +0 -38
  36. package/dist/esm-node/rsc/rsc-css-loader.mjs +0 -9
  37. package/dist/esm-node/rsc/rsc-server-loader.mjs +0 -60
  38. package/dist/esm-node/rsc/rsc-ssr-loader.mjs +0 -27
  39. package/dist/types/rsc/common.d.ts +0 -22
  40. package/dist/types/rsc/plugins/rsbuild-rsc-plugin.d.ts +0 -7
  41. package/dist/types/rsc/plugins/rspack-rsc-client-plugin.d.ts +0 -14
  42. package/dist/types/rsc/plugins/rspack-rsc-server-plugin.d.ts +0 -32
  43. package/dist/types/rsc/rsc-client-loader.d.ts +0 -7
  44. package/dist/types/rsc/rsc-css-loader.d.ts +0 -2
  45. package/dist/types/rsc/rsc-server-loader.d.ts +0 -6
  46. package/dist/types/rsc/rsc-ssr-loader.d.ts +0 -6
@@ -1,60 +0,0 @@
1
- import __rslib_shim_module__ from "node:module";
2
- const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
3
- import path from "path";
4
- import { transform } from "@swc/core";
5
- import { setRscBuildInfo } from "./common.mjs";
6
- function extractMetadata(code) {
7
- const metadataRegex = /\/\* @modern-js-rsc-metadata\n([\s\S]*?)\*\//;
8
- const match = code.match(metadataRegex);
9
- if (!match) return null;
10
- try {
11
- const metadata = JSON.parse(match[1]);
12
- return metadata;
13
- } catch (e) {
14
- console.error('Failed to parse metadata:', e);
15
- return null;
16
- }
17
- }
18
- async function rscServerLoader(source) {
19
- this.cacheable(true);
20
- const callback = this.async();
21
- const { appDir, runtimePath = '@modern-js/runtime/rsc/server' } = this.getOptions();
22
- const result = await transform(source, {
23
- filename: this.resourcePath,
24
- jsc: {
25
- target: 'es2020',
26
- experimental: {
27
- cacheRoot: path.resolve(appDir, 'node_modules/.swc'),
28
- plugins: [
29
- [
30
- require.resolve('@modern-js/flight-server-transform-plugin'),
31
- {
32
- appDir: appDir,
33
- runtimePath: runtimePath
34
- }
35
- ]
36
- ]
37
- }
38
- },
39
- isModule: true
40
- });
41
- const { code, map } = result;
42
- const metadata = extractMetadata(code);
43
- if (metadata?.directive && 'client' === metadata.directive) {
44
- const { exportNames } = metadata;
45
- if (exportNames.length > 0) setRscBuildInfo(this._module, {
46
- type: 'client',
47
- resourcePath: this.resourcePath,
48
- clientReferences: exportNames
49
- });
50
- } else if (metadata) {
51
- const { exportNames } = metadata;
52
- if (exportNames.length > 0) setRscBuildInfo(this._module, {
53
- type: 'server',
54
- resourcePath: this.resourcePath,
55
- exportNames: exportNames.map((item)=>item.exportName)
56
- });
57
- }
58
- return callback(null, code, map);
59
- }
60
- export { rscServerLoader as default };
@@ -1,27 +0,0 @@
1
- import "node:module";
2
- import { getExportNames, isServerModule, parseSource, setRscBuildInfo } from "./common.mjs";
3
- async function rscSsrLoader(source, sourceMap) {
4
- this.cacheable(true);
5
- const callback = this.async();
6
- const ast = await parseSource(source);
7
- const hasDeclareServerDirective = await isServerModule(ast);
8
- const resourcePath = this.resourcePath;
9
- if (!hasDeclareServerDirective) return void callback(null, source, sourceMap);
10
- const exportedNames = await getExportNames(ast, true);
11
- const importsCode = `
12
- 'use server';
13
- `;
14
- const exportsCode = exportedNames.map((name)=>{
15
- if ('default' === name) return 'export default () => {throw new Error("Server actions must not be called during server-side rendering.")}';
16
- return `export const ${name} = () => {
17
- throw new Error("Server actions must not be called during server-side rendering.")
18
- }`;
19
- }).join('\n');
20
- if (exportedNames.length > 0) setRscBuildInfo(this._module, {
21
- type: 'server',
22
- resourcePath,
23
- exportNames: exportedNames
24
- });
25
- callback(null, `${importsCode}\n${exportsCode}`, sourceMap);
26
- }
27
- export { rscSsrLoader as default };
@@ -1,22 +0,0 @@
1
- export type { ClientReference, ImportManifestEntry, ModuleLoading, SSRManifest, SSRModuleMap, ClientReferencesMap, ServerReferencesMap, ServerManifest, ServerReferencesModuleInfo, ClientManifest, } from '@modern-js/types/server';
2
- import type { Rspack } from '@rsbuild/core';
3
- import { type Module } from '@swc/core';
4
- export declare const rspackRscLayerName = "react-server";
5
- export type SourceMap = Parameters<Rspack.LoaderDefinitionFunction>[1];
6
- export declare const MODERN_RSC_INFO = "modernRscInfo";
7
- export declare const sharedData: {
8
- store: Map<string, any>;
9
- get<T>(key: string): T | undefined;
10
- set<T>(key: string, value: T): void;
11
- clear(): void;
12
- };
13
- export declare function setBuildInfo(mod: Rspack.Module, property: Record<string, any>): void;
14
- export declare function setRscBuildInfo(mod: Rspack.Module, property: Record<string, any>): void;
15
- export declare function removeRscBuildInfo(mod: Rspack.Module): void;
16
- export declare function getRscBuildInfo(mod: Rspack.Module): any;
17
- export declare function isCssModule(mod: Rspack.Module): any;
18
- export declare const parseSource: (source: string) => Promise<Module>;
19
- export declare const getExportNames: (ast: Module, collectFuncOnly?: boolean) => Promise<string[]>;
20
- export declare const isServerModule: (ast: Module) => Promise<boolean>;
21
- export declare const isClientModule: (ast: Module) => Promise<boolean>;
22
- export declare function findRootIssuer(modulegraph: Rspack.ModuleGraph, module: Rspack.NormalModule): Rspack.NormalModule;
@@ -1,7 +0,0 @@
1
- import type { RsbuildPlugin } from '@rsbuild/core';
2
- export declare const rsbuildRscPlugin: ({ appDir, rscClientRuntimePath, rscServerRuntimePath, internalDirectory, }: {
3
- appDir: string;
4
- rscClientRuntimePath?: string;
5
- rscServerRuntimePath?: string;
6
- internalDirectory?: string;
7
- }) => RsbuildPlugin;
@@ -1,14 +0,0 @@
1
- import type { Rspack } from '@rsbuild/core';
2
- export interface RscClientPluginOptions {
3
- readonly clientManifestFilename?: string;
4
- readonly ssrManifestFilename?: string;
5
- }
6
- export declare class RspackRscClientPlugin {
7
- private clientReferencesMap;
8
- private clientManifestFilename;
9
- private ssrManifestFilename;
10
- private styles?;
11
- private dependencies;
12
- constructor(options?: RscClientPluginOptions);
13
- apply(compiler: Rspack.Compiler): void;
14
- }
@@ -1,32 +0,0 @@
1
- import type { Rspack } from '@rsbuild/core';
2
- export interface RscServerPluginOptions {
3
- readonly serverManifestFilename?: string;
4
- readonly entryPath2Name: Map<string, string>;
5
- }
6
- export interface ModuleExportsInfo {
7
- readonly moduleResource: string;
8
- readonly exportName: string;
9
- }
10
- export interface EntryInfo {
11
- readonly entryName: string;
12
- readonly entryPath: string;
13
- }
14
- export declare class RscServerPlugin {
15
- private clientReferencesMap;
16
- private serverReferencesMap;
17
- private serverManifest;
18
- private serverManifestFilename;
19
- private entryPath2Name;
20
- private styles;
21
- private moduleToEntries;
22
- constructor(options: RscServerPluginOptions);
23
- private isValidModule;
24
- private hasValidEntries;
25
- private getEntryNameFromIssuer;
26
- private createEntryFromIssuer;
27
- private buildModuleToEntriesMapping;
28
- private traverseModulesFromEntry;
29
- private findModuleEntries;
30
- private getEntryPathByName;
31
- apply(compiler: Rspack.Compiler): void;
32
- }
@@ -1,7 +0,0 @@
1
- import type { Rspack } from '@rsbuild/core';
2
- import { type SourceMap } from './common';
3
- export type ClientLoaderOptions = {
4
- callServerImport?: string;
5
- registerImport?: string;
6
- };
7
- export default function rscClientLoader(this: Rspack.LoaderContext<ClientLoaderOptions>, source: string, sourceMap: SourceMap): Promise<void>;
@@ -1,2 +0,0 @@
1
- import type { Rspack } from '@rsbuild/core';
2
- export default function rscCssLoader(this: Rspack.LoaderContext<unknown>, source: string): string;
@@ -1,6 +0,0 @@
1
- import type { Rspack } from '@rsbuild/core';
2
- export type RscServerLoaderOptions = {
3
- appDir: string;
4
- runtimePath?: string;
5
- };
6
- export default function rscServerLoader(this: Rspack.LoaderContext<RscServerLoaderOptions>, source: string): Promise<void>;
@@ -1,6 +0,0 @@
1
- import type { Rspack } from '@rsbuild/core';
2
- import { type SourceMap } from './common';
3
- export type RscSsrLoaderOptions = {
4
- entryPath2Name: Map<string, string>;
5
- };
6
- export default function rscSsrLoader(this: Rspack.LoaderContext<RscSsrLoaderOptions>, source: string, sourceMap: SourceMap): Promise<void>;