UNPKG

@nx/rspack

Version:

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

93 lines (90 loc) 5.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertConfigToRspackPluginGenerator = convertConfigToRspackPluginGenerator; const devkit_1 = require("@nx/devkit"); const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils"); const extract_rspack_options_1 = require("./lib/extract-rspack-options"); const normalize_path_options_1 = require("./lib/normalize-path-options"); const path_1 = require("path"); const validate_project_1 = require("./lib/validate-project"); // Make text JSON compatible const preprocessText = (text) => { return text .replace(/(\w+):/g, '"$1":') // Quote property names .replace(/'/g, '"') // Convert single quotes to double quotes .replace(/,(\s*[}\]])/g, '$1') // Remove trailing commas .replace(/(\r\n|\n|\r|\t)/gm, ''); // Remove newlines and tabs }; async function convertConfigToRspackPluginGenerator(tree, options) { let migrated = 0; const projects = (0, devkit_1.getProjects)(tree); (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nx/rspack:rspack', (currentTargetOptions, projectName, targetName, configurationName) => { if (options.project && projectName !== options.project) { return; } if (!configurationName) { const project = projects.get(projectName); const target = project.targets[targetName]; const hasError = (0, validate_project_1.validateProject)(tree, project); if (hasError) { throw new Error(hasError); } const rspackConfigPath = currentTargetOptions?.rspackConfig || ''; if (rspackConfigPath && tree.exists(rspackConfigPath)) { let { withNxConfig: rspackOptions, withReactConfig } = (0, extract_rspack_options_1.extractRspackOptions)(tree, rspackConfigPath); // if rspackOptions === undefined // withNx was not found in the rspack.config.js file so we should skip this project if (rspackOptions !== undefined) { let parsedOptions = {}; if (rspackOptions) { parsedOptions = JSON.parse(preprocessText(rspackOptions.getText())); parsedOptions = (0, normalize_path_options_1.normalizePathOptions)(project.root, parsedOptions); } target.options.standardRspackConfigFunction = true; (0, devkit_1.updateProjectConfiguration)(tree, projectName, project); const { dir, name, ext } = (0, path_1.parse)(rspackConfigPath); tree.rename(rspackConfigPath, `${(0, devkit_1.joinPathFragments)(dir, `${name}.old${ext}`)}`); tree.write(rspackConfigPath, (0, devkit_1.stripIndents) ` const { NxAppRspackPlugin } = require('@nx/rspack/app-plugin'); const { NxReactRspackPlugin } = require('@nx/rspack/react-plugin'); const { useLegacyNxPlugin } = require('@nx/rspack'); // This file was migrated using @nx/rspack:convert-config-to-rspack-plugin from your './rspack.config.old.js' // Please check that the options here are correct as they were moved from the old rspack.config.js to this file. const options = ${rspackOptions ? JSON.stringify(parsedOptions, null, 2) : '{}'}; /** * @type{import('@rspack/core').RspackOptionsNormalized} */ module.exports = async () => ({ plugins: [ ${rspackOptions ? 'new NxAppRspackPlugin(options)' : 'new NxAppRspackPlugin()'}, ${withReactConfig ? `new NxReactRspackPlugin(${withReactConfig.getText()})` : `new NxReactRspackPlugin({ // Uncomment this line if you don't want to use SVGR // See: https://react-svgr.com/ // svgr: false })`}, // NOTE: useLegacyNxPlugin ensures that the non-standard Rspack configuration file previously used still works. // To remove its usage, move options such as "plugins" into this file as standard Rspack configuration options. // To enhance configurations after Nx plugins have applied, you can add a new plugin with the \`apply\` method. // e.g. \`{ apply: (compiler) => { /* modify compiler.options */ }\` // eslint-disable-next-line react-hooks/rules-of-hooks await useLegacyNxPlugin(require('./rspack.config.old'), options), ], }); `); migrated++; } } } }); if (migrated === 0) { throw new Error('Could not find any projects to migrate.'); } if (!options.skipFormat) { await (0, devkit_1.formatFiles)(tree); } } exports.default = convertConfigToRspackPluginGenerator;