UNPKG

@nx/rsbuild

Version:

The Nx Plugin for Rsbuild contains an Nx plugin, executors and utilities that support building applications using Rsbuild.

202 lines (201 loc) • 8.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createNodesV2 = void 0; const devkit_1 = require("@nx/devkit"); const get_named_inputs_1 = require("@nx/devkit/src/utils/get-named-inputs"); const file_hasher_1 = require("nx/src/hasher/file-hasher"); const cache_directory_1 = require("nx/src/utils/cache-directory"); const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup"); const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes"); const js_1 = require("@nx/js"); const fs_1 = require("fs"); const path_1 = require("path"); const minimatch_1 = require("minimatch"); const core_1 = require("@rsbuild/core"); const util_1 = require("@nx/js/src/plugins/typescript/util"); const pmc = (0, devkit_1.getPackageManagerCommand)(); function readTargetsCache(cachePath) { return (0, fs_1.existsSync)(cachePath) ? (0, devkit_1.readJsonFile)(cachePath) : {}; } function writeTargetsCache(cachePath, results) { (0, devkit_1.writeJsonFile)(cachePath, results); } const rsbuildConfigGlob = '**/rsbuild.config.{js,ts,mjs,mts,cjs,cts}'; exports.createNodesV2 = [ rsbuildConfigGlob, async (configFilePaths, options, context) => { const optionsHash = (0, file_hasher_1.hashObject)(options); const cachePath = (0, path_1.join)(cache_directory_1.workspaceDataDirectory, `rsbuild-${optionsHash}.hash`); const targetsCache = readTargetsCache(cachePath); const isUsingTsSolutionSetup = (0, ts_solution_setup_1.isUsingTsSolutionSetup)(); try { return await (0, devkit_1.createNodesFromFiles)((configFile, options, context) => createNodesInternal(configFile, options, context, targetsCache, isUsingTsSolutionSetup), configFilePaths, options, context); } finally { writeTargetsCache(cachePath, targetsCache); } }, ]; async function createNodesInternal(configFilePath, options, context, targetsCache, isUsingTsSolutionSetup) { const projectRoot = (0, path_1.dirname)(configFilePath); // Do not create a project if package.json and project.json isn't there. const siblingFiles = (0, fs_1.readdirSync)((0, path_1.join)(context.workspaceRoot, projectRoot)); if (!siblingFiles.includes('package.json') && !siblingFiles.includes('project.json')) { return {}; } const tsConfigFiles = siblingFiles.filter((p) => (0, minimatch_1.minimatch)(p, 'tsconfig*{.json,.*.json}')) ?? []; const normalizedOptions = normalizeOptions(options); const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, { ...normalizedOptions, isUsingTsSolutionSetup }, context, [(0, js_1.getLockFileName)((0, devkit_1.detectPackageManager)(context.workspaceRoot))]); targetsCache[hash] ??= await createRsbuildTargets(configFilePath, projectRoot, normalizedOptions, tsConfigFiles, isUsingTsSolutionSetup, context); const { targets, metadata } = targetsCache[hash]; return { projects: { [projectRoot]: { root: projectRoot, targets, metadata, }, }, }; } async function createRsbuildTargets(configFilePath, projectRoot, options, tsConfigFiles, isUsingTsSolutionSetup, context) { const absoluteConfigFilePath = (0, devkit_1.joinPathFragments)(context.workspaceRoot, configFilePath); const rsbuildConfig = await (0, core_1.loadConfig)({ path: absoluteConfigFilePath, }); if (!rsbuildConfig.filePath) { return { targets: {}, metadata: {} }; } const namedInputs = (0, get_named_inputs_1.getNamedInputs)(projectRoot, context); const { buildOutputs } = getOutputs(rsbuildConfig.content, projectRoot, context.workspaceRoot); const targets = {}; targets[options.buildTargetName] = { command: `rsbuild build`, options: { cwd: projectRoot, args: ['--mode=production'] }, cache: true, dependsOn: [`^${options.buildTargetName}`], inputs: [ ...('production' in namedInputs ? ['production', '^production'] : ['default', '^default']), { externalDependencies: ['@rsbuild/core'], }, ], outputs: buildOutputs, metadata: { technologies: ['rsbuild'], description: `Run Rsbuild build`, help: { command: `${pmc.exec} rsbuild build --help`, example: { options: { watch: false, }, }, }, }, }; targets[options.devTargetName] = { continuous: true, command: `rsbuild dev`, options: { cwd: projectRoot, args: ['--mode=development'], }, }; targets[options.previewTargetName] = { continuous: true, command: `rsbuild preview`, dependsOn: [`${options.buildTargetName}`, `^${options.buildTargetName}`], options: { cwd: projectRoot, args: ['--mode=production'], }, }; targets[options.inspectTargetName] = { command: `rsbuild inspect`, options: { cwd: projectRoot, }, }; if (tsConfigFiles.length) { const tsConfigToUse = ['tsconfig.app.json', 'tsconfig.lib.json', 'tsconfig.json'].find((t) => tsConfigFiles.includes(t)) ?? tsConfigFiles[0]; targets[options.typecheckTargetName] = { cache: true, inputs: [ ...('production' in namedInputs ? ['production', '^production'] : ['default', '^default']), { externalDependencies: ['typescript'] }, ], command: isUsingTsSolutionSetup ? `tsc --build --emitDeclarationOnly` : `tsc -p ${tsConfigToUse} --noEmit`, options: { cwd: (0, devkit_1.joinPathFragments)(projectRoot) }, metadata: { description: `Runs type-checking for the project.`, technologies: ['typescript'], help: { command: isUsingTsSolutionSetup ? `${pmc.exec} tsc --build --help` : `${pmc.exec} tsc -p ${tsConfigToUse} --help`, example: isUsingTsSolutionSetup ? { args: ['--force'] } : { options: { noEmit: true } }, }, }, }; if (isUsingTsSolutionSetup) { targets[options.typecheckTargetName].dependsOn = [ `^${options.typecheckTargetName}`, ]; targets[options.typecheckTargetName].syncGenerators = [ '@nx/js:typescript-sync', ]; } } (0, util_1.addBuildAndWatchDepsTargets)(context.workspaceRoot, projectRoot, targets, options, pmc); return { targets, metadata: {} }; } function getOutputs(rsbuildConfig, projectRoot, workspaceRoot) { const buildOutputPath = normalizeOutputPath(rsbuildConfig?.output?.distPath?.root ? (0, path_1.dirname)(rsbuildConfig?.output.distPath.root) : undefined, projectRoot, workspaceRoot, 'dist'); return { buildOutputs: [buildOutputPath], }; } function normalizeOutputPath(outputPath, projectRoot, workspaceRoot, path) { if (!outputPath) { if (projectRoot === '.') { return `{projectRoot}/${path}`; } else { return `{workspaceRoot}/${path}/{projectRoot}`; } } else { if ((0, path_1.isAbsolute)(outputPath)) { return `{workspaceRoot}/${(0, path_1.relative)(workspaceRoot, outputPath)}`; } else { if (outputPath.startsWith('..')) { return (0, path_1.join)('{workspaceRoot}', (0, path_1.join)(projectRoot, outputPath)); } else { return (0, path_1.join)('{projectRoot}', outputPath); } } } } function normalizeOptions(options) { options ??= {}; options.buildTargetName ??= 'build'; options.devTargetName ??= 'dev'; options.previewTargetName ??= 'preview'; options.inspectTargetName ??= 'inspect'; options.typecheckTargetName ??= 'typecheck'; return options; }