UNPKG

@nx/vite

Version:

The Nx Plugin for building and testing applications using Vite

91 lines (89 loc) 4.32 kB
"use strict"; function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { getOptions: function() { return getOptions; }, getOptionsAsArgv: function() { return getOptionsAsArgv; } }); const _extends = require("@swc/helpers/_/_extends"); const _object_without_properties_loose = require("@swc/helpers/_/_object_without_properties_loose"); const _devkit = require("@nx/devkit"); const _optionsutils = require("../../../utils/options-utils"); const _path = require("path"); const _executorutils = require("../../../utils/executor-utils"); async function getOptions(options, context, projectRoot) { var _resolved_config, _resolved_config1; // Allows ESM to be required in CJS modules. Vite will be published as ESM in the future. const { loadConfigFromFile, mergeConfig } = await (0, _executorutils.loadViteDynamicImport)(); const viteConfigPath = (0, _optionsutils.normalizeViteConfigFilePath)(context.root, projectRoot, options.configFile); if (!viteConfigPath) { throw new Error((0, _devkit.stripIndents)` Unable to load test config from config file ${viteConfigPath}. Please make sure that vitest is configured correctly, or use the @nx/vite:vitest generator to configure it for you. You can read more here: https://nx.dev/nx-api/vite/generators/vitest `); } var _options_mode; const resolved = await loadConfigFromFile({ mode: (_options_mode = options == null ? void 0 : options.mode) != null ? _options_mode : 'production', command: 'serve' }, viteConfigPath); if (!viteConfigPath || !(resolved == null ? void 0 : (_resolved_config = resolved.config) == null ? void 0 : _resolved_config['test'])) { var _resolved_path; _devkit.logger.warn((0, _devkit.stripIndents)`Unable to load test config from config file ${(_resolved_path = resolved == null ? void 0 : resolved.path) != null ? _resolved_path : viteConfigPath} Some settings may not be applied as expected. You can manually set the config in the project, ${context.projectName}, configuration. `); } const root = projectRoot === '.' ? process.cwd() : (0, _path.relative)(context.cwd, (0, _devkit.joinPathFragments)(context.root, projectRoot)); const { parseCLI } = await (0, _executorutils.loadVitestDynamicImport)(); const _parseCLI = parseCLI([ 'vitest', ...getOptionsAsArgv(options) ]), { options: { watch } } = _parseCLI, normalizedExtraArgs = _object_without_properties_loose._(_parseCLI.options, [ "watch" ]); const { reportsDirectory, coverage } = normalizedExtraArgs, restNormalizedArgs = _object_without_properties_loose._(normalizedExtraArgs, [ "reportsDirectory", "coverage" ]); var _resolved_config_root; const settings = _extends._({ // Explicitly set watch mode to false if not provided otherwise vitest // will enable watch mode by default for non CI environments watch: watch != null ? watch : false }, restNormalizedArgs, { // This should not be needed as it's going to be set in vite.config.ts // but leaving it here in case someone did not migrate correctly root: (_resolved_config_root = resolved.config.root) != null ? _resolved_config_root : root, config: viteConfigPath, coverage: _extends._({}, coverage != null ? coverage : {}, reportsDirectory && { reportsDirectory }) }); var _resolved_config_test; return mergeConfig((_resolved_config_test = resolved == null ? void 0 : (_resolved_config1 = resolved.config) == null ? void 0 : _resolved_config1['test']) != null ? _resolved_config_test : {}, settings); } function getOptionsAsArgv(obj) { const argv = []; for (const [key, value] of Object.entries(obj)){ if (Array.isArray(value)) { value.forEach((item)=>argv.push(`--${key}=${item}`)); } else if (typeof value === 'object' && value !== null) { argv.push(`--${key}='${JSON.stringify(value)}'`); } else { argv.push(`--${key}=${value}`); } } return argv; } //# sourceMappingURL=utils.js.map