Skip to content

Commit ea1a533

Browse files
Fix Webpack aliasing
1 parent 714913d commit ea1a533

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

code/frameworks/nextjs/src/config/webpack.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { NextConfig } from 'next';
22
import type { Configuration as WebpackConfig } from 'webpack';
33
import { DefinePlugin } from 'webpack';
44

5-
import { addScopedAlias, resolveNextConfig } from '../utils';
5+
import { addScopedAlias, resolveNextConfig, setAlias } from '../utils';
66

77
const tryResolve = (path: string) => {
88
try {
@@ -20,33 +20,32 @@ export const configureConfig = async ({
2020
nextConfigPath?: string;
2121
}): Promise<NextConfig> => {
2222
const nextConfig = await resolveNextConfig({ nextConfigPath });
23-
baseConfig.resolve ??= {};
24-
baseConfig.resolve.alias ??= {};
25-
const aliasConfig = baseConfig.resolve.alias;
2623

2724
addScopedAlias(baseConfig, 'next/config');
25+
26+
// @ts-expect-error We know that alias is an object
27+
if (baseConfig.resolve?.alias?.['react-dom']) {
28+
// Removing the alias to react-dom to avoid conflicts with the alias we are setting
29+
// because the react-dom alias is an exact match and we need to alias separate parts of react-dom
30+
// in different places
31+
// @ts-expect-error We know that alias is an object
32+
delete baseConfig.resolve.alias?.['react-dom'];
33+
}
34+
2835
if (tryResolve('next/dist/compiled/react')) {
2936
addScopedAlias(baseConfig, 'react', 'next/dist/compiled/react');
3037
}
3138
if (tryResolve('next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js')) {
32-
addScopedAlias(
39+
setAlias(
3340
baseConfig,
3441
'react-dom/test-utils',
3542
'next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js'
3643
);
37-
} else {
38-
const name = 'react-dom/test-utils';
39-
if (Array.isArray(aliasConfig)) {
40-
aliasConfig.push({
41-
name,
42-
alias: name,
43-
});
44-
} else {
45-
aliasConfig[name] = name;
46-
}
4744
}
4845
if (tryResolve('next/dist/compiled/react-dom')) {
49-
addScopedAlias(baseConfig, 'react-dom', 'next/dist/compiled/react-dom');
46+
setAlias(baseConfig, 'react-dom$', 'next/dist/compiled/react-dom');
47+
setAlias(baseConfig, 'react-dom/client', 'next/dist/compiled/react-dom/client');
48+
setAlias(baseConfig, 'react-dom/server', 'next/dist/compiled/react-dom/server');
5049
}
5150

5251
setupRuntimeConfig(baseConfig, nextConfig);

code/frameworks/nextjs/src/utils.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,27 @@ export const resolveNextConfig = async ({
2727
return loadConfig(PHASE_DEVELOPMENT_SERVER, dir, undefined);
2828
};
2929

30-
// This is to help the addon in development
31-
// Without it, webpack resolves packages in its node_modules instead of the example's node_modules
32-
export const addScopedAlias = (baseConfig: WebpackConfig, name: string, alias?: string): void => {
30+
export function setAlias(baseConfig: WebpackConfig, name: string, alias: string) {
3331
baseConfig.resolve ??= {};
3432
baseConfig.resolve.alias ??= {};
3533
const aliasConfig = baseConfig.resolve.alias;
3634

37-
const scopedAlias = scopedResolve(`${alias ?? name}`);
38-
3935
if (Array.isArray(aliasConfig)) {
4036
aliasConfig.push({
4137
name,
42-
alias: scopedAlias,
38+
alias,
4339
});
4440
} else {
45-
aliasConfig[name] = scopedAlias;
41+
aliasConfig[name] = alias;
4642
}
43+
}
44+
45+
// This is to help the addon in development
46+
// Without it, webpack resolves packages in its node_modules instead of the example's node_modules
47+
export const addScopedAlias = (baseConfig: WebpackConfig, name: string, alias?: string): void => {
48+
const scopedAlias = scopedResolve(`${alias ?? name}`);
49+
50+
setAlias(baseConfig, name, scopedAlias);
4751
};
4852

4953
/**
@@ -64,7 +68,7 @@ export const scopedResolve = (id: string): string => {
6468
let scopedModulePath;
6569

6670
try {
67-
// TODO: Remove in next major release (SB 8.0) and use the statement in the catch block per default instead
71+
// TODO: Remove in next major release (SB 9.0) and use the statement in the catch block per default instead
6872
scopedModulePath = require.resolve(id, { paths: [resolve()] });
6973
} catch (e) {
7074
scopedModulePath = require.resolve(id);

code/renderers/react/src/act-compat.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ declare const globalThis: {
1010

1111
const reactAct =
1212
// @ts-expect-error act might not be available in some versions of React
13-
typeof React.act === 'function'
14-
? // @ts-expect-error act might not be available in some versions of React
15-
React.act
16-
: DeprecatedReactTestUtils.act ?? (async (cb: () => Promise<void> | void) => cb());
13+
typeof React.act === 'function' ? React.act : DeprecatedReactTestUtils.act;
1714

1815
export function setReactActEnvironment(isReactActEnvironment: boolean) {
1916
globalThis.IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;

0 commit comments

Comments
 (0)