UNPKG

@nx/vite

Version:

The Nx Plugin for building and testing applications using Vite

138 lines (137 loc) 5.67 kB
"use strict"; function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { default: function() { return _default; }, viteDevServerExecutor: function() { return viteDevServerExecutor; } }); const _extends = require("@swc/helpers/_/_extends"); const _devkit = require("@nx/devkit"); const _optionsutils = require("../../utils/options-utils"); const _executorutils = require("../../utils/executor-utils"); const _path = require("path"); const _buildimpl = require("../build/build.impl"); async function* viteDevServerExecutor(options, context) { process.env.VITE_CJS_IGNORE_WARNING = 'true'; // Allows ESM to be required in CJS modules. Vite will be published as ESM in the future. const { mergeConfig, createServer, resolveConfig } = await (0, _executorutils.loadViteDynamicImport)(); const projectRoot = context.projectsConfigurations.projects[context.projectName].root; const root = projectRoot === '.' ? process.cwd() : (0, _path.relative)(context.cwd, (0, _devkit.joinPathFragments)(context.root, projectRoot)); (0, _executorutils.createBuildableTsConfig)(projectRoot, options, context); // Retrieve the option for the configured buildTarget. const buildTargetOptions = (0, _optionsutils.getNxTargetOptions)(options.buildTarget, context); const { configuration } = (0, _devkit.parseTargetString)(options.buildTarget, context); const { buildOptions, otherOptions: otherOptionsFromBuild } = await (0, _buildimpl.getBuildExtraArgs)(buildTargetOptions); const viteConfigPath = (0, _optionsutils.normalizeViteConfigFilePath)(context.root, projectRoot, buildTargetOptions.configFile); const { serverOptions, otherOptions } = await getServerExtraArgs(options, configuration, buildOptions, otherOptionsFromBuild); var _otherOptions_mode, _ref; const defaultMode = (_ref = (_otherOptions_mode = otherOptions == null ? void 0 : otherOptions.mode) != null ? _otherOptions_mode : buildTargetOptions == null ? void 0 : buildTargetOptions['mode']) != null ? _ref : 'development'; var _process_env_NODE_ENV; const resolved = await resolveConfig({ configFile: viteConfigPath, mode: defaultMode }, 'serve', defaultMode, (_process_env_NODE_ENV = process.env.NODE_ENV) != null ? _process_env_NODE_ENV : defaultMode); var _resolved_root; // vite InlineConfig const serverConfig = mergeConfig({ // 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_root = resolved.root) != null ? _resolved_root : root, configFile: viteConfigPath }, _extends._({ server: _extends._({}, await (0, _optionsutils.getViteServerOptions)(options, context), serverOptions) }, otherOptions)); try { const server = await createServer(serverConfig); await runViteDevServer(server); const resolvedUrls = [ ...server.resolvedUrls.local, ...server.resolvedUrls.network ]; var _resolvedUrls_; yield { success: true, baseUrl: (_resolvedUrls_ = resolvedUrls[0]) != null ? _resolvedUrls_ : '' }; } catch (e) { console.error(e); yield { success: false, baseUrl: '' }; } await new Promise((resolve)=>{ process.once('SIGINT', ()=>resolve()); process.once('SIGTERM', ()=>resolve()); process.once('exit', ()=>resolve()); }); } // vite ViteDevServer async function runViteDevServer(server) { await server.listen(); server.printUrls(); const processOnExit = async ()=>{ await server.close(); }; process.once('SIGINT', processOnExit); process.once('SIGTERM', processOnExit); process.once('exit', processOnExit); } const _default = viteDevServerExecutor; async function getServerExtraArgs(options, configuration, buildOptionsFromBuildTarget, otherOptionsFromBuildTarget) { // support passing extra args to vite cli const schema = await Promise.resolve().then(()=>require("./schema.json")); const extraArgs = {}; for (const key of Object.keys(options)){ if (!schema.properties[key]) { extraArgs[key] = options[key]; } } let serverOptions = {}; const serverSchemaKeys = [ 'hmr', 'warmup', 'watch', 'middlewareMode', 'fs', 'origin', 'preTransformRequests', 'sourcemapIgnoreList', 'port', 'strictPort', 'host', 'https', 'open', 'proxy', 'cors', 'headers' ]; let otherOptions = {}; for (const key of Object.keys(extraArgs)){ if (serverSchemaKeys.includes(key)) { serverOptions[key] = extraArgs[key]; } else { otherOptions[key] = extraArgs[key]; } } if (configuration) { var _buildOptionsFromBuildTarget_watch; serverOptions = _extends._({}, serverOptions, { watch: (_buildOptionsFromBuildTarget_watch = buildOptionsFromBuildTarget == null ? void 0 : buildOptionsFromBuildTarget.watch) != null ? _buildOptionsFromBuildTarget_watch : serverOptions == null ? void 0 : serverOptions.watch }); otherOptions = _extends._({}, otherOptions, otherOptionsFromBuildTarget != null ? otherOptionsFromBuildTarget : {}); } return { serverOptions, otherOptions }; } //# sourceMappingURL=dev-server.impl.js.map