Skip to content

Commit

Permalink
finishing merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Mar 28, 2021
1 parent 1619275 commit ca63cef
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 37 deletions.
5 changes: 1 addition & 4 deletions packages/compat/src/compat-adapters/ember-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export default class extends V1Addon {
return pkg;
}
get v2Tree() {
return broccoliMergeTrees([
super.v2Tree,
new Funnel(this.rootTree, { include: ['dist/ember-template-compiler.js'] }),
]);
return mergeTrees([super.v2Tree, new Funnel(this.rootTree, { include: ['dist/ember-template-compiler.js'] })]);
}

// when using real modules, we're replacing treeForAddon and treeForVendor
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/babel-plugin-inline-hbs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as t from '@babel/types';
import type { NodePath } from '@babel/traverse';
import { join } from 'path';
import { TemplateCompiler, TemplateCompilerParams } from './template-compiler';
import { TemplateCompiler } from './template-compiler-common';
import { parse } from '@babel/core';
import { ResolvedDep } from './resolver';
import { ImportAdder } from './babel-import-adder';
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/browser-index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export type { default as Package } from './package';
export { getOrCreate } from './get-or-create';
export { explicitRelative, extensionsPattern } from './paths';
export { default as PackageCache } from './browser-package-cache';
export { TemplateCompiler, TemplateCompilerParams } from './template-compiler-common';
48 changes: 20 additions & 28 deletions packages/macros/src/babel/macros-babel-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import type { NodePath } from '@babel/traverse';
import type {
CallExpression,
Identifier,
IfStatement,
ConditionalExpression,
ForOfStatement,
FunctionDeclaration,
OptionalMemberExpression,
Program,
stringLiteral,
importNamespaceSpecifier,
} from '@babel/types';
import type * as t from '@babel/types';
import { PackageCache } from '@embroider/shared-internals';
import State, { sourceFile, pathToRuntime } from './state';
import { inlineRuntimeConfig, insertConfig, Mode as GetConfigMode } from './get-config';
Expand All @@ -25,43 +14,44 @@ import { BabelContext } from './babel-context';
const packageCache = PackageCache.shared('embroider-stage3');

export default function main(context: BabelContext): unknown {
let t = context.types;
let visitor = {
Program: {
enter(_: NodePath<Program>, state: State) {
enter(_: NodePath<t.Program>, state: State) {
state.generatedRequires = new Set();
state.jobs = [];
state.removed = new Set();
state.calledIdentifiers = new Set();
state.neededRuntimeImports = new Map();
state.neededEagerImports = new Map();
},
exit(path: NodePath<Program>, state: State) {
exit(path: NodePath<t.Program>, state: State) {
pruneMacroImports(path);
addRuntimeImports(path, state, context);
addEagerImports(path, state);
addEagerImports(path, state, t);
for (let handler of state.jobs) {
handler();
}
},
},
'IfStatement|ConditionalExpression': {
enter(path: NodePath<IfStatement | ConditionalExpression>, state: State) {
enter(path: NodePath<t.IfStatement | t.ConditionalExpression>, state: State) {
if (isMacroConditionPath(path)) {
state.calledIdentifiers.add(path.get('test').get('callee').node);
macroCondition(path, state);
}
},
},
ForOfStatement: {
enter(path: NodePath<ForOfStatement>, state: State) {
enter(path: NodePath<t.ForOfStatement>, state: State) {
if (isEachPath(path)) {
state.calledIdentifiers.add(path.get('right').get('callee').node);
insertEach(path, state, context);
}
},
},
FunctionDeclaration: {
enter(path: NodePath<FunctionDeclaration>, state: State) {
enter(path: NodePath<t.FunctionDeclaration>, state: State) {
let id = path.get('id');
if (id.isIdentifier() && id.node.name === 'initializeRuntimeMacrosConfig') {
let pkg = packageCache.ownerOfFile(sourceFile(path, state));
Expand All @@ -72,7 +62,7 @@ export default function main(context: BabelContext): unknown {
},
},
CallExpression: {
enter(path: NodePath<CallExpression>, state: State) {
enter(path: NodePath<t.CallExpression>, state: State) {
let callee = path.get('callee');
if (!callee.isIdentifier()) {
return;
Expand Down Expand Up @@ -127,7 +117,7 @@ export default function main(context: BabelContext): unknown {
path.replaceWith(buildLiterals(result.value, context));
}
},
exit(path: NodePath<CallExpression>, state: State) {
exit(path: NodePath<t.CallExpression>, state: State) {
let callee = path.get('callee');
if (!callee.isIdentifier()) {
return;
Expand All @@ -151,15 +141,15 @@ export default function main(context: BabelContext): unknown {
replacePaths.push(path);
state.calledIdentifiers.add(callee.node);
} else {
let r = context.types.identifier('require');
let r = t.identifier('require');
state.generatedRequires.add(r);
callee.replaceWith(r);
}
return;
}
},
},
ReferencedIdentifier(path: NodePath<Identifier>, state: State) {
ReferencedIdentifier(path: NodePath<t.Identifier>, state: State) {
for (let candidate of [
'dependencySatisfies',
'moduleExists',
Expand Down Expand Up @@ -211,7 +201,7 @@ export default function main(context: BabelContext): unknown {
// Our importSync macro has been compiled to `require`. But we want to
// distinguish that from any pre-existing, user-written `require` in an
// Ember addon, which should retain its *runtime* meaning.
path.replaceWith(context.types.memberExpression(context.types.identifier('window'), path.node));
path.replaceWith(t.memberExpression(t.identifier('window'), path.node));
}
},
};
Expand All @@ -221,7 +211,7 @@ export default function main(context: BabelContext): unknown {
// optional chaining. To make that work we need to see the optional chaining
// before preset-env compiles them away.
(visitor as any).OptionalMemberExpression = {
enter(path: NodePath<OptionalMemberExpression>, state: State) {
enter(path: NodePath<t.OptionalMemberExpression>, state: State) {
if (state.opts.mode === 'compile-time') {
let result = new Evaluator({ state }).evaluate(path);
if (result.confident) {
Expand All @@ -248,7 +238,7 @@ function pruneMacroImports(path: NodePath) {
}
}

function addRuntimeImports(path: NodePath<Program>, state: State, context: BabelContext) {
function addRuntimeImports(path: NodePath<t.Program>, state: State, context: BabelContext) {
let t = context.types;
if (state.neededRuntimeImports.size > 0) {
path.node.body.push(
Expand All @@ -262,14 +252,16 @@ function addRuntimeImports(path: NodePath<Program>, state: State, context: Babel
}
}

function addEagerImports(path: NodePath<Program>, state: State) {
function addEagerImports(path: NodePath<t.Program>, state: State, t: BabelContext['types']) {
let createdNames = new Set<string>();
for (let [specifier, replacePaths] of state.neededEagerImports.entries()) {
let local = unusedNameLike('a', replacePaths, createdNames);
createdNames.add(local);
path.node.body.push(importDeclaration([importNamespaceSpecifier(identifier(local))], stringLiteral(specifier)));
path.node.body.push(
t.importDeclaration([t.importNamespaceSpecifier(t.identifier(local))], t.stringLiteral(specifier))
);
for (let nodePath of replacePaths) {
nodePath.replaceWith(identifier(local));
nodePath.replaceWith(t.identifier(local));
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/shared-internals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"url": "https://github.com/embroider-build/embroider.git",
"directory": "packages/shared-internals"
},
"exports": {
".": {
"browser": "./src/browser-index.js",
"default": "./src/index.js"
}
},
"license": "MIT",
"author": "Edward Faulkner",
"main": "src/index.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/shared-internals/src/browser-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type { default as Package } from './package';
export { getOrCreate } from './get-or-create';
export { explicitRelative, extensionsPattern } from './paths';
export { default as PackageCache } from './browser-package-cache';

0 comments on commit ca63cef

Please sign in to comment.