From 0c25fa433df9fb6d02d63bdbb981fcf3081c05b5 Mon Sep 17 00:00:00 2001 From: Barak Date: Mon, 5 Feb 2024 09:29:19 +0200 Subject: [PATCH] refactor: extract create entry points from build config --- packages/engine-cli/src/create-entrypoints.ts | 113 ++++++++++++++++ ...create-environments-build-configuration.ts | 123 +++--------------- packages/engine-cli/src/engine-build.ts | 14 +- 3 files changed, 140 insertions(+), 110 deletions(-) create mode 100644 packages/engine-cli/src/create-entrypoints.ts diff --git a/packages/engine-cli/src/create-entrypoints.ts b/packages/engine-cli/src/create-entrypoints.ts new file mode 100644 index 000000000..b80f724f6 --- /dev/null +++ b/packages/engine-cli/src/create-entrypoints.ts @@ -0,0 +1,113 @@ +import { TopLevelConfig } from '@wixc3/engine-core'; +import { + ConfigurationEnvironmentMapping, + FeatureEnvironmentMapping, + IConfigDefinition, +} from '@wixc3/engine-runtime-node'; +import { + IFeatureDefinition, + createMainEntrypoint, + createNodeEntrypoint, + createNodeEnvironmentManagerEntrypoint, + getResolvedEnvironments, +} from '@wixc3/engine-scripts'; +import { SetMultiMap } from '@wixc3/patterns'; + +export interface CreateEntryPointOptions { + dev: boolean; + featureEnvironmentsMapping: FeatureEnvironmentMapping; + configMapping: ConfigurationEnvironmentMapping; + configurations: SetMultiMap; + features: Map; + publicPath: string; + environments: ReturnType; + config: TopLevelConfig; + publicConfigsRoute: string; + featureName?: string; + configName?: string; +} + +export type EntryPoints = { + webEntryPoints: Map; + nodeEntryPoints: Map; +}; + +export function createEntryPoints( + options: CreateEntryPointOptions, + jsOutExtension: '.js' | '.mjs', + nodeFormat: 'esm' | 'cjs', +): EntryPoints { + const { + dev, + publicPath, + featureName, + configName, + environments, + features, + configurations, + config, + featureEnvironmentsMapping, + configMapping, + publicConfigsRoute, + } = options; + + const mode = dev ? 'development' : 'production'; + const browserTargets = concatIterables(environments.webEnvs.values(), environments.workerEnvs.values()); + const nodeTargets = concatIterables(environments.nodeEnvs.values(), environments.workerThreadEnvs.values()); + + const webEntryPoints = new Map(); + const nodeEntryPoints = new Map(); + + const entrypointContent = createNodeEnvironmentManagerEntrypoint({ + featureEnvironmentsMapping, + configMapping, + moduleType: nodeFormat, + }); + + nodeEntryPoints.set(`engine-environment-manager${jsOutExtension}`, entrypointContent); + + for (const { env, childEnvs } of browserTargets) { + const entrypointContent = createMainEntrypoint({ + features, + childEnvs, + env, + featureName, + configName, + publicPath, + publicPathVariableName: 'PUBLIC_PATH', + configurations, + mode, + staticBuild: false, + publicConfigsRoute, + config, + }); + + webEntryPoints.set( + `${env.name}.${env.type === 'webworker' ? 'webworker' : 'web'}${jsOutExtension}`, + entrypointContent, + ); + } + + for (const { env, childEnvs } of nodeTargets) { + const entrypointContent = createNodeEntrypoint({ + features, + childEnvs, + env, + featureName, + configName, + configurations, + mode, + staticBuild: false, + publicConfigsRoute: '/configs', + config, + }); + nodeEntryPoints.set(`${env.name}.${env.type}${jsOutExtension}`, entrypointContent); + } + return { webEntryPoints, nodeEntryPoints }; +} + +function* concatIterables(...iterables: Iterable[]) { + for (const iterable of iterables) { + yield* iterable; + } +} diff --git a/packages/engine-cli/src/create-environments-build-configuration.ts b/packages/engine-cli/src/create-environments-build-configuration.ts index 82418338d..2905e10b3 100644 --- a/packages/engine-cli/src/create-environments-build-configuration.ts +++ b/packages/engine-cli/src/create-environments-build-configuration.ts @@ -1,117 +1,40 @@ -import { TopLevelConfig } from '@wixc3/engine-core'; -import { - ConfigurationEnvironmentMapping, - FeatureEnvironmentMapping, - IConfigDefinition, -} from '@wixc3/engine-runtime-node'; -import { - IFeatureDefinition, - OverrideConfigHook, - createMainEntrypoint, - createNodeEntrypoint, - createNodeEnvironmentManagerEntrypoint, - getResolvedEnvironments, -} from '@wixc3/engine-scripts'; -import { SetMultiMap } from '@wixc3/patterns'; +import { OverrideConfigHook } from '@wixc3/engine-scripts'; import { BuildOptions, Plugin } from 'esbuild'; import { topLevelConfigPlugin } from './top-level-config-plugin-esbuild'; import { join } from 'node:path'; import { htmlPlugin } from './esbuild-html-plugin'; import { dynamicEntryPlugin } from './esbuild-dynamic-entry-plugin'; +import { CreateEntryPointOptions, createEntryPoints, EntryPoints } from './create-entrypoints'; -export interface CreateEnvBuildConfigOptions { +export type CreateEnvBuildConfigOptions = CreateBuildConfigOptions & CreateEntryPointOptions; + +export function createEnvironmentsBuildConfiguration(options: CreateEnvBuildConfigOptions & CreateEntryPointOptions) { + const jsOutExtension = '.js' as '.js' | '.mjs'; + const nodeFormat = jsOutExtension === '.mjs' ? 'esm' : 'cjs'; + const entryPoints = createEntryPoints(options, jsOutExtension, nodeFormat); + const buildConfig = createBuildConfiguration(options, entryPoints, jsOutExtension, nodeFormat); + return buildConfig; +} + +export interface CreateBuildConfigOptions { dev: boolean; buildPlugins: Plugin[] | OverrideConfigHook; - featureEnvironmentsMapping: FeatureEnvironmentMapping; - configMapping: ConfigurationEnvironmentMapping; - configurations: SetMultiMap; - features: Map; publicPath: string; - environments: ReturnType; - config: TopLevelConfig; outputPath: string; - publicConfigsRoute: string; - featureName?: string; - configName?: string; buildConditions?: string[]; extensions?: string[]; } -export function createEnvironmentsBuildConfiguration(options: CreateEnvBuildConfigOptions) { - const { - dev, - featureName, - configName, - outputPath, - environments, - publicPath, - features, - configurations, - config, - buildPlugins, - buildConditions, - extensions, - featureEnvironmentsMapping, - configMapping, - publicConfigsRoute, - } = options; +export function createBuildConfiguration( + options: CreateBuildConfigOptions, + { nodeEntryPoints, webEntryPoints }: EntryPoints, + jsOutExtension: '.js' | '.mjs', + nodeFormat: 'esm' | 'cjs', +) { + const { dev, outputPath, publicPath, buildPlugins, buildConditions, extensions } = options; - const mode = dev ? 'development' : 'production'; - const jsOutExtension = '.js' as '.js' | '.mjs'; - const nodeFormat = jsOutExtension === '.mjs' ? 'esm' : 'cjs'; - const browserTargets = concatIterables(environments.webEnvs.values(), environments.workerEnvs.values()); - const nodeTargets = concatIterables(environments.nodeEnvs.values(), environments.workerThreadEnvs.values()); - - const webEntryPoints = new Map(); - const nodeEntryPoints = new Map(); const commonPlugins = Array.isArray(buildPlugins) ? buildPlugins : []; - const entrypointContent = createNodeEnvironmentManagerEntrypoint({ - featureEnvironmentsMapping, - configMapping, - moduleType: nodeFormat, - }); - - nodeEntryPoints.set(`engine-environment-manager${jsOutExtension}`, entrypointContent); - - for (const { env, childEnvs } of browserTargets) { - const entrypointContent = createMainEntrypoint({ - features, - childEnvs, - env, - featureName, - configName, - publicPath, - publicPathVariableName: 'PUBLIC_PATH', - configurations, - mode, - staticBuild: false, - publicConfigsRoute, - config, - }); - - webEntryPoints.set( - `${env.name}.${env.type === 'webworker' ? 'webworker' : 'web'}${jsOutExtension}`, - entrypointContent, - ); - } - - for (const { env, childEnvs } of nodeTargets) { - const entrypointContent = createNodeEntrypoint({ - features, - childEnvs, - env, - featureName, - configName, - configurations, - mode, - staticBuild: false, - publicConfigsRoute: '/configs', - config, - }); - nodeEntryPoints.set(`${env.name}.${env.type}${jsOutExtension}`, entrypointContent); - } - const commonConfig = { target: 'es2020', bundle: true, @@ -178,9 +101,3 @@ export function createEnvironmentsBuildConfiguration(options: CreateEnvBuildConf nodeConfig, }; } - -function* concatIterables(...iterables: Iterable[]) { - for (const iterable of iterables) { - yield* iterable; - } -} diff --git a/packages/engine-cli/src/engine-build.ts b/packages/engine-cli/src/engine-build.ts index 329fae457..817289892 100644 --- a/packages/engine-cli/src/engine-build.ts +++ b/packages/engine-cli/src/engine-build.ts @@ -71,6 +71,13 @@ export async function runEngine({ await importModules(rootDir, requiredPaths); + if (clean) { + if (verbose) { + console.log(`Cleaning ${outputPath}`); + } + await fs.promises.rm(outputPath, { recursive: true, force: true }); + } + const { features, configurations } = await analyzeFeatures( fs, rootDir, @@ -80,13 +87,6 @@ export async function runEngine({ buildConditions, ); - if (clean) { - if (verbose) { - console.log(`Cleaning ${outputPath}`); - } - await fs.promises.rm(outputPath, { recursive: true, force: true }); - } - const buildConfigurationsOptions = { features, configurations,