UNPKG

@equinor/fusion-framework-cli

Version:

--- title: Fusion Framework CLI ---

137 lines 6.74 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { createServer, defineConfig, mergeConfig, } from 'vite'; import { assert } from 'node:console'; import { join, relative } from 'node:path'; import portFinder from 'portfinder'; import deepmerge from 'deepmerge/index.js'; import ViteRestart from 'vite-plugin-restart'; import { appProxyPlugin } from '../lib/plugins/app-proxy/app-proxy-plugin.js'; import { helpProxyPlugin } from '../lib/plugins/help-proxy/help-proxy-plugin.js'; import { appSettingsPlugin } from '../lib/plugins/app-settings/index.js'; import { externalPublicPlugin } from '../lib/plugins/external-public/external-public-plugin.js'; import { supportedExt } from '../lib/utils/config.js'; import { manifestConfigFilename } from '../lib/app-manifest.js'; import { appConfigFilename } from '../lib/app-config.js'; import { loadAppConfig } from './utils/load-app-config.js'; import { loadViteConfig } from './utils/load-vite-config.js'; import { loadPackage } from './utils/load-package.js'; import { Spinner } from './utils/spinner.js'; import { chalk, formatPath } from './utils/format.js'; import { loadAppManifest } from './utils/load-manifest.js'; import { proxyRequestLogger } from './utils/proxy-request-logger.js'; export const createDevServer = (options) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d; const { configSourceFiles, library, port, devPortalPath, noOpen } = options; const spinner = Spinner.Global({ prefixText: chalk.dim('dev-server') }); const pkg = yield loadPackage(); const env = { command: 'serve', mode: (_a = process.env.NODE_ENV) !== null && _a !== void 0 ? _a : 'development', root: pkg.root, }; const generateManifest = () => __awaiter(void 0, void 0, void 0, function* () { const { manifest } = yield loadAppManifest(env, pkg, { file: configSourceFiles.manifest, }); const assetPath = `bundles/apps/${manifest.appKey}/${pkg.packageJson.version}`; return deepmerge(manifest, { build: { assetPath, configUrl: `${assetPath}/config`, }, }); }); const generateConfig = () => __awaiter(void 0, void 0, void 0, function* () { const { config } = yield loadAppConfig(env, pkg, { file: configSourceFiles.app, }); return config; }); const { appKey } = yield generateManifest(); /** * Load application manifest * Application might have overridden the `appKey` */ spinner.info(`resolved application key ${chalk.magenta(appKey)}`); const { viteConfig: baseViteConfig, path: viteConfigPath } = yield loadViteConfig(env, { file: configSourceFiles.vite, }); /** * Defines the configuration for the development server. */ const devServerConfig = defineConfig({ publicDir: devPortalPath, appType: 'custom', server: { open: !noOpen ? `/apps/${appKey}` : false, port: port !== null && port !== void 0 ? port : (yield portFinder.getPortPromise({ port: 3000 })), }, plugins: [ // Serve the dev portal as static files externalPublicPlugin(devPortalPath), appSettingsPlugin({ match: `/apps-proxy/persons/me/apps/${appKey}/settings`, }), // Proxy requests to the app server appProxyPlugin({ proxy: { path: '/apps-proxy', target: 'https://apps.ci.api.fusion-dev.net/', onProxyReq: proxyRequestLogger, }, app: { key: appKey, version: String(pkg.packageJson.version), generateConfig, generateManifest, manifestPath: `persons/me/apps/${appKey}`, }, }), // Proxy help assets request to help service. helpProxyPlugin({ proxy: { path: '/help-proxy', target: 'https://help.ci.api.fusion-dev.net/', onProxyReq: proxyRequestLogger, }, }), // Restart the server when config changes or the dev portal source is updated ViteRestart({ restart: [ 'package.json', viteConfigPath, join(relative(process.cwd(), devPortalPath), '/**/*'), ].filter((x) => !!x), /** reload the CLI when config changes, note change to APP-KEY need restart */ reload: [ ...supportedExt.map((ext) => [appConfigFilename, ext].join('')), ...supportedExt.map((ext) => [manifestConfigFilename, ext].join('')), ], }), ], }); // Merge the base Vite config with the dev server config const viteConfig = mergeConfig(devServerConfig, baseViteConfig); /** Add library/framework plugins */ if (library === 'react') { const reactPlugin = yield import('@vitejs/plugin-react'); viteConfig.plugins.push(reactPlugin.default()); } assert((_b = viteConfig.build) === null || _b === void 0 ? void 0 : _b.lib, 'expected vite build to have library defined'); const { entry } = viteConfig.build.lib; spinner.info('💾 application entrypoint', formatPath(String(entry), { relative: true })); spinner.info('resolving cli internal assets from', formatPath(String(viteConfig.publicDir), { relative: true })); const vite = yield createServer(Object.assign(Object.assign({}, env), viteConfig)); spinner.start('🚀 start server'); yield vite.listen(); spinner.succeed('🔗', chalk.underline.green(new URL(`/apps/${appKey}`, (_d = (_c = vite.resolvedUrls) === null || _c === void 0 ? void 0 : _c.local[0]) !== null && _d !== void 0 ? _d : `https://localhost:/${vite.config.server.port}`).href)); }); //# sourceMappingURL=create-dev-serve.js.map