Skip to content

Commit

Permalink
fix(react): ensure rspack react app is added to exclude on rspack plu…
Browse files Browse the repository at this point in the history
…gin #28464
  • Loading branch information
Coly010 committed Oct 18, 2024
1 parent f971c1b commit e9228b2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,4 +1239,85 @@ describe('app', () => {
}
`);
});

it('should add project to excludes when @nx/rspack/plugin is setup as object and --bundler=rspack', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
let nxJson = readNxJson(tree);
delete nxJson.targetDefaults;
nxJson.plugins ??= [];
nxJson.plugins.push({
plugin: '@nx/rspack/plugin',
options: {
buildTargetName: 'build-base',
},
});
updateNxJson(tree, nxJson);

// ACT
await applicationGenerator(tree, {
directory: 'myapp',
addPlugin: true,
linter: Linter.None,
style: 'none',
bundler: 'rspack',
e2eTestRunner: 'none',
});

// ASSERT
nxJson = readNxJson(tree);
expect(
nxJson.plugins.find((p) =>
typeof p === 'string'
? p === '@nx/rspack/plugin'
: p.plugin === '@nx/rspack/plugin'
)
).toMatchInlineSnapshot(`
{
"exclude": [
"myapp/**",
],
"options": {
"buildTargetName": "build-base",
},
"plugin": "@nx/rspack/plugin",
}
`);
});
it('should add project to excludes when @nx/rspack/plugin is setup as string and --bundler=rspack', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
let nxJson = readNxJson(tree);
delete nxJson.targetDefaults;
nxJson.plugins ??= [];
nxJson.plugins.push('@nx/rspack/plugin');
updateNxJson(tree, nxJson);

// ACT
await applicationGenerator(tree, {
directory: 'myapp',
addPlugin: true,
linter: Linter.None,
style: 'none',
bundler: 'rspack',
e2eTestRunner: 'none',
});

// ASSERT
nxJson = readNxJson(tree);
expect(
nxJson.plugins.find((p) =>
typeof p === 'string'
? p === '@nx/rspack/plugin'
: p.plugin === '@nx/rspack/plugin'
)
).toMatchInlineSnapshot(`
{
"exclude": [
"myapp/**",
],
"plugin": "@nx/rspack/plugin",
}
`);
});
});
2 changes: 2 additions & 0 deletions packages/react/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-com
import { setupTailwindGenerator } from '../setup-tailwind/setup-tailwind';
import { useFlatConfig } from '@nx/eslint/src/utils/flat-config';
import { assertNotUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { addProjectRootToRspackPluginExcludesIfExists } from './lib/add-project-root-to-rspack-plugin-excludes';

async function addLinting(host: Tree, options: NormalizedSchema) {
const tasks: GeneratorCallback[] = [];
Expand Down Expand Up @@ -248,6 +249,7 @@ export async function applicationGeneratorInternal(
framework: 'react',
});
tasks.push(rspackTask);
addProjectRootToRspackPluginExcludesIfExists(host, options.appProjectRoot);
}

if (options.bundler !== 'vite' && options.unitTestRunner === 'vitest') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { joinPathFragments, Tree, updateNxJson } from '@nx/devkit';
import { readNxJson } from '@nx/devkit';

export function addProjectRootToRspackPluginExcludesIfExists(
tree: Tree,
projectRoot: string
) {
const excludeProjectGlob = joinPathFragments(projectRoot, '/**');
const nxJson = readNxJson(tree);
if (!nxJson.plugins?.length) {
return;
}
for (let i = 0; i < nxJson.plugins.length; i++) {
let plugin = nxJson.plugins[i];
const isRspackPlugin =
typeof plugin === 'string'
? plugin === '@nx/rspack/plugin'
: plugin.plugin === '@nx/rspack/plugin';
if (isRspackPlugin) {
if (typeof plugin === 'string') {
plugin = {
plugin: plugin,
exclude: [excludeProjectGlob],
};
} else {
plugin.exclude = [...(plugin.exclude ?? []), excludeProjectGlob];
}
nxJson.plugins[i] = plugin;
}
}
updateNxJson(tree, nxJson);
}

0 comments on commit e9228b2

Please sign in to comment.