Skip to content

Commit

Permalink
reduce deps in macros and util packages
Browse files Browse the repository at this point in the history
Before this change, anyone using `@embroider/util` or `@embroider/macros` would get a full copy of `@embroider/core`. And macros also pulled in its own complete `@babel/core`.

This refactors the few bits of shared code into a separate `@embroider/shared-internals` package, which should hopefully reduce the dependency weight for users of `@embroider/macros` and `@embroider/util`.
  • Loading branch information
ef4 committed Mar 25, 2021
1 parent 5eb196d commit be045b1
Show file tree
Hide file tree
Showing 42 changed files with 179 additions and 117 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/packages/core/**/*.js
/packages/core/**/*.d.ts
/packages/shared-internals/**/*.js
/packages/shared-internals/**/*.d.ts
/packages/compat/**/*.js
/packages/compat/**/*.d.ts
/packages/macros/**/*.js
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/src/compat-adapters/ember-engines.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import V1Addon from '../v1-addon';
import Filter from 'broccoli-persistent-filter';
import { AddonMeta } from '@embroider/core/src/metadata';
import { AddonMeta } from '@embroider/core';

class Awk extends Filter {
search: string;
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@babel/traverse": "^7.12.1",
"@babel/types": "^7.12.1",
"@embroider/macros": "0.37.0",
"@embroider/shared-internals": "0.37.0",
"assert-never": "^1.1.0",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"broccoli-node-api": "^1.7.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/app-differ.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { V2AddonPackage } from './package';
import { AddonPackage } from '@embroider/shared-internals';
import MultiTreeDiff, { InputTree } from './multi-tree-diff';
import walkSync from 'walk-sync';
import { join, basename, dirname } from 'path';
Expand Down Expand Up @@ -28,7 +28,7 @@ export default class AppDiffer {
constructor(
private outputPath: string,
private ownAppJSDir: string,
activeAddonDescendants: V2AddonPackage[],
activeAddonDescendants: AddonPackage[],
// arguments below this point are only needed in fastboot mode. Fastboot
// makes this pretty messy because fastboot trees all merge into the app 🤮.
fastbootEnabled = false,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/app-files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sep } from 'path';
import Package, { V2AddonPackage } from './package';
import { Package, AddonPackage } from '@embroider/shared-internals';
import AppDiffer from './app-differ';

export interface RouteFiles {
Expand Down Expand Up @@ -147,7 +147,7 @@ export interface EngineSummary {
// the engine's own package
package: Package;
// the set of active addons in the engine
addons: Set<V2AddonPackage>;
addons: Set<AddonPackage>;
// the parent engine, if any
parent: EngineSummary | undefined;
// where the engine's own V2 code comes from
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { AddonMeta, AppMeta } from './metadata';
import {
AddonMeta,
AppMeta,
Package,
AddonPackage,
explicitRelative,
extensionsPattern,
} from '@embroider/shared-internals';
import { OutputPaths } from './wait-for-trees';
import { compile } from './js-handlebars';
import Package, { V2AddonPackage } from './package';
import resolve from 'resolve';
import { Memoize } from 'typescript-memoize';
import { copySync, ensureDirSync, readJSONSync, statSync, unlinkSync, writeFileSync } from 'fs-extra';
Expand All @@ -22,7 +28,6 @@ import { TemplateCompilerPlugins } from '.';
import { templateCompilerModule, TemplateCompilerParams } from './template-compiler';
import { Resolver } from './resolver';
import { Options as AdjustImportsOptions } from './babel-plugin-adjust-imports';
import { explicitRelative, extensionsPattern } from './paths';
import { mangledEngineRoot } from './engine-mangler';
import { AppFiles, Engine, EngineSummary, RouteFiles } from './app-files';
import partition from 'lodash/partition';
Expand All @@ -46,10 +51,10 @@ export type EmberENV = unknown;
*/
export interface AppAdapter<TreeNames> {
// the set of all addon packages that are active (recursive)
readonly allActiveAddons: V2AddonPackage[];
readonly allActiveAddons: AddonPackage[];

// the direct active addon dependencies of a given package
activeAddonChildren(pkg: Package): V2AddonPackage[];
activeAddonChildren(pkg: Package): AddonPackage[];

// path to the directory where the app's own Javascript lives. Doesn't include
// any files copied out of addons, we take care of that generically in
Expand Down Expand Up @@ -1389,7 +1394,7 @@ module.exports = babelFilter({{{json-stringify skipBabel}}});
// meta['renamed-modules'] has mapping from classic filename to real filename.
// This takes that and converts it to the inverst mapping from real import path
// to classic import path.
function inverseRenamedModules(meta: V2AddonPackage['meta'], extensions: RegExp) {
function inverseRenamedModules(meta: AddonPackage['meta'], extensions: RegExp) {
let renamed = meta['renamed-modules'];
if (renamed) {
let inverted = {} as { [name: string]: string };
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/babel-filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PackageCache from './package-cache';
import { PackageCache } from '@embroider/shared-internals';
import Options from './options';
import semver from 'semver';

Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/babel-plugin-adjust-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ import {
ExportAllDeclaration,
importNamespaceSpecifier,
} from '@babel/types';
import PackageCache from './package-cache';
import Package, { V2Package } from './package';
import { PackageCache, Package, V2Package, explicitRelative } from '@embroider/shared-internals';
import { outputFileSync } from 'fs-extra';
import { Memoize } from 'typescript-memoize';
import { compile } from './js-handlebars';
import { explicitRelative } from './paths';

interface State {
emberCLIVanillaJobs: Function[];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/build-stage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import WaitForTrees, { OutputPaths } from './wait-for-trees';
import PackageCache from './package-cache';
import { PackageCache } from '@embroider/shared-internals';
import Stage from './stage';
import { Node } from 'broccoli-node-api';
import { Memoize } from 'typescript-memoize';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/engine-mangler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Package from './package';
import { Package } from '@embroider/shared-internals';

export function mangledEngineRoot(pkg: Package) {
return `${pkg.root}__engine_internal__`;
Expand Down
18 changes: 13 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export {
applyVariantToTemplateCompiler,
} from './packager';
export { Resolver } from './resolver';
export { AppMeta, AddonMeta } from './metadata';
export { default as Package, V2AddonPackage as AddonPackage, V2AppPackage as AppPackage, V2Package } from './package';
export { default as Stage } from './stage';
export {
TemplateCompiler,
Expand All @@ -18,14 +16,24 @@ export {
export { Asset, EmberAsset, ImplicitAssetPaths } from './asset';
export { default as Options, optionsWithDefaults } from './options';
export { default as toBroccoliPlugin } from './to-broccoli-plugin';
export { default as PackageCache } from './package-cache';
export { default as packageName } from './package-name';
export { default as WaitForTrees, OutputPaths } from './wait-for-trees';
export { default as BuildStage } from './build-stage';
export { getOrCreate } from './get-or-create';
export { compile as jsHandlebarsCompile } from './js-handlebars';
export { AppAdapter, AppBuilder, EmberENV } from './app';
export { todo, unsupported, warn, debug, expectWarning, throwOnWarnings } from './messages';
export { explicitRelative, extensionsPattern } from './paths';
export { default as babelFilter } from './babel-filter';
export { mangledEngineRoot } from './engine-mangler';

export {
AppMeta,
AddonMeta,
explicitRelative,
extensionsPattern,
getOrCreate,
Package,
AddonPackage,
AppPackage,
V2Package,
PackageCache,
} from '@embroider/shared-internals';
4 changes: 2 additions & 2 deletions packages/core/src/stage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Node } from 'broccoli-node-api';
import PackageCache from './package-cache';
import type { Node } from 'broccoli-node-api';
import { PackageCache } from '@embroider/shared-internals';

// A build Stage is _kinda_ like a Broccoli transform, and it interoperates with
// Broccoli, but it takes a different approach to how stages combine.
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/template-colocation-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
importSpecifier,
} from '@babel/types';
import { dirname } from 'path';
import { explicitRelative } from './paths';
import PackageCache from './package-cache';
import { explicitRelative } from '@embroider/shared-internals';
import { PackageCache } from '@embroider/shared-internals';

const packageCache = PackageCache.shared('embroider-stage3');

Expand Down
9 changes: 5 additions & 4 deletions packages/macros/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
"test": "jest"
},
"dependencies": {
"@babel/core": "^7.12.3",
"@babel/traverse": "^7.12.1",
"@babel/types": "^7.12.1",
"@embroider/core": "0.37.0",
"@embroider/shared-internals": "0.37.0",
"assert-never": "^1.1.0",
"ember-cli-babel": "^7.23.0",
"lodash": "^4.17.10",
"resolve": "^1.8.1",
"semver": "^7.3.2"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/traverse": "^7.12.1",
"@babel/types": "^7.12.1",
"@embroider/core": "0.37.0",
"@embroider/test-support": "0.36.0",
"@types/babel__core": "^7.1.10",
"@types/babel__generator": "^7.6.2",
Expand Down
7 changes: 7 additions & 0 deletions packages/macros/src/babel/babel-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This type probably exists somewhere in our dependencies but I couldn't find
// it. It's the object babel passes to your plugin function.
import type { types, template } from '@babel/core';
export interface BabelContext {
template: typeof template;
types: typeof types;
}
6 changes: 3 additions & 3 deletions packages/macros/src/babel/dependency-satisfies.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NodePath } from '@babel/traverse';
import { CallExpression } from '@babel/types';
import type { NodePath } from '@babel/traverse';
import type { CallExpression } from '@babel/types';
import State, { sourceFile } from './state';
import { satisfies } from 'semver';
import { PackageCache } from '@embroider/core';
import { PackageCache } from '@embroider/shared-internals';
import error from './error';
import { assertArray } from './evaluate-json';

Expand Down
22 changes: 6 additions & 16 deletions packages/macros/src/babel/each.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NodePath } from '@babel/traverse';
import { Evaluator } from './evaluate-json';
import { parse } from '@babel/core';
import { CallExpression, ForOfStatement, identifier, File, ExpressionStatement, Identifier } from '@babel/types';
import type { NodePath } from '@babel/traverse';
import { buildLiterals, Evaluator } from './evaluate-json';
import type { CallExpression, ForOfStatement, Identifier } from '@babel/types';
import error from './error';
import State, { cloneDeep } from './state';
import { BabelContext } from './babel-context';

type CallEachExpression = NodePath<CallExpression> & {
get(callee: 'callee'): NodePath<Identifier>;
Expand All @@ -24,7 +24,7 @@ export function isEachPath(path: NodePath<ForOfStatement>): path is EachPath {
return false;
}

export function insertEach(path: EachPath, state: State) {
export function insertEach(path: EachPath, state: State, context: BabelContext) {
let args = path.get('right').get('arguments');
if (args.length !== 1) {
throw error(path, `the each() macro accepts exactly one argument, you passed ${args.length}`);
Expand Down Expand Up @@ -54,7 +54,7 @@ export function insertEach(path: EachPath, state: State) {
state.neededRuntimeImports.set(callee.node.name, 'each');
} else {
for (let element of array.value) {
let literalElement = asLiteral(element);
let literalElement = buildLiterals(element, context);
for (let target of nameRefs) {
target.replaceWith(literalElement);
}
Expand All @@ -63,13 +63,3 @@ export function insertEach(path: EachPath, state: State) {
path.remove();
}
}

function asLiteral(value: unknown | undefined) {
if (typeof value === 'undefined') {
return identifier('undefined');
}
let ast = parse(`a(${JSON.stringify(value)})`, {}) as File;
let statement = ast.program.body[0] as ExpressionStatement;
let expression = statement.expression as CallExpression;
return expression.arguments[0];
}
2 changes: 1 addition & 1 deletion packages/macros/src/babel/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodePath } from '@babel/traverse';
import type { NodePath } from '@babel/traverse';

export default function error(path: NodePath, message: string) {
return path.buildCodeFrameError(message, MacroError);
Expand Down
15 changes: 6 additions & 9 deletions packages/macros/src/babel/evaluate-json.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { NodePath } from '@babel/traverse';
import {
import type { NodePath } from '@babel/traverse';
import type {
Identifier,
ObjectExpression,
identifier,
MemberExpression,
Expression,
File,
ExpressionStatement,
CallExpression,
OptionalMemberExpression,
} from '@babel/types';
import { parse } from '@babel/core';
import State, { owningPackage } from './state';
import dependencySatisfies from './dependency-satisfies';
import moduleExists from './module-exists';
import getConfig from './get-config';
import { BabelContext } from './babel-context';

type OpValue = string | boolean | number;

Expand Down Expand Up @@ -423,12 +421,11 @@ export function assertArray<T>(input: T | T[]): T[] {
return input;
}

export function buildLiterals(value: unknown | undefined): Identifier | ObjectExpression {
export function buildLiterals(value: unknown | undefined, babelContext: BabelContext): Identifier | ObjectExpression {
if (typeof value === 'undefined') {
return identifier('undefined');
return babelContext.types.identifier('undefined');
}
let ast = parse(`a(${JSON.stringify(value)})`, {}) as File;
let statement = ast.program.body[0] as ExpressionStatement;
let statement = babelContext.template(`a(${JSON.stringify(value)})`)() as ExpressionStatement;
let expression = statement.expression as CallExpression;
return expression.arguments[0] as ObjectExpression;
}
4 changes: 2 additions & 2 deletions packages/macros/src/babel/fail-build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NodePath } from '@babel/traverse';
import type { NodePath } from '@babel/traverse';
import { Evaluator, ConfidentResult } from './evaluate-json';
import { CallExpression } from '@babel/types';
import type { CallExpression } from '@babel/types';
import error from './error';
import { format } from 'util';
import State from './state';
Expand Down
Loading

0 comments on commit be045b1

Please sign in to comment.