UNPKG

@nx/rsbuild

Version:

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

150 lines (149 loc) • 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addHtmlTemplatePath = addHtmlTemplatePath; exports.addCopyAssets = addCopyAssets; exports.addExperimentalSwcPlugin = addExperimentalSwcPlugin; exports.addSourceDefine = addSourceDefine; const indent_by_1 = require("./indent-by"); const tsquery_1 = require("@phenomnomnominal/tsquery"); const DEFINE_CONFIG_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) > ObjectLiteralExpression'; function addHtmlTemplatePath(tree, configFilePath, templatePath) { const HTML_CONFIG_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=html]) > ObjectLiteralExpression'; const TEMPLATE_STRING_SELECTOR = 'PropertyAssignment:has(Identifier[name=template]) > StringLiteral'; let configContents = tree.read(configFilePath, 'utf-8'); const ast = tsquery_1.tsquery.ast(configContents); const htmlConfigNodes = (0, tsquery_1.tsquery)(ast, HTML_CONFIG_SELECTOR); if (htmlConfigNodes.length === 0) { const defineConfigNodes = (0, tsquery_1.tsquery)(ast, DEFINE_CONFIG_SELECTOR); if (defineConfigNodes.length === 0) { throw new Error(`Could not find 'defineConfig' in the config file at ${configFilePath}.`); } const defineConfigNode = defineConfigNodes[0]; configContents = `${configContents.slice(0, defineConfigNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(1)(`html: {\n${(0, indent_by_1.indentBy)(1)(`template: '${templatePath}'`)}\n},`)}\t${configContents.slice(defineConfigNode.getStart() + 1)}`; } else { const htmlConfigNode = htmlConfigNodes[0]; const templateStringNodes = (0, tsquery_1.tsquery)(htmlConfigNode, TEMPLATE_STRING_SELECTOR); if (templateStringNodes.length === 0) { configContents = `${configContents.slice(0, htmlConfigNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(2)(`template: '${templatePath}',`)}\n\t\t${configContents.slice(htmlConfigNode.getStart() + 1)}`; } else { const templateStringNode = templateStringNodes[0]; configContents = `${configContents.slice(0, templateStringNode.getStart())}'${templatePath}',${configContents.slice(templateStringNode.getEnd())}`; } } tree.write(configFilePath, configContents); } function addCopyAssets(tree, configFilePath, from) { const OUTPUT_CONFIG_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=output]) > ObjectLiteralExpression'; const COPY_ARRAY_SELECTOR = 'PropertyAssignment:has(Identifier[name=copy]) > ArrayLiteralExpression'; const copyAssetArrayElement = `{ from: '${from}' }`; let configContents = tree.read(configFilePath, 'utf-8'); const ast = tsquery_1.tsquery.ast(configContents); const outputConfigNodes = (0, tsquery_1.tsquery)(ast, OUTPUT_CONFIG_SELECTOR); if (outputConfigNodes.length === 0) { const defineConfigNodes = (0, tsquery_1.tsquery)(ast, DEFINE_CONFIG_SELECTOR); if (defineConfigNodes.length === 0) { throw new Error(`Could not find 'defineConfig' in the config file at ${configFilePath}.`); } const defineConfigNode = defineConfigNodes[0]; configContents = `${configContents.slice(0, defineConfigNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(1)(`output: {\n${(0, indent_by_1.indentBy)(2)(`copy: [${copyAssetArrayElement}]`)},\n}`)},${configContents.slice(defineConfigNode.getStart() + 1)}`; } else { const outputConfigNode = outputConfigNodes[0]; const copyAssetsArrayNodes = (0, tsquery_1.tsquery)(outputConfigNode, COPY_ARRAY_SELECTOR); if (copyAssetsArrayNodes.length === 0) { configContents = `${configContents.slice(0, outputConfigNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(2)(`copy: [${copyAssetArrayElement}],`)}\n\t${configContents.slice(outputConfigNode.getStart() + 1)}`; } else { const copyAssetsArrayNode = copyAssetsArrayNodes[0]; configContents = `${configContents.slice(0, copyAssetsArrayNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(2)(copyAssetArrayElement)},\n\t\t${configContents.slice(copyAssetsArrayNode.getStart() + 1)}`; } } tree.write(configFilePath, configContents); } function addExperimentalSwcPlugin(tree, configFilePath, pluginName) { const SWC_JSC_EXPERIMENTAL_PLUGIN_ARRAY_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=tools]) PropertyAssignment:has(Identifier[name=swc]) PropertyAssignment:has(Identifier[name=jsc]) PropertyAssignment:has(Identifier[name=experimental]) PropertyAssignment:has(Identifier[name=plugins]) > ArrayLiteralExpression'; const SWC_JSC_EXPERIMENTAL_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=tools]) PropertyAssignment:has(Identifier[name=swc]) PropertyAssignment:has(Identifier[name=jsc]) PropertyAssignment:has(Identifier[name=experimental]) > ObjectLiteralExpression'; const SWC_JSC_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=tools]) PropertyAssignment:has(Identifier[name=swc]) PropertyAssignment:has(Identifier[name=jsc]) > ObjectLiteralExpression'; const SWC_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=tools]) PropertyAssignment:has(Identifier[name=swc]) > ObjectLiteralExpression'; const TOOLS_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=tools]) > ObjectLiteralExpression'; let configContents = tree.read(configFilePath, 'utf-8'); const ast = tsquery_1.tsquery.ast(configContents); const pluginToAdd = (0, indent_by_1.indentBy)(1)(`['${pluginName}', {}],`); const pluginsArrayToAdd = (0, indent_by_1.indentBy)(1)(`plugins: [\n${pluginToAdd}\n],`); const experimentalObjectToAdd = (0, indent_by_1.indentBy)(1)(`experimental: {\n${pluginsArrayToAdd} \n},`); const jscObjectToAdd = (0, indent_by_1.indentBy)(1)(`jsc: {\n${experimentalObjectToAdd}\n},`); const swcObjectToAdd = (0, indent_by_1.indentBy)(1)(`swc: {\n${jscObjectToAdd}\n},`); const toolsObjectToAdd = (0, indent_by_1.indentBy)(1)(`tools: {\n${swcObjectToAdd}\n},`); const toolsNodes = (0, tsquery_1.tsquery)(ast, TOOLS_SELECTOR); if (toolsNodes.length === 0) { const defineConfigNodes = (0, tsquery_1.tsquery)(ast, DEFINE_CONFIG_SELECTOR); if (defineConfigNodes.length === 0) { throw new Error(`Could not find 'defineConfig' in the config file at ${configFilePath}.`); } const defineConfigNode = defineConfigNodes[0]; configContents = `${configContents.slice(0, defineConfigNode.getStart() + 1)}\n${toolsObjectToAdd}${configContents.slice(defineConfigNode.getStart() + 1)}`; } else { const swcNodes = (0, tsquery_1.tsquery)(ast, SWC_SELECTOR); if (swcNodes.length === 0) { const toolsNode = toolsNodes[0]; configContents = `${configContents.slice(0, toolsNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(1)(swcObjectToAdd)}\n\t${configContents.slice(toolsNode.getStart() + 1)}`; } else { const jscNodes = (0, tsquery_1.tsquery)(ast, SWC_JSC_SELECTOR); if (jscNodes.length === 0) { const swcNode = swcNodes[0]; configContents = `${configContents.slice(0, swcNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(2)(jscObjectToAdd)}\n\t\t${configContents.slice(swcNode.getStart() + 1)}`; } else { const experimentalNodes = (0, tsquery_1.tsquery)(ast, SWC_JSC_EXPERIMENTAL_SELECTOR); if (experimentalNodes.length === 0) { const jscNode = jscNodes[0]; configContents = `${configContents.slice(0, jscNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(3)(experimentalObjectToAdd)}\n\t\t\t${configContents.slice(jscNode.getStart() + 1)}`; } else { const pluginsArrayNodes = (0, tsquery_1.tsquery)(ast, SWC_JSC_EXPERIMENTAL_PLUGIN_ARRAY_SELECTOR); if (pluginsArrayNodes.length === 0) { const experimentalNode = experimentalNodes[0]; configContents = `${configContents.slice(0, experimentalNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(4)(pluginsArrayToAdd)}\n\t\t\t\t${configContents.slice(experimentalNode.getStart() + 1)}`; } else { const pluginsArrayNode = pluginsArrayNodes[0]; configContents = `${configContents.slice(0, pluginsArrayNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(4)(pluginToAdd)}\n\t\t\t\t\t${configContents.slice(pluginsArrayNode.getStart() + 1)}`; } } } } } tree.write(configFilePath, configContents); } function addSourceDefine(tree, configFilePath, key, value) { const SOURCE_CONFIG_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=source]) > ObjectLiteralExpression'; const DEFINE_OBJECT_SELECTOR = 'CallExpression:has(Identifier[name=defineConfig]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=source]) ObjectLiteralExpression PropertyAssignment:has(Identifier[name=define]) > ObjectLiteralExpression'; let configContents = tree.read(configFilePath, 'utf-8'); const ast = tsquery_1.tsquery.ast(configContents); const defineProperty = `'${key}': '${value}'`; const sourceConfigNodes = (0, tsquery_1.tsquery)(ast, SOURCE_CONFIG_SELECTOR); if (sourceConfigNodes.length === 0) { const defineConfigNodes = (0, tsquery_1.tsquery)(ast, DEFINE_CONFIG_SELECTOR); if (defineConfigNodes.length === 0) { throw new Error(`Could not find 'defineConfig' in the config file at ${configFilePath}.`); } const defineConfigNode = defineConfigNodes[0]; configContents = `${configContents.slice(0, defineConfigNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(1)(`source: {\n${(0, indent_by_1.indentBy)(1)(`define: {\n${(0, indent_by_1.indentBy)(1)(defineProperty)},\n}`)},\n}`)},${configContents.slice(defineConfigNode.getStart() + 1)}`; } else { const sourceConfigNode = sourceConfigNodes[0]; const defineObjectNodes = (0, tsquery_1.tsquery)(ast, DEFINE_OBJECT_SELECTOR); if (defineObjectNodes.length === 0) { configContents = `${configContents.slice(0, sourceConfigNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(2)(`define: {\n${(0, indent_by_1.indentBy)(1)(defineProperty)},\n},`)}\n\t\t${configContents.slice(sourceConfigNode.getStart() + 1)}`; } else { const defineObjectNode = defineObjectNodes[0]; configContents = `${configContents.slice(0, defineObjectNode.getStart() + 1)}\n${(0, indent_by_1.indentBy)(3)(defineProperty)},\n\t\t\t${configContents.slice(defineObjectNode.getStart() + 1)}`; } } tree.write(configFilePath, configContents); }