UNPKG

@nx/angular-rsbuild

Version:

Rsbuild Plugin for building Angular.

110 lines (109 loc) 4.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProxyConfig = getProxyConfig; exports.getJsonErrorLineColumn = getJsonErrorLineColumn; exports.normalizeProxyConfiguration = normalizeProxyConfiguration; exports.assertIsError = assertIsError; const tslib_1 = require("tslib"); const node_assert_1 = tslib_1.__importDefault(require("node:assert")); const node_fs_1 = require("node:fs"); const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); const node_url_1 = require("node:url"); const load_esm_1 = require("./load-esm"); async function getProxyConfig(root, proxyConfig) { if (!proxyConfig) { return undefined; } const proxyPath = (0, node_path_1.resolve)(root, proxyConfig); if (!(0, node_fs_1.existsSync)(proxyPath)) { throw new Error(`Proxy configuration file ${proxyPath} does not exist.`); } let proxyConfiguration; switch ((0, node_path_1.extname)(proxyPath)) { case '.json': { const content = await (0, promises_1.readFile)(proxyPath, 'utf-8'); const { parse, printParseErrorCode } = await Promise.resolve().then(() => tslib_1.__importStar(require('jsonc-parser'))); const parseErrors = []; proxyConfiguration = parse(content, parseErrors, { allowTrailingComma: true, }); if (parseErrors.length > 0) { let errorMessage = `Proxy configuration file ${proxyPath} contains parse errors:`; for (const parseError of parseErrors) { const { line, column } = getJsonErrorLineColumn(parseError.offset, content); errorMessage += `\n[${line}, ${column}] ${printParseErrorCode(parseError.error)}`; } throw new Error(errorMessage); } break; } case '.mjs': // Load the ESM configuration file using the TypeScript dynamic import workaround. // Once TypeScript provides support for keeping the dynamic import this workaround can be // changed to a direct dynamic import. proxyConfiguration = (await (0, load_esm_1.loadEsmModule)((0, node_url_1.pathToFileURL)(proxyPath))).default; break; case '.cjs': proxyConfiguration = require(proxyPath); break; default: // The file could be either CommonJS or ESM. // CommonJS is tried first then ESM if loading fails. try { proxyConfiguration = require(proxyPath); } catch (e) { assertIsError(e); if (e.code !== 'ERR_REQUIRE_ESM') { throw e; } // Load the ESM configuration file using the TypeScript dynamic import workaround. // Once TypeScript provides support for keeping the dynamic import this workaround can be // changed to a direct dynamic import. proxyConfiguration = (await (0, load_esm_1.loadEsmModule)((0, node_url_1.pathToFileURL)(proxyPath))).default; } } return normalizeProxyConfiguration(proxyConfiguration); } function getJsonErrorLineColumn(offset, content) { if (offset === 0) { return { line: 1, column: 1 }; } let line = 0; let position = 0; while (true) { ++line; const nextNewline = content.indexOf('\n', position); if (nextNewline === -1 || nextNewline > offset) { break; } position = nextNewline + 1; } return { line, column: offset - position + 1 }; } /** * Normalizes the proxy configuration to ensure a consistent format. * If the input is an array, it is returned as-is. * If the input is an object, it is transformed into an array of objects. */ function normalizeProxyConfiguration(proxy) { return Array.isArray(proxy) ? proxy : Object.entries(proxy).map(([context, value]) => ({ context: [context], ...value, })); } /** * Asserts that the value is an Error instance or an object with name and message properties. */ function assertIsError(value) { const isError = value instanceof Error || // The following is needing to identify errors coming from RxJs. (typeof value === 'object' && value && 'name' in value && 'message' in value); (0, node_assert_1.default)(isError, 'catch clause variable is not an Error instance'); }