diff --git a/packages/core/src/createContext.ts b/packages/core/src/createContext.ts index 7b16c07196..d000747764 100644 --- a/packages/core/src/createContext.ts +++ b/packages/core/src/createContext.ts @@ -108,6 +108,7 @@ export function createPublicContext( 'configPath', 'tsconfigPath', 'bundlerType', + 'environments', ]; // Using Proxy to get the current value of context. diff --git a/packages/core/src/plugins/entry.ts b/packages/core/src/plugins/entry.ts index dab08d8fc5..c5d692c5d0 100644 --- a/packages/core/src/plugins/entry.ts +++ b/packages/core/src/plugins/entry.ts @@ -30,11 +30,10 @@ export const pluginEntry = (): RsbuildPlugin => ({ setup(api) { api.modifyBundlerChain( - async (chain, { target, isServer, isServiceWorker }) => { - const config = api.getNormalizedConfig(); + async (chain, { environment, isServer, isServiceWorker }) => { + const config = api.getNormalizedConfig({ environment }); const { preEntry } = config.source; - const entry = - target === 'web' ? api.context.entry : getEntryObject(config, target); + const { entry } = api.context.environments[environment]; const injectCoreJsEntry = config.output.polyfill === 'entry' && !isServer && !isServiceWorker; diff --git a/packages/core/tests/__snapshots__/entry.test.ts.snap b/packages/core/tests/__snapshots__/entry.test.ts.snap new file mode 100644 index 0000000000..78f7822806 --- /dev/null +++ b/packages/core/tests/__snapshots__/entry.test.ts.snap @@ -0,0 +1,20 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`plugin-entry > should apply environments entry config correctly 1`] = ` +[ + { + "entry": { + "index": [ + "src/index.client", + ], + }, + }, + { + "entry": { + "index": [ + "src/index.ssr", + ], + }, + }, +] +`; diff --git a/packages/core/tests/entry.test.ts b/packages/core/tests/entry.test.ts index aa49ab7309..80ee3e32c9 100644 --- a/packages/core/tests/entry.test.ts +++ b/packages/core/tests/entry.test.ts @@ -60,4 +60,31 @@ describe('plugin-entry', () => { expect(config).toEqual(item.expected); }); + + it('should apply environments entry config correctly', async () => { + const rsbuild = await createStubRsbuild({ + plugins: [pluginEntry()], + rsbuildConfig: { + environments: { + web: { + source: { + entry: { + index: './src/index.client', + }, + }, + }, + ssr: { + source: { + entry: { + index: './src/index.ssr', + }, + }, + }, + }, + }, + }); + + const configs = await rsbuild.initConfigs(); + expect(configs).toMatchSnapshot(); + }); });