Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/yummy-tables-marry.md
Original file line number Diff line number Diff line change
@@ -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,
}),
```
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface PluginReactLynxOptions {
// @alpha
experimental_isLazyBundle?: boolean;
firstScreenSyncTiming?: 'immediately' | 'jsReady';
injectGlobalFetch?: boolean;
jsx?: Partial<JsxTransformerConfig> | undefined;
pipelineSchedulerConfig?: number;
removeDescendantSelectorScope?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions packages/rspeedy/plugin-react/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function applyEntry(
pipelineSchedulerConfig,
removeDescendantSelectorScope,
targetSdkVersion,
injectGlobalFetch,

experimental_isLazyBundle,
} = options
Expand Down Expand Up @@ -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}`)
Expand Down
7 changes: 7 additions & 0 deletions packages/rspeedy/plugin-react/src/pluginReactLynx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuntimeWrapperWebpackPluginOptions>);
apply(compiler: Compiler): void;
static defaultOptions: Readonly<Required<RuntimeWrapperWebpackPluginOptions>>;
constructor(options?: Partial<RuntimeWrapperWebpackPluginOptions>);
apply(compiler: Compiler): void;
static defaultOptions: Readonly<Required<RuntimeWrapperWebpackPluginOptions>>;
}

// @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'];
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
* 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 = [
Expand Down Expand Up @@ -78,6 +83,7 @@
test: /\.js$/,
bannerType: () => 'script',
injectVars: defaultInjectVars,
injectGlobalFetch: true,
});

/**
Expand Down Expand Up @@ -108,7 +114,7 @@
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'
Expand Down Expand Up @@ -136,6 +142,7 @@
overrideRuntimePromise: true,
moduleId: '[name].js',
targetSdkVersion,
injectGlobalFetch,
})
+ (isDev
? lynxChunkEntries(JSON.stringify(chunk.id))
Expand Down Expand Up @@ -216,11 +223,13 @@
moduleId,
overrideRuntimePromise,
targetSdkVersion,
injectGlobalFetch,
}: {
injectStr: string;
moduleId: string;
overrideRuntimePromise: boolean;
targetSdkVersion: string;
injectGlobalFetch?: boolean;
}) => {
return (
`
Expand All @@ -230,7 +239,11 @@
JSON.stringify(targetSdkVersion)
};
${overrideRuntimePromise ? `var Promise = lynx.Promise;` : ''}
var fetch = lynx.fetch;
${
injectGlobalFetch
? `var fetch = lynx.fetch;`
: ''

Check warning on line 245 in packages/webpack/runtime-wrapper-webpack-plugin/src/RuntimeWrapperWebpackPlugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/webpack/runtime-wrapper-webpack-plugin/src/RuntimeWrapperWebpackPlugin.ts#L245

Added line #L245 was not covered by tests
}
`
);
};
Expand Down
Loading