Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): skip DefinePlugin for SSR #16612

Merged
merged 1 commit into from
Apr 27, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { composePlugins, withNx } = require('@nx/webpack');
const { withReact } = require('@nx/react');
const { withModuleFederationForSSR } = require('@nx/react/module-federation');
const {composePlugins, withNx} = require('@nx/webpack');
const {withReact} = require('@nx/react');
const {withModuleFederationForSSR} = require('@nx/react/module-federation');

const baseConfig = require('./module-federation.config');

const defaultConfig = {
...baseConfig
...baseConfig
};

// Nx plugins for webpack to build config object from Nx options and context.
module.exports = composePlugins(withNx(), withReact(), withModuleFederationForSSR(defaultConfig));
module.exports = composePlugins(withNx(), withReact({ssr: true}), withModuleFederationForSSR(defaultConfig));
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { composePlugins, withNx } = require('@nx/webpack');
const { withReact } = require('@nx/react');
const { withModuleFederationForSSR } = require('@nx/react/module-federation');
const {composePlugins, withNx} = require('@nx/webpack');
const {withReact} = require('@nx/react');
const {withModuleFederationForSSR} = require('@nx/react/module-federation');

const baseConfig = require("./module-federation.server.config");

const defaultConfig = {
...baseConfig,
...baseConfig,
};

// Nx plugins for webpack to build config object from Nx options and context.
module.exports = composePlugins(withNx(), withReact(), withModuleFederationForSSR(defaultConfig));
module.exports = composePlugins(withNx(), withReact({ssr: true}), withModuleFederationForSSR(defaultConfig));
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ function determineRemoteUrl(remote: string) {
export async function withModuleFederationForSSR(
options: ModuleFederationConfig
) {
const reactWebpackConfig = require('../../plugins/webpack');

const { sharedLibraries, sharedDependencies, mappedRemotes } =
await getModuleFederationConfig(options, determineRemoteUrl, {
isServer: true,
});

return (config) => {
config = reactWebpackConfig(config);

config.target = false;
config.output.uniqueName = options.name;
config.optimization = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ function determineRemoteUrl(remote: string) {
export async function withModuleFederation(
options: ModuleFederationConfig
): Promise<AsyncNxWebpackPlugin> {
const reactWebpackConfig = require('../../plugins/webpack');

const { sharedDependencies, sharedLibraries, mappedRemotes } =
await getModuleFederationConfig(options, determineRemoteUrl);

return (config, ctx) => {
config = reactWebpackConfig(config, ctx);
config.output.uniqueName = options.name;
config.output.publicPath = 'auto';

Expand Down
17 changes: 9 additions & 8 deletions packages/webpack/src/utils/with-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getOutputHashFormat } from './hash-format';
import { PostcssCliResources } from './webpack/plugins/postcss-cli-resources';
import { normalizeExtraEntryPoints } from './webpack/normalize-entry';

import { NxWebpackPlugin } from './config';
import { NxWebpackExecutionContext, NxWebpackPlugin } from './config';
import {
ExtraEntryPointClass,
NormalizedWebpackExecutorOptions,
Expand All @@ -25,7 +25,6 @@ import CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
import MiniCssExtractPlugin = require('mini-css-extract-plugin');
import autoprefixer = require('autoprefixer');
import postcssImports = require('postcss-import');
import { NxWebpackExecutionContext } from './config';

interface PostcssOptions {
(loader: any): any;
Expand All @@ -47,6 +46,7 @@ export interface WithWebOptions {
stylePreprocessorOptions?: any;
styles?: Array<ExtraEntryPointClass | string>;
subresourceIntegrity?: boolean;
ssr?: boolean;
}

// Omit deprecated options
Expand Down Expand Up @@ -110,11 +110,13 @@ export function withWeb(pluginOptions: WithWebOptions = {}): NxWebpackPlugin {
})
);
}
plugins.push(
new webpack.DefinePlugin(
getClientEnvironment(process.env.NODE_ENV).stringified
)
);
if (!pluginOptions.ssr) {
plugins.push(
new webpack.DefinePlugin(
getClientEnvironment(process.env.NODE_ENV).stringified
)
);
}

const entry: { [key: string]: string[] } = {};
const globalStylePaths: string[] = [];
Expand Down Expand Up @@ -557,7 +559,6 @@ function getCommonLoadersForGlobalStyle(

function postcssOptionsCreator(
options: MergedOptions,

{
includePaths,
forCssModules = false,
Expand Down