UNPKG

@nx/angular-rsbuild

Version:

Rsbuild Plugin for building Angular.

204 lines (203 loc) 7.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_PLUGIN_ANGULAR_OPTIONS = void 0; exports.resolveFileReplacements = resolveFileReplacements; exports.getHasServer = getHasServer; exports.validateSsr = validateSsr; exports.validateOptimization = validateOptimization; exports.normalizeOptions = normalizeOptions; exports.normalizeOutputPath = normalizeOutputPath; const node_path_1 = require("node:path"); const node_fs_1 = require("node:fs"); /** * Resolves file replacement paths to absolute paths based on the provided root directory. * * @param fileReplacements - Array of file replacements with relative paths. * @param root - The root directory to resolve the paths against. * @returns Array of file replacements resolved against the root. */ function resolveFileReplacements(fileReplacements, root) { return fileReplacements.map((fileReplacement) => ({ replace: (0, node_path_1.resolve)(root, fileReplacement.replace), with: (0, node_path_1.resolve)(root, fileReplacement.with), })); } function getHasServer(root, server, ssr) { return !!(server && ssr && ssr.entry && (0, node_fs_1.existsSync)((0, node_path_1.join)(root, server)) && (0, node_fs_1.existsSync)((0, node_path_1.join)(root, ssr.entry))); } function validateSsr(ssr) { if (!ssr) { return; } if (ssr === true) { throw new Error('The "ssr" option should be an object or false. Please check the documentation.'); } if (typeof ssr === 'object') if (!ssr.entry) { throw new Error('The "ssr" option should have an "entry" property. Please check the documentation.'); } else if (ssr.experimentalPlatform === 'neutral') { console.warn('The "ssr.experimentalPlatform" option is not currently supported. Node will be used as the platform.'); } } function validateOptimization(optimization) { if (typeof optimization === 'boolean' || optimization === undefined) { return; } if (typeof optimization === 'object') console.warn('The "optimization" option currently only supports a boolean value. Please check the documentation.'); } function validateChunkOptions(options) { if (options.namedChunks !== undefined) { console.warn(`The "namedChunks" option is not supported with Rsbuild.`); } if (options.commonChunk !== undefined) { console.warn(`The "commonChunk" option is not supported with Rsbuild.`); } if (options.vendorChunk !== undefined) { console.warn(`The "vendorChunk" option is not supported with Rsbuild.`); } } exports.DEFAULT_PLUGIN_ANGULAR_OPTIONS = { index: './src/index.html', browser: './src/main.ts', define: {}, deleteOutputPath: true, externalDependencies: [], server: undefined, ssr: undefined, fileReplacements: [], hasServer: false, polyfills: [], assets: ['./public'], styles: ['./src/styles.css'], scripts: [], aot: true, inlineStyleLanguage: 'css', tsConfig: (0, node_path_1.join)(process.cwd(), 'tsconfig.app.json'), optimization: true, outputPath: normalizeOutputPath(process.cwd(), undefined), outputHashing: 'all', sourceMap: false, useTsProjectReferences: false, namedChunks: false, skipTypeChecking: false, devServer: { port: 4200, }, }; function normalizeOptions(options = {}) { const root = options.root ?? process.cwd(); const { fileReplacements = [], server, ssr, optimization, devServer, define, deleteOutputPath, externalDependencies, ...restOptions } = options; validateSsr(ssr); const normalizedSsr = !ssr ? false : typeof ssr === 'object' ? { entry: ssr.entry, experimentalPlatform: 'node', // @TODO: Add support for neutral platform } : ssr; validateOptimization(optimization); const normalizedOptimization = optimization !== false; // @TODO: Add support for optimization options const aot = options.aot ?? true; const advancedOptimizations = aot && normalizedOptimization; const tsConfig = options.tsConfig ? (0, node_path_1.resolve)(root, options.tsConfig) : (0, node_path_1.join)(root, 'tsconfig.app.json'); validateChunkOptions(options); return { ...exports.DEFAULT_PLUGIN_ANGULAR_OPTIONS, ...restOptions, ...(server != null ? { server } : {}), ...(ssr != null ? { ssr: normalizedSsr } : {}), ...(define != null ? { define } : {}), ...(deleteOutputPath != null ? { deleteOutputPath } : {}), externalDependencies: externalDependencies ?? [], optimization: normalizedOptimization, outputPath: normalizeOutputPath(root, options.outputPath), sourceMap: normalizeSourceMap(options.sourceMap), advancedOptimizations, aot, outputHashing: options.outputHashing ?? 'all', namedChunks: options.namedChunks ?? false, fileReplacements: resolveFileReplacements(fileReplacements, root), hasServer: getHasServer(root, server, normalizedSsr), devServer: normalizeDevServer(devServer), root, tsConfig, }; } function normalizeSourceMap(sourceMap) { if (sourceMap === undefined || sourceMap === true) { return { scripts: true, styles: true, hidden: false, vendor: false, }; } if (sourceMap === false) { return { scripts: false, styles: false, hidden: false, vendor: false, }; } return { scripts: sourceMap.scripts ?? true, styles: sourceMap.styles ?? true, hidden: sourceMap.hidden ?? false, vendor: sourceMap.vendor ?? false, }; } function normalizeDevServer(devServer) { const defaultPort = 4200; if (!devServer) { return { port: defaultPort }; } return { ...devServer, port: devServer.port ?? defaultPort, }; } /** * This is slightly different to the Rspack solution as Rsbuild will use only relative paths * from the base directory provided as the outputPath. */ function normalizeOutputPath(root, outputPath) { const defaultBase = (0, node_path_1.join)(root, 'dist'); const defaultBrowser = (0, node_path_1.join)(defaultBase, 'browser'); if (!outputPath) { return { base: defaultBase, browser: defaultBrowser, server: (0, node_path_1.join)(defaultBase, 'server'), media: 'media', }; } if (typeof outputPath === 'string') { if (!outputPath.startsWith(root)) { outputPath = (0, node_path_1.join)(root, outputPath); } return { base: outputPath, browser: (0, node_path_1.join)(outputPath, 'browser'), server: (0, node_path_1.join)(outputPath, 'server'), media: 'media', }; } const providedBase = outputPath.base ?? defaultBase; const providedBrowser = outputPath.browser ?? (0, node_path_1.join)(providedBase, 'browser'); return { base: providedBase, browser: providedBrowser, server: outputPath.server ?? (0, node_path_1.join)(outputPath.base ?? defaultBase, 'server'), media: outputPath.media ?? 'media', }; }