UNPKG

@nx/rspack

Version:

The Nx Plugin for Rspack contains executors and generators that support building applications using Rspack.

142 lines (141 loc) 5.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const tslib_1 = require("tslib"); const devkit_1 = require("@nx/devkit"); const package_json_1 = require("nx/package.json"); const configuration_1 = tslib_1.__importDefault(require("../configuration/configuration")); const init_1 = tslib_1.__importDefault(require("../init/init")); const normalize_options_1 = require("./lib/normalize-options"); /** * Updates the exclude field for any @nx/rspack/plugin registrations in nx.json */ function updateRspackPluginExclusion(tree, options) { const nxJson = (0, devkit_1.readNxJson)(tree); if (!nxJson.plugins?.length) { return; } let updated = false; // Loop through all plugins to find @nx/rspack/plugin registrations for (let i = 0; i < nxJson.plugins.length; i++) { const plugin = nxJson.plugins[i]; const isRspackPlugin = typeof plugin === 'string' ? plugin === '@nx/rspack/plugin' : plugin.plugin === '@nx/rspack/plugin'; if (!isRspackPlugin) { continue; } if (typeof plugin === 'string') { // Convert string notation to object notation with exclude field nxJson.plugins[i] = { plugin: '@nx/rspack/plugin', exclude: [`${options.appProjectRoot}/**`], }; updated = true; } else { // Object notation if (!plugin.exclude) { // Add exclude field if it doesn't exist plugin.exclude = [`${options.appProjectRoot}/**`]; updated = true; } else if (Array.isArray(plugin.exclude)) { // Add to existing exclude field if it's an array plugin.exclude.push(`${options.appProjectRoot}/**`); updated = true; } } } if (updated) { (0, devkit_1.updateNxJson)(tree, nxJson); } } // TODO(v22) - remove this generator async function default_1(tree, _options) { // Add deprecation warning with alternatives based on framework const framework = _options.framework || 'react'; // Default is react devkit_1.logger.warn(`The @nx/rspack:application generator is deprecated and will be removed in Nx 22. ` + `Please use @nx/${framework === 'nest' ? 'nest' : framework === 'web' ? 'web' : 'react'}:application instead.`); const tasks = []; const initTask = await (0, init_1.default)(tree, { ..._options, }); tasks.push(initTask); const options = await (0, normalize_options_1.normalizeOptions)(tree, _options); if (framework === 'nest') { updateRspackPluginExclusion(tree, options); } options.style ??= 'css'; if (options.framework === 'nest') { const { applicationGenerator: nestAppGenerator } = (0, devkit_1.ensurePackage)('@nx/nest', package_json_1.version); const createAppTask = await nestAppGenerator(tree, { ...options, skipFormat: true, tags: options.tags ?? '', addPlugin: false, }); const convertAppTask = await (0, configuration_1.default)(tree, { project: options.name, target: 'node', newProject: false, buildTarget: 'build', framework: 'nest', addPlugin: false, }); tasks.push(createAppTask, convertAppTask); } else if (options.framework === 'web') { const { applicationGenerator: webAppGenerator } = (0, devkit_1.ensurePackage)('@nx/web', package_json_1.version); const createAppTask = await webAppGenerator(tree, { bundler: 'webpack', name: options.name, style: options.style, directory: options.directory, tags: options.tags ?? '', unitTestRunner: options.unitTestRunner, e2eTestRunner: options.e2eTestRunner, rootProject: options.rootProject, skipFormat: true, addPlugin: false, }); const convertAppTask = await (0, configuration_1.default)(tree, { project: options.name, target: 'web', newProject: false, buildTarget: 'build', serveTarget: 'serve', framework: 'web', addPlugin: false, }); tasks.push(createAppTask, convertAppTask); } else { // default to react const { applicationGenerator: reactAppGenerator } = (0, devkit_1.ensurePackage)('@nx/react', package_json_1.version); const createAppTask = await reactAppGenerator(tree, { bundler: 'webpack', name: options.name, style: options.style, directory: options.directory, tags: options.tags ?? '', unitTestRunner: options.unitTestRunner, e2eTestRunner: options.e2eTestRunner, rootProject: options.rootProject, skipFormat: true, addPlugin: false, }); const convertAppTask = await (0, configuration_1.default)(tree, { project: options.name, target: 'web', newProject: false, buildTarget: 'build', serveTarget: 'serve', framework: 'react', addPlugin: false, }); tasks.push(createAppTask, convertAppTask); } await (0, devkit_1.formatFiles)(tree); return (0, devkit_1.runTasksInSerial)(...tasks); }