Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .changeset/thick-ideas-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
"@lynx-js/react-rsbuild-plugin": patch
---
12 changes: 12 additions & 0 deletions packages/rspeedy/plugin-react/etc/react-rsbuild-plugin.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
```ts

import { LAYERS } from '@lynx-js/react-webpack-plugin';
import type { LynxTemplatePlugin as LynxTemplatePlugin_2 } from '@lynx-js/template-webpack-plugin';
import type { RsbuildPlugin } from '@rsbuild/core';
import type { TemplateHooks } from '@lynx-js/template-webpack-plugin';

// @public
export interface AddComponentElementConfig {
Expand Down Expand Up @@ -46,6 +48,14 @@ export interface ExtractStrConfig {

export { LAYERS }

// Warning: (ae-missing-release-tag) "LynxTemplatePlugin" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export interface LynxTemplatePlugin {
// (undocumented)
getLynxTemplatePluginHooks: typeof LynxTemplatePlugin_2.getLynxTemplatePluginHooks;
}

// @public
export function pluginReactLynx(userOptions?: PluginReactLynxOptions): RsbuildPlugin[];

Expand Down Expand Up @@ -83,4 +93,6 @@ export interface ShakeVisitorConfig {
retainProp: Array<string>
}

export { TemplateHooks }

```
15 changes: 15 additions & 0 deletions packages/rspeedy/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
* A rsbuild plugin that integrates with ReactLynx.
*/

import type {
LynxTemplatePlugin as InnerLynxTemplatePlugin,
TemplateHooks,
} from '@lynx-js/template-webpack-plugin'

export { pluginReactLynx } from './pluginReactLynx.js'
export type { PluginReactLynxOptions } from './pluginReactLynx.js'

Expand All @@ -19,4 +24,14 @@ export type {
ShakeVisitorConfig,
} from '@lynx-js/react-transform'

interface LynxTemplatePlugin {
getLynxTemplatePluginHooks:
typeof InnerLynxTemplatePlugin.getLynxTemplatePluginHooks
}

// We only export types here
// It is encouraged to use `api.useExposed(Symbol.for('LynxTemplatePlugin'))`
// to access the actual API
export type { LynxTemplatePlugin, TemplateHooks }

export { LAYERS } from '@lynx-js/react-webpack-plugin'
10 changes: 10 additions & 0 deletions packages/rspeedy/plugin-react/src/pluginReactLynx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
} from '@lynx-js/react-transform'
import { LAYERS } from '@lynx-js/react-webpack-plugin'
import type { ExposedAPI } from '@lynx-js/rspeedy'
import { LynxTemplatePlugin } from '@lynx-js/template-webpack-plugin'

import { applyBackgroundOnly } from './backgroundOnly.js'
import { applyCSS } from './css.js'
Expand Down Expand Up @@ -369,6 +370,15 @@ export function pluginReactLynx(
}

api.expose(Symbol.for('LAYERS'), LAYERS)
// Only expose `LynxTemplatePlugin.getLynxTemplatePluginHooks` to avoid
// other breaking changes in `LynxTemplatePlugin`
// breaks `pluginReactLynx`
api.expose(Symbol.for('LynxTemplatePlugin'), {
LynxTemplatePlugin: {
getLynxTemplatePluginHooks: LynxTemplatePlugin
.getLynxTemplatePluginHooks.bind(LynxTemplatePlugin),
},
})

const rspeedyAPIs = api.useExposed<ExposedAPI>(
Symbol.for('rspeedy.api'),
Expand Down
94 changes: 94 additions & 0 deletions packages/rspeedy/plugin-react/test/expose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2025 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import type { RsbuildPlugin, Rspack } from '@rsbuild/core'
import { describe, expect, test } from 'vitest'

import { createStubRspeedy as createRspeedy } from './createRspeedy.js'
import { pluginStubRspeedyAPI } from './stub-rspeedy-api.plugin.js'
import type { LynxTemplatePlugin, TemplateHooks } from '../src/index.js'

describe('Expose', () => {
test('LynxTemplatePlugin', async () => {
const { pluginReactLynx } = await import('../src/index.js')

let expose: { LynxTemplatePlugin: LynxTemplatePlugin } | undefined
let beforeEncodeArgs:
| Parameters<Parameters<TemplateHooks['beforeEncode']['tap']>[1]>[0]
| undefined

const rsbuild = await createRspeedy({
rspeedyConfig: {
source: {
entry: {
main: new URL('./fixtures/basic.tsx', import.meta.url).pathname,
},
},
plugins: [
pluginReactLynx(),
pluginStubRspeedyAPI(),
{
name: 'pluginThatUsesTemplateHooks',
setup(api) {
expose = api.useExposed<
{ LynxTemplatePlugin: LynxTemplatePlugin }
>(Symbol.for('LynxTemplatePlugin'))
api.modifyBundlerChain(chain => {
const PLUGIN_NAME = 'pluginThatUsesTemplateHooks'
chain.plugin(PLUGIN_NAME).use({
apply(compiler) {
compiler.hooks.compilation.tap(
PLUGIN_NAME,
compilation => {
const templateHooks = expose!.LynxTemplatePlugin
.getLynxTemplatePluginHooks(
compilation as unknown as Parameters<
LynxTemplatePlugin['getLynxTemplatePluginHooks']
>[0],
)
templateHooks.beforeEncode.tap(PLUGIN_NAME, args => {
beforeEncodeArgs = args
return args
})
},
)
},
} as Rspack.RspackPluginInstance)
})
},
} as RsbuildPlugin,
],
},
})

expect(expose).toBeUndefined()
expect(beforeEncodeArgs).toBeUndefined()

await rsbuild.initConfigs()
expect(expose).toMatchInlineSnapshot(`
{
"LynxTemplatePlugin": {
"getLynxTemplatePluginHooks": [Function],
},
}
`)

await rsbuild.build()

expect(Object.keys(beforeEncodeArgs!.encodeData.lepusCode))
.toMatchInlineSnapshot(`
[
"root",
"chunks",
"filename",
]
`)
expect(Object.keys(beforeEncodeArgs!.encodeData.manifest))
.toMatchInlineSnapshot(`
[
"/app-service.js",
"/.rspeedy/main/background.js",
]
`)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const LynxTemplatePluginHooksMap = new WeakMap<Compilation, TemplateHooks>();
* compiler.hooks.compilation.tap("MyPlugin", (compilation) => {
* console.log("The compiler is starting a new compilation...");
*
* LynxTemplatePlugin.getCompilationHooks(compilation).beforeEmit.tapAsync(
* LynxTemplatePlugin.getLynxTemplatePluginHooks(compilation).beforeEmit.tapAsync(
* "MyPlugin", // <-- Set a meaningful name here for stacktraces
* (data, cb) => {
* // Manipulate the content
Expand Down
Loading