Skip to content

Commit

Permalink
feat(rspack): Update configuration generator to support NxWebpackAppP…
Browse files Browse the repository at this point in the history
…lugin
  • Loading branch information
ndcunningham committed Nov 21, 2024
1 parent 1c2ae37 commit 1944ac9
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 101 deletions.
55 changes: 33 additions & 22 deletions packages/react/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ export async function applicationGeneratorInternal(
);
tasks.push(ensureDependencies(host, { uiFramework: 'react' }));
}
} else if (options.bundler === 'rspack') {
const { rspackInitGenerator } = ensurePackage(
'@nx/rspack',
nxRspackVersion
);
const rspackInitTask = await rspackInitGenerator(host, {
...options,
addPlugin: false,
skipFormat: true,
});
tasks.push(rspackInitTask);
}

if (!options.rootProject) {
Expand Down Expand Up @@ -227,28 +238,28 @@ export async function applicationGeneratorInternal(
false
);
} else if (options.bundler === 'rspack') {
const { configurationGenerator } = ensurePackage(
'@nx/rspack',
nxRspackVersion
);
const rspackTask = await configurationGenerator(host, {
project: options.projectName,
main: joinPathFragments(
options.appProjectRoot,
maybeJs(
{
js: options.js,
useJsx: true,
},
`src/main.tsx`
)
),
tsConfig: joinPathFragments(options.appProjectRoot, 'tsconfig.app.json'),
target: 'web',
newProject: true,
framework: 'react',
});
tasks.push(rspackTask);
// const { configurationGenerator } = ensurePackage(
// '@nx/rspack',
// nxRspackVersion
// );
// const rspackTask = await configurationGenerator(host, {
// project: options.projectName,
// main: joinPathFragments(
// options.appProjectRoot,
// maybeJs(
// {
// js: options.js,
// useJsx: true,
// },
// `src/main.tsx`
// )
// ),
// tsConfig: joinPathFragments(options.appProjectRoot, 'tsconfig.app.json'),
// target: 'web',
// newProject: true,
// framework: 'react',
// });
// tasks.push(rspackTask);
addProjectRootToRspackPluginExcludesIfExists(host, options.appProjectRoot);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<%_ if (rspackPluginOptions) { _%>
const { NxAppRspackPlugin } = require('@nx/rspack/app-plugin');
const { NxReactRspackPlugin } = require('@nx/rspack/react-plugin');
const { join } = require('path');

module.exports = {
output: {
path: join(__dirname, '<%= offsetFromRoot %><%= rspackPluginOptions.outputPath %>'),
},
devServer: {
port: 4200,
historyApiFallback: {
index: '/index.html',
disableDotRule: true,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
},
},
plugins: [
new NxAppRspackPlugin({
tsConfig: '<%= rspackPluginOptions.tsConfig %>',
main: '<%= rspackPluginOptions.main %>',
index: '<%= rspackPluginOptions.index %>',
baseHref: '<%= rspackPluginOptions.baseHref %>',
assets: <%- JSON.stringify(rspackPluginOptions.assets) %>,
styles: <%- JSON.stringify(rspackPluginOptions.styles) %>,
outputHashing: process.env['NODE_ENV'] === 'production' ? 'all' : 'none',
optimization: process.env['NODE_ENV'] === 'production',
}),
new NxReactRspackPlugin({
// Uncomment this line if you don't want to use SVGR
// See: https://react-svgr.com/
// svgr: false
}),
],
};
<%_ } else { _%>
const { composePlugins, withNx, withReact } = require('@nx/rspack');

// Nx plugins for rspack.
module.exports = composePlugins(
withNx(),
withReact({
// Uncomment this line if you don't want to use SVGR
// See: https://react-svgr.com/
// svgr: false
}),
(config) => {
// Update the rspack config as needed here.
// e.g. `config.plugins.push(new MyPlugin())`
return config;
}
);
<%_ } _%>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getNxCloudAppOnBoardingUrl,
createNxCloudOnboardingURLForWelcomeApp,
} from 'nx/src/nx-cloud/utilities/onboarding';
import { hasRspackPlugin } from '../../../utils/has-rspack-plugin';

export async function createApplicationFiles(
host: Tree,
Expand Down Expand Up @@ -145,7 +146,12 @@ export async function createApplicationFiles(
host,
join(__dirname, '../files/base-rspack'),
options.appProjectRoot,
templateVariables
{
...templateVariables,
rspackPluginOptions: hasRspackPlugin(host)
? createNxRspackPluginOptions(options)
: null,
}
);
}

Expand Down Expand Up @@ -226,3 +232,36 @@ function createNxWebpackPluginOptions(
],
};
}

function createNxRspackPluginOptions(
options: NormalizedSchema
): WithNxOptions & WithReactOptions {
return {
target: 'web',
outputPath: joinPathFragments(
'dist',
options.appProjectRoot != '.'
? options.appProjectRoot
: options.projectName
),
index: './src/index.html',
baseHref: '/',
main: maybeJs(
{
js: options.js,
useJsx: true,
},
`./src/main.tsx`
),
tsConfig: './tsconfig.app.json',
assets: ['./src/favicon.ico', './src/assets'],
styles:
options.styledModule || !options.hasStyles
? []
: [
`./src/styles.${
options.style !== 'tailwind' ? options.style : 'css'
}`,
],
};
}
10 changes: 10 additions & 0 deletions packages/react/src/utils/has-rspack-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { readNxJson, Tree } from '@nx/devkit';

export function hasRspackPlugin(tree: Tree) {
const nxJson = readNxJson(tree);
return !!nxJson.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/rspack/plugin'
: p.plugin === '@nx/rspack/plugin'
);
}
Loading

0 comments on commit 1944ac9

Please sign in to comment.