UNPKG

@nx/vite

Version:

The Nx Plugin for building and testing applications using Vite

120 lines (119 loc) 5.65 kB
"use strict"; Object.defineProperty(exports, "updateBuildOutDirAndRoot", { enumerable: true, get: function() { return updateBuildOutDirAndRoot; } }); const _devkit = require("@nx/devkit"); const _tsquery = require("@phenomnomnominal/tsquery"); const _updateviteconfig = require("../update-vite-config"); function updateBuildOutDirAndRoot(options, configContents, projectConfig, targetName, tree, projectName, configPath) { var _tsquery_query; const foundDefineConfig = (_tsquery_query = _tsquery.tsquery.query(configContents, 'CallExpression:has(Identifier[name="defineConfig"])')) == null ? void 0 : _tsquery_query[0]; if (!foundDefineConfig) { (0, _updateviteconfig.notFoundWarning)(configPath); return; } configContents = fixBuild(options, configContents, projectConfig, targetName, tree, projectName, configPath); configContents = addRoot(configContents, configPath); return configContents; } function fixBuild(options, configContents, projectConfig, targetName, tree, projectName, configPath) { var _tsquery_query; const configNode = (0, _updateviteconfig.getConfigNode)(configContents); if (!configNode) { (0, _updateviteconfig.notFoundWarning)(configPath); return configContents; } let outputPath = ''; // In vite.config.ts, we want to keep the path relative to workspace root if (options.outputPath) { outputPath = (0, _devkit.joinPathFragments)((0, _devkit.offsetFromRoot)(projectConfig.root), options.outputPath); } else { outputPath = (0, _devkit.joinPathFragments)((0, _devkit.offsetFromRoot)(projectConfig.root), 'dist', projectConfig.root); } // In project.json, we want to keep the path starting from workspace root projectConfig.targets[targetName].options.outputPath = options.outputPath ? options.outputPath : (0, _devkit.joinPathFragments)('dist', projectConfig.root); (0, _devkit.updateProjectConfiguration)(tree, projectName, projectConfig); const buildObject = (_tsquery_query = _tsquery.tsquery.query(configNode, `PropertyAssignment:has(Identifier[name="build"])`)) == null ? void 0 : _tsquery_query[0]; if (buildObject) { var _tsquery_query1, _tsquery_query2, _tsquery_query3; const reportCompressedSizeExists = ((_tsquery_query1 = _tsquery.tsquery.query(buildObject, `PropertyAssignment:has(Identifier[name="reportCompressedSize"])`)) == null ? void 0 : _tsquery_query1.length) > 0; const commonjsOptionsExists = ((_tsquery_query2 = _tsquery.tsquery.query(buildObject, `PropertyAssignment:has(Identifier[name="commonjsOptions"])`)) == null ? void 0 : _tsquery_query2.length) > 0; const buildOutDir = (_tsquery_query3 = _tsquery.tsquery.query(buildObject, `PropertyAssignment:has(Identifier[name="outDir"])`)) == null ? void 0 : _tsquery_query3.length; // Array to store changes let changes = []; // Add outDir if not present if (!buildOutDir) { changes.push({ type: _devkit.ChangeType.Insert, index: buildObject.getStart() + `build: {`.length + 1, text: `outDir: '${outputPath}',` }); } // Add reportCompressedSize if not present if (!reportCompressedSizeExists) { changes.push({ type: _devkit.ChangeType.Insert, index: buildObject.getStart() + `build: {`.length + 1, text: `reportCompressedSize: true,` }); } // Add commonjsOptions if not present if (!commonjsOptionsExists) { changes.push({ type: _devkit.ChangeType.Insert, index: buildObject.getStart() + `build: {`.length + 1, text: `commonjsOptions: { transformMixedEsModules: true },` }); } if (changes.length > 0) { return (0, _devkit.applyChangesToString)(configContents, changes); } } else { return addBuildProperty(configContents, outputPath, configPath); } return configContents; } function addRoot(configFileContents, configPath) { var _tsquery_query; const configNode = (0, _updateviteconfig.getConfigNode)(configFileContents); if (!configNode) { (0, _updateviteconfig.notFoundWarning)(configPath); return configFileContents; } const rootOption = (_tsquery_query = _tsquery.tsquery.query(configNode, `PropertyAssignment:has(Identifier[name="root"]) Identifier[name="__dirname"]`)) == null ? void 0 : _tsquery_query[0]; if (rootOption) { return configFileContents; } else { return (0, _devkit.applyChangesToString)(configFileContents, [ { type: _devkit.ChangeType.Insert, index: configNode.getStart() + 1, text: `root: __dirname,` } ]); } } function addBuildProperty(configFileContents, outputPath, configPath) { const configNode = (0, _updateviteconfig.getConfigNode)(configFileContents); if (!configNode) { (0, _updateviteconfig.notFoundWarning)(configPath); return configFileContents; } return (0, _devkit.applyChangesToString)(configFileContents, [ { type: _devkit.ChangeType.Insert, index: configNode.getStart() + 1, text: `build: { outDir: '${outputPath}', reportCompressedSize: true, commonjsOptions: { transformMixedEsModules: true, }, },` } ]); } //# sourceMappingURL=edit-build-config.js.map