Skip to content

feat: integrate env variable loading into createRsbuild options#4783

Merged
chenjiahan merged 5 commits intomainfrom
env_loading_option_0313
Mar 13, 2025
Merged

feat: integrate env variable loading into createRsbuild options#4783
chenjiahan merged 5 commits intomainfrom
env_loading_option_0313

Conversation

@chenjiahan
Copy link
Member

Summary

Although Rsbuild's JavaScript API already provides the loadEnv method, combining loadEnv with createRsbuild is still somewhat cumbersome.

This PR introduces a new loadEnv option for createRsbuild to simplify the logic of env loading.

const rsbuild = createRsbuild({ loadEnv: true });

This is equivalent to:

const envs = loadEnv();

const config = {
  // some options
};

config.source.define = {
  ...envs.publicVars,
  ...config.source.define,
};

// watch the env files
config.dev.watchFiles = [
  ...(config.dev.watchFiles ? castArray(config.dev.watchFiles) : []),
  {
    paths: envs.filePaths,
    type: 'reload-server',
  },
];

// add env files to build dependencies, so that the build cache
// can be invalidated when the env files are changed.
if (config.performance?.buildCache && envs.filePaths.length > 0) {
  const { buildCache } = config.performance;
  if (buildCache === true) {
    config.performance.buildCache = {
      buildDependencies: envs.filePaths,
    };
  } else {
    buildCache.buildDependencies ||= [];
    buildCache.buildDependencies.push(...envs.filePaths);
  }
}

const rsbuild = createRsbuild({
  rsbuildConfig: config,
});

rsbuild.onCloseBuild(envs.cleanup);
rsbuild.onCloseDevServer(envs.cleanup);

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@netlify
Copy link

netlify bot commented Mar 13, 2025

Deploy Preview for rsbuild ready!

Name Link
🔨 Latest commit 0df40f0
🔍 Latest deploy log https://app.netlify.com/sites/rsbuild/deploys/67d2f0585cea880008d6a501
😎 Deploy Preview https://deploy-preview-4783--rsbuild.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 76 (🔴 down 8 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: 60 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@chenjiahan chenjiahan merged commit e86b0a9 into main Mar 13, 2025
11 checks passed
@chenjiahan chenjiahan deleted the env_loading_option_0313 branch March 13, 2025 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant