Skip to content

Commit

Permalink
fix: compat with loading config from environments
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 23, 2024
1 parent 84902a6 commit c13f3a1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
43 changes: 40 additions & 3 deletions packages/builder-rsbuild/src/preview/iframe-rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,58 @@ export default async (
throw new Error('Cache is required')
}

let contentFromConfig: RsbuildConfig = {}
const { content } = await loadConfig({
cwd: workingDir,
path: rsbuildConfigPath,
})

const { environments, ...withoutEnv } = content
if (content.environments) {
const envCount = Object.keys(content.environments).length
if (envCount === 0) {
// Empty useless environment field.
contentFromConfig = withoutEnv
} else if (envCount === 1) {
// Directly use the unique environment.
contentFromConfig = mergeRsbuildConfig(
withoutEnv,
content.environments[0],
)
} else {
// User need to specify the environment first if more than one provided.
const userEnv = builderOptions.environment
if (typeof userEnv !== 'string') {
throw new Error(
'You must specify an environment when there are multiple environments in the Rsbuild config.',
)
}

if (Object.keys(content.environments).includes(userEnv)) {
contentFromConfig = mergeRsbuildConfig(
withoutEnv,
content.environments[userEnv],
)
} else {
throw new Error(
`The specified environment "${userEnv}" is not found in the Rsbuild config.`,
)
}
}
} else {
contentFromConfig = content
}

// Reset `config.source.entry` field, do not use provided entry
// see https://github.com/rspack-contrib/storybook-rsbuild/issues/43
content.source ??= {}
content.source.entry = {}
contentFromConfig.source ??= {}
contentFromConfig.source.entry = {}

const resourceFilename = isProd
? 'static/media/[name].[contenthash:8][ext]'
: 'static/media/[path][name][ext]'

const merged = mergeRsbuildConfig(content, {
const merged = mergeRsbuildConfig(contentFromConfig, {
output: {
cleanDistPath: false,
dataUriLimit: {
Expand Down
4 changes: 4 additions & 0 deletions packages/builder-rsbuild/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export type BuilderOptions = {
* Enable Rspack's lazy compilation (experimental).
*/
lazyCompilation?: boolean
/**
* Which environment to use from the Rsbuild config.
*/
environment?: string
}

export interface BuilderResult extends BuilderResultBase {
Expand Down
7 changes: 7 additions & 0 deletions sandboxes/react-16/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ import { defineConfig } from '@rsbuild/core'
import { pluginReact } from '@rsbuild/plugin-react'

export default defineConfig({
environments: {
web: {
source: {
entry: { index: './src/index.tsx' },
},
},
},
plugins: [pluginReact()],
})

0 comments on commit c13f3a1

Please sign in to comment.