Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export default function build(
config: Rsg.SanitizedStyleguidistConfig,
callback: (err: Error, stats: webpack.Stats) => void
) {
return webpack(makeWebpackConfig(config, 'production'), (err, stats) => {
// FIX: Allow process.env.NODE_ENV to override the default 'production' state
// This prevents the mismatch where Babel thinks it is in one env and Styleguidist another.
const env = (process.env.NODE_ENV as 'development' | 'production' | 'none') || 'production';

return webpack(makeWebpackConfig(config, env), (err, stats) => {
// require('fs').writeFileSync('stats.json', JSON.stringify(stats.toJson()));
callback(err as Error, stats as webpack.Stats);
});
Expand Down
11 changes: 8 additions & 3 deletions src/scripts/make-webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ export default function (
config: Rsg.SanitizedStyleguidistConfig,
env: 'development' | 'production' | 'none'
): Configuration {
process.env.NODE_ENV = process.env.NODE_ENV || env;
// FIX: Prioritize the existing process.env.NODE_ENV if it's already set
// This ensures that Babel and other loaders see the same environment as Styleguidist
const activeEnv = process.env.NODE_ENV || env;
process.env.NODE_ENV = activeEnv;

const isProd = env === 'production';
// Use the resolved activeEnv for production checks
const isProd = activeEnv === 'production';

const template = isFunction(config.template) ? config.template : MiniHtmlWebpackTemplate;
const templateContext = isFunction(config.template) ? {} : config.template;
Expand All @@ -44,7 +48,8 @@ export default function (

let webpackConfig: Configuration = {
entry: config.require.concat([path.resolve(sourceDir, 'index')]),
mode: env,
// FIX: Use activeEnv for mode to keep Webpack optimization in sync with NODE_ENV
mode: activeEnv as 'development' | 'production' | 'none',
output: {
path: config.styleguideDir,
filename: 'build/[name].bundle.js',
Expand Down