diff --git a/.changeset/yummy-tables-marry.md b/.changeset/yummy-tables-marry.md new file mode 100644 index 0000000000..7f3fb7b1c8 --- /dev/null +++ b/.changeset/yummy-tables-marry.md @@ -0,0 +1,11 @@ +--- +"@lynx-js/runtime-wrapper-webpack-plugin": patch +--- + +Avoid override user defined fetch function, specify `injectGlobalFetch: false` to `pluginReactLynx` to disable global injection of `fetch` function. + +```js +pluginReactLynx({ + injectGlobalFetch: false, +}), +``` diff --git a/packages/rspeedy/plugin-react/etc/react-rsbuild-plugin.api.md b/packages/rspeedy/plugin-react/etc/react-rsbuild-plugin.api.md index ac89c19d5a..846dde31fd 100644 --- a/packages/rspeedy/plugin-react/etc/react-rsbuild-plugin.api.md +++ b/packages/rspeedy/plugin-react/etc/react-rsbuild-plugin.api.md @@ -37,6 +37,7 @@ export interface PluginReactLynxOptions { // @alpha experimental_isLazyBundle?: boolean; firstScreenSyncTiming?: 'immediately' | 'jsReady'; + injectGlobalFetch?: boolean; jsx?: Partial | undefined; pipelineSchedulerConfig?: number; removeDescendantSelectorScope?: boolean; diff --git a/packages/rspeedy/plugin-react/src/entry.ts b/packages/rspeedy/plugin-react/src/entry.ts index 87ac323b15..4c8f9b915f 100644 --- a/packages/rspeedy/plugin-react/src/entry.ts +++ b/packages/rspeedy/plugin-react/src/entry.ts @@ -53,6 +53,7 @@ export function applyEntry( pipelineSchedulerConfig, removeDescendantSelectorScope, targetSdkVersion, + injectGlobalFetch, experimental_isLazyBundle, } = options @@ -199,6 +200,7 @@ export function applyEntry( targetSdkVersion, // Inject runtime wrapper for all `.js` but not `main-thread.js` and `main-thread.[hash].js`. test: /^(?!.*main-thread(?:\.[A-Fa-f0-9]*)?\.js$).*\.js$/, + injectGlobalFetch, }]) .end() .plugin(`${LynxEncodePlugin.name}`) diff --git a/packages/rspeedy/plugin-react/src/pluginReactLynx.ts b/packages/rspeedy/plugin-react/src/pluginReactLynx.ts index 31cef32a61..b452ff08c5 100644 --- a/packages/rspeedy/plugin-react/src/pluginReactLynx.ts +++ b/packages/rspeedy/plugin-react/src/pluginReactLynx.ts @@ -285,6 +285,11 @@ export interface PluginReactLynxOptions { * @deprecated `targetSdkVersion` is now an alias of {@link PluginReactLynxOptions.engineVersion}. Use {@link PluginReactLynxOptions.engineVersion} instead. */ targetSdkVersion?: string + /** + * Inject `lynx.fetch` to global scope `fetch`. + * @defaultValue true + */ + injectGlobalFetch?: boolean /** * Generate standalone lazy bundle. @@ -341,6 +346,8 @@ export function pluginReactLynx( engineVersion: '', experimental_isLazyBundle: false, + + injectGlobalFetch: true, } const resolvedOptions = Object.assign(defaultOptions, userOptions, { // Use `engineVersion` to override the default values diff --git a/packages/webpack/runtime-wrapper-webpack-plugin/etc/runtime-wrapper-webpack-plugin.api.md b/packages/webpack/runtime-wrapper-webpack-plugin/etc/runtime-wrapper-webpack-plugin.api.md index 862493b6c5..ac6753bd51 100644 --- a/packages/webpack/runtime-wrapper-webpack-plugin/etc/runtime-wrapper-webpack-plugin.api.md +++ b/packages/webpack/runtime-wrapper-webpack-plugin/etc/runtime-wrapper-webpack-plugin.api.md @@ -3,21 +3,25 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts + import type { BannerPlugin } from 'webpack'; import type { Compiler } from 'webpack'; // @public export class RuntimeWrapperWebpackPlugin { - constructor(options?: Partial); - apply(compiler: Compiler): void; - static defaultOptions: Readonly>; + constructor(options?: Partial); + apply(compiler: Compiler): void; + static defaultOptions: Readonly>; } // @public export interface RuntimeWrapperWebpackPluginOptions { - bannerType: (filename: string) => 'script' | 'bundle'; - injectVars?: ((vars: string[]) => string[]) | string[]; - targetSdkVersion: string; - test: BannerPlugin['options']['test']; + bannerType: (filename: string) => 'script' | 'bundle'; + // Warning: (tsdoc-undefined-tag) The TSDoc tag "@default" is not defined in this configuration + injectGlobalFetch?: boolean; + injectVars?: ((vars: string[]) => string[]) | string[]; + targetSdkVersion: string; + test: BannerPlugin['options']['test']; } + ``` diff --git a/packages/webpack/runtime-wrapper-webpack-plugin/src/RuntimeWrapperWebpackPlugin.ts b/packages/webpack/runtime-wrapper-webpack-plugin/src/RuntimeWrapperWebpackPlugin.ts index ed6d1fcb1e..3edd10b133 100644 --- a/packages/webpack/runtime-wrapper-webpack-plugin/src/RuntimeWrapperWebpackPlugin.ts +++ b/packages/webpack/runtime-wrapper-webpack-plugin/src/RuntimeWrapperWebpackPlugin.ts @@ -37,6 +37,11 @@ interface RuntimeWrapperWebpackPluginOptions { * The variables to be injected into the chunk. */ injectVars?: ((vars: string[]) => string[]) | string[]; + /** + * Inject `lynx.fetch` to global scope `fetch`. + * @defaultValue true + */ + injectGlobalFetch?: boolean; } const defaultInjectVars = [ @@ -78,6 +83,7 @@ class RuntimeWrapperWebpackPlugin { test: /\.js$/, bannerType: () => 'script', injectVars: defaultInjectVars, + injectGlobalFetch: true, }); /** @@ -108,7 +114,7 @@ class RuntimeWrapperWebpackPluginImpl { public compiler: Compiler, public options: RuntimeWrapperWebpackPluginOptions, ) { - const { targetSdkVersion, test } = options; + const { targetSdkVersion, test, injectGlobalFetch = true } = options; const { BannerPlugin } = compiler.webpack; const isDev = process.env['NODE_ENV'] === 'development' @@ -136,6 +142,7 @@ class RuntimeWrapperWebpackPluginImpl { overrideRuntimePromise: true, moduleId: '[name].js', targetSdkVersion, + injectGlobalFetch, }) + (isDev ? lynxChunkEntries(JSON.stringify(chunk.id)) @@ -216,11 +223,13 @@ const amdBanner = ({ moduleId, overrideRuntimePromise, targetSdkVersion, + injectGlobalFetch, }: { injectStr: string; moduleId: string; overrideRuntimePromise: boolean; targetSdkVersion: string; + injectGlobalFetch?: boolean; }) => { return ( ` @@ -230,7 +239,11 @@ lynx.targetSdkVersion=lynx.targetSdkVersion||${ JSON.stringify(targetSdkVersion) }; ${overrideRuntimePromise ? `var Promise = lynx.Promise;` : ''} -var fetch = lynx.fetch; +${ + injectGlobalFetch + ? `var fetch = lynx.fetch;` + : '' + } ` ); };