Skip to content

Commit

Permalink
make an explicit options type and use it everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Dec 23, 2024
1 parent 230e133 commit 4dc7a3a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/compat/src/compat-app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import escapeRegExp from 'escape-string-regexp';

import type CompatApp from './compat-app';
import { SyncDir } from './sync-dir';
import type { CompatOptionsType } from './options';

// This exists during the actual broccoli build step. As opposed to CompatApp,
// which also exists during pipeline-construction time.
Expand All @@ -75,7 +76,7 @@ export class CompatAppBuilder {
private root: string,
private origAppPackage: Package,
private appPackageWithMovedDeps: Package,
private options: Required<Options>,
private options: CompatOptionsType,
private compatApp: CompatApp,
private configTree: V1Config,
private synthVendor: Package,
Expand Down
3 changes: 2 additions & 1 deletion packages/compat/src/compat-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Node as BroccoliNode } from 'broccoli-node-api';
import type { Stage, Package } from '@embroider/core';
import { PackageCache, WaitForTrees, RewrittenPackageCache, locateEmbroiderWorkingDir } from '@embroider/core';
import type Options from './options';
import type { CompatOptionsType } from './options';
import { optionsWithDefaults } from './options';
import { Memoize } from 'typescript-memoize';
import { sync as pkgUpSync } from 'pkg-up';
Expand Down Expand Up @@ -41,7 +42,7 @@ interface Group {
export default class CompatApp {
private annotation = '@embroider/compat/app';
private active: CompatAppBuilder | undefined;
readonly options: Required<Options>;
readonly options: CompatOptionsType;

private _publicAssets: { [filePath: string]: string } = Object.create(null);
private _implicitScripts: string[] = [];
Expand Down
25 changes: 6 additions & 19 deletions packages/compat/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,6 @@ export default interface Options extends CoreOptions {
// it on in production. But it can be helpful when testing how much of your
// app is able to work with staticComponents enabled.
allowUnsafeDynamicComponents?: boolean;

/**
* When true, we statically resolve all components, modifiers, and helpers (collectively
* knows as Invokables) at build time. This causes any unused Invokables to be left out
* of the build if they are unused i.e. "tree shaking".
*
* Defaults to false which gives you greater compatibility with classic Ember apps at the
* cost of bigger builds.
*
* This setting takes over from `staticHelpers`, `staticModifiers`, and `staticComponents`
* because the Developer Experience was less than ideal if any of these settings did not
* agree i.e. they all needed to be true or they all needed to be false.
*
* Enabling this is a prerequisite for route splitting.
*/
staticInvokables?: boolean;
}

const defaults = Object.assign(coreWithDefaults(), {
Expand All @@ -122,10 +106,14 @@ const defaults = Object.assign(coreWithDefaults(), {
workspaceDir: null,
packageRules: [],
allowUnsafeDynamicComponents: false,
staticInvokables: false,
});

export function optionsWithDefaults(options?: Options): Required<Options> {
export type CompatOptionsType = Required<
Omit<Options, 'staticHelpers' | 'staticModifiers' | 'staticComponents' | 'staticInvokables'>
> &
Pick<Options, 'staticHelpers' | 'staticModifiers' | 'staticComponents' | 'staticInvokables'>;

export function optionsWithDefaults(options?: Options): CompatOptionsType {
return Object.assign({}, defaults, options);
}

Expand All @@ -138,7 +126,6 @@ export const recommendedOptions: { [name: string]: Options } = Object.freeze({
optimized: Object.freeze({
staticAddonTrees: true,
staticAddonTestSupportTrees: true,
staticInvokables: true,
staticEmberSource: true,
allowUnsafeDynamicComponents: false,
}),
Expand Down
6 changes: 3 additions & 3 deletions packages/compat/src/v1-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import rewriteAddonTree from './rewrite-addon-tree';
import { mergeWithAppend } from './merges';
import type { AddonMeta, PackageCache, AddonInstance, AddonTreePath } from '@embroider/core';
import { debug, findTopmostAddon } from '@embroider/core';
import type Options from './options';
import walkSync from 'walk-sync';
import ObserveTree from './observe-tree';
import { isEmbroiderMacrosPlugin } from '@embroider/macros/src/node';
Expand All @@ -34,6 +33,7 @@ import loadAstPlugins from './prepare-htmlbars-ast-plugins';
import getRealAddon from './get-real-addon';
import type { Options as EtcOptions } from 'babel-plugin-ember-template-compilation';
import type CompatApp from './compat-app';
import type { CompatOptionsType } from './options';

const stockTreeNames: AddonTreePath[] = Object.freeze([
'addon',
Expand Down Expand Up @@ -82,7 +82,7 @@ const fastbootPublicationDir = '_fastboot_';
export default class V1Addon {
constructor(
protected addonInstance: AddonInstance,
protected addonOptions: Required<Options>,
protected addonOptions: CompatOptionsType,
protected app: CompatApp,
private packageCache: PackageCache,
private orderIdx: number
Expand Down Expand Up @@ -1094,7 +1094,7 @@ export default class V1Addon {
export interface V1AddonConstructor {
new (
addonInstance: any,
options: Required<Options>,
options: CompatOptionsType,
app: CompatApp,
packageCache: PackageCache,
orderIdx: number
Expand Down
26 changes: 22 additions & 4 deletions packages/core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ export default interface Options {
*/
staticComponents?: boolean;

/**
* When true, we statically resolve all components, modifiers, and helpers (collectively
* knows as Invokables) at build time. This causes any unused Invokables to be left out
* of the build if they are unused i.e. "tree shaking".
*
* Defaults to false which gives you greater compatibility with classic Ember apps at the
* cost of bigger builds.
*
* This setting takes over from `staticHelpers`, `staticModifiers`, and `staticComponents`
* because the Developer Experience was less than ideal if any of these settings did not
* agree i.e. they all needed to be true or they all needed to be false.
*
* Enabling this is a prerequisite for route splitting.
*/
staticInvokables?: boolean;

// Enables per-route code splitting. Any route names that match these patterns
// will be split out of the initial app payload. If you use this, you must
// also add @embroider/router to your app. See [@embroider/router's
Expand Down Expand Up @@ -136,11 +152,13 @@ export default interface Options {
};
}

export function optionsWithDefaults(options?: Options): Required<Options> {
export type CoreOptionsType = Required<
Omit<Options, 'staticHelpers' | 'staticModifiers' | 'staticComponents' | 'staticInvokables'>
> &
Pick<Options, 'staticHelpers' | 'staticModifiers' | 'staticComponents' | 'staticInvokables'>;

export function optionsWithDefaults(options?: Options): CoreOptionsType {
let defaults = {
staticHelpers: false,
staticModifiers: false,
staticComponents: false,
splitAtRoutes: [],
staticAppPaths: [],
skipBabel: [],
Expand Down

0 comments on commit 4dc7a3a

Please sign in to comment.