Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract create entry points from build config #2309

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions packages/engine-cli/src/create-entrypoints.ts
Original file line number Diff line number Diff line change
@@ -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<string, IConfigDefinition>;
features: Map<string, IFeatureDefinition>;
publicPath: string;
environments: ReturnType<typeof getResolvedEnvironments>;
config: TopLevelConfig;
publicConfigsRoute: string;
featureName?: string;
configName?: string;
}

export type EntryPoints = {
webEntryPoints: Map<string, string>;
nodeEntryPoints: Map<string, string>;
};

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<string, string>();
const nodeEntryPoints = new Map<string, string>();

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<T>(...iterables: Iterable<T>[]) {
for (const iterable of iterables) {
yield* iterable;
}
}
123 changes: 20 additions & 103 deletions packages/engine-cli/src/create-environments-build-configuration.ts
Original file line number Diff line number Diff line change
@@ -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<string, IConfigDefinition>;
features: Map<string, IFeatureDefinition>;
publicPath: string;
environments: ReturnType<typeof getResolvedEnvironments>;
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<string, string>();
const nodeEntryPoints = new Map<string, string>();
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,
Expand Down Expand Up @@ -178,9 +101,3 @@ export function createEnvironmentsBuildConfiguration(options: CreateEnvBuildConf
nodeConfig,
};
}

function* concatIterables<T>(...iterables: Iterable<T>[]) {
for (const iterable of iterables) {
yield* iterable;
}
}
14 changes: 7 additions & 7 deletions packages/engine-cli/src/engine-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down