UNPKG

@nx/vite

Version:

The Nx Plugin for building and testing applications using Vite

69 lines (66 loc) 2.77 kB
"use strict"; function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { addConfigValuesToViteConfig: function() { return addConfigValuesToViteConfig; }, getViteConfigPath: function() { return getViteConfigPath; }, toProjectRelativePath: function() { return toProjectRelativePath; } }); const _posix = require("path/posix"); const _devkit = require("@nx/devkit"); const _tsquery = require("@phenomnomnominal/tsquery"); function toProjectRelativePath(path, projectRoot) { if (projectRoot === '.') { // workspace and project root are the same, we normalize it to ensure it // works with Jest since some paths only work when they start with `./` return path.startsWith('.') ? path : `./${path}`; } const relativePath = (0, _posix.relative)((0, _posix.resolve)(_devkit.workspaceRoot, projectRoot), (0, _posix.resolve)(_devkit.workspaceRoot, path)); return relativePath.startsWith('.') ? relativePath : `./${relativePath}`; } function getViteConfigPath(tree, root) { return [ (0, _devkit.joinPathFragments)(root, `vite.config.ts`), (0, _devkit.joinPathFragments)(root, `vite.config.cts`), (0, _devkit.joinPathFragments)(root, `vite.config.mts`), (0, _devkit.joinPathFragments)(root, `vite.config.js`), (0, _devkit.joinPathFragments)(root, `vite.config.cjs`), (0, _devkit.joinPathFragments)(root, `vite.config.mjs`) ].find((f)=>tree.exists(f)); } function addConfigValuesToViteConfig(tree, configFile, configValues) { const IMPORT_PROPERTY_SELECTOR = 'ImportDeclaration'; const viteConfigContents = tree.read(configFile, 'utf-8'); const ast = _tsquery.tsquery.ast(viteConfigContents); // AST TO GET SECTION TO APPEND TO const importNodes = (0, _tsquery.tsquery)(ast, IMPORT_PROPERTY_SELECTOR, { visitAllChildren: true }); if (importNodes.length === 0) { return; } const lastImportNode = importNodes[importNodes.length - 1]; const configValuesString = ` // These options were migrated by @nx/vite:convert-to-inferred from the project.json file. const configValues = ${JSON.stringify(configValues)}; // Determine the correct configValue to use based on the configuration const nxConfiguration = process.env.NX_TASK_TARGET_CONFIGURATION ?? 'default'; const options = { ...configValues.default, ...(configValues[nxConfiguration] ?? {}) }`; tree.write(configFile, `${viteConfigContents.slice(0, lastImportNode.getEnd())} ${configValuesString} ${viteConfigContents.slice(lastImportNode.getEnd())}`); } //# sourceMappingURL=utils.js.map