UNPKG

@nx/angular-rsbuild

Version:

Rsbuild Plugin for building Angular.

102 lines (101 loc) 4.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProxyConfig = getProxyConfig; const tslib_1 = require("tslib"); const node_assert_1 = tslib_1.__importDefault(require("node:assert")); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const node_url_1 = require("node:url"); 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 node_fs_1.promises.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 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 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 }; } function normalizeProxyConfiguration(proxy) { return Array.isArray(proxy) ? proxy : Object.entries(proxy).map(([context, value]) => ({ context: [context], ...value, })); } 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'); } let load; function loadEsmModule(modulePath) { load ??= new Function('modulePath', `return import(modulePath);`); return load(modulePath); }