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-native): fix react native web configuration #29608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { TargetConfiguration, joinPathFragments } from '@nx/devkit';
import {
TargetConfiguration,
Tree,
joinPathFragments,
readProjectConfiguration,
} from '@nx/devkit';
import type { WithReactOptions } from '@nx/react';
import type { WithNxOptions } from '@nx/webpack';

import { NormalizedSchema } from './normalize-schema';

export function createBuildTarget(
tree: Tree,
options: NormalizedSchema
): TargetConfiguration {
return {
Expand All @@ -21,7 +27,10 @@ export function createBuildTarget(
index: joinPathFragments(options.projectRoot, 'src/index.html'),
baseHref: '/',
main: joinPathFragments(options.projectRoot, `src/main-web.tsx`),
tsConfig: joinPathFragments(options.projectRoot, 'tsconfig.app.json'),
tsConfig: joinPathFragments(
options.projectRoot,
determineTsConfig(tree, options)
),
assets: [
joinPathFragments(options.projectRoot, 'src/favicon.ico'),
joinPathFragments(options.projectRoot, 'src/assets'),
Expand Down Expand Up @@ -73,6 +82,7 @@ export function createServeTarget(
}

export function createNxWebpackPluginOptions(
tree: Tree,
options: NormalizedSchema
): WithNxOptions & WithReactOptions {
return {
Expand All @@ -85,8 +95,20 @@ export function createNxWebpackPluginOptions(
index: './src/index.html',
baseHref: '/',
main: `./src/main-web.tsx`,
tsConfig: './tsconfig.app.json',
tsConfig: determineTsConfig(tree, options),
assets: ['./src/favicon.ico', './src/assets'],
styles: [],
};
}

export function determineTsConfig(tree: Tree, options: NormalizedSchema) {
const project = readProjectConfiguration(tree, options.project);

const appJson = joinPathFragments(project.root, 'tsconfig.app.json');
if (tree.exists(appJson)) return 'tsconfig.app.json';

const libJson = joinPathFragments(project.root, 'tsconfig.lib.json');
if (tree.exists(libJson)) return 'tsconfig.lib.json';

return 'tsconfig.json';
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export async function webConfigurationGenerator(
}

// apply webpack or vite init generator
const bundlerTask = await addBundlerConfiguration(tree, normalizedSchema);
tasks.push(bundlerTask);
const bundlerTasks = await addBundlerConfiguration(tree, normalizedSchema);
tasks.push(...bundlerTasks);

// create files for webpack and vite config, index.html
if (normalizedSchema.bundler === 'vite') {
Expand All @@ -77,7 +77,7 @@ export async function webConfigurationGenerator(
...normalizedSchema,
tmpl: '',
webpackPluginOptions: hasWebpackPlugin(tree)
? createNxWebpackPluginOptions(normalizedSchema)
? createNxWebpackPluginOptions(tree, normalizedSchema)
: null,
}
);
Expand Down Expand Up @@ -110,7 +110,7 @@ export async function webConfigurationGenerator(
async function addBundlerConfiguration(
tree: Tree,
normalizedSchema: NormalizedSchema
) {
): Promise<GeneratorCallback[]> {
if (normalizedSchema.bundler === 'vite') {
const { viteConfigurationGenerator } = ensurePackage<
typeof import('@nx/vite')
Expand All @@ -125,8 +125,9 @@ async function addBundlerConfiguration(
compiler: 'babel',
skipFormat: true,
});
return viteTask;
return [viteTask];
} else {
let tasks: GeneratorCallback[] = [];
const { webpackInitGenerator } = ensurePackage<
typeof import('@nx/webpack')
>('@nx/webpack', nxVersion);
Expand All @@ -135,6 +136,13 @@ async function addBundlerConfiguration(
skipFormat: true,
skipPackageJson: normalizedSchema.skipPackageJson,
});
tasks.push(webpackInitTask);
if (!normalizedSchema.skipPackageJson) {
const { ensureDependencies } = await import(
'@nx/webpack/src/utils/ensure-dependencies'
);
tasks.push(ensureDependencies(tree, { uiFramework: 'react' }));
}

if (!hasWebpackPlugin(tree)) {
const projectConfiguration = readProjectConfiguration(
Expand All @@ -143,7 +151,7 @@ async function addBundlerConfiguration(
);
projectConfiguration.targets = {
...projectConfiguration.targets,
build: createBuildTarget(normalizedSchema),
build: createBuildTarget(tree, normalizedSchema),
serve: createServeTarget(normalizedSchema),
};
updateProjectConfiguration(
Expand All @@ -153,7 +161,7 @@ async function addBundlerConfiguration(
);
}

return webpackInitTask;
return tasks;
}
}

Expand Down
Loading