Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
thoov committed Jun 24, 2021
1 parent 785a437 commit b755b1d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
14 changes: 10 additions & 4 deletions packages/macros/src/ember-addon-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,26 @@ export = {
// MacrosConfig.astPlugins is static because in classic ember-cli, at this
// point there's not yet an appInstance, so we defer getting it and
// calling setConfig until our included hook.
let { plugins, setConfig } = MacrosConfig.astPlugins((this as any).parent.root);
let { plugins, setConfig, getConfigForPlugin } = MacrosConfig.astPlugins((this as any).parent.root);
this.setMacrosConfig = setConfig;
plugins.forEach((plugin, index) => {
let name = `@embroider/macros/${index}`;
let baseDir = join(__dirname, '..');
let projectRoot = (this as any).parent.root;

registry.add('htmlbars-ast-plugin', {
name: `@embroider/macros/${index}`,
name,
plugin,
parallelBabel: {
requireFile: join(__dirname, 'glimmer', 'ast-transform.js'),
buildUsing: 'makePlugin',
buildUsing: 'buildPlugin',
params: {
name,
plugin,
get configs() {
return getConfigForPlugin();
},
methodName: index === 0 ? 'makeSecondTransform' : 'makeFirstTransform',
projectRoot: projectRoot,
baseDir,
},
},
Expand All @@ -105,5 +110,6 @@ export = {
});
}
},

options: {},
};
29 changes: 11 additions & 18 deletions packages/macros/src/glimmer/ast-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import { maybeAttrs } from './macro-maybe-attrs';
import { macroIfBlock, macroIfExpression, macroIfMustache } from './macro-condition';
import { failBuild } from './fail-build';

export function makePlugin(params: { name: string; plugin: Function; baseDir: string }) {
export function buildPlugin(params: {
name: string;
baseDir: string;
projectRoot: string;
methodName: string;
configs: any;
}) {
return {
name: params.name,
parallelBabel: (params.plugin as any).parallelBabel,
plugin: params.plugin,
plugin:
params.methodName === 'makeFirstTransform'
? makeFirstTransform({ userConfigs: params.configs, baseDir: params.projectRoot })
: makeSecondTransform(),
baseDir: () => params.baseDir,
};
}
Expand Down Expand Up @@ -90,16 +98,6 @@ export function makeFirstTransform(opts: { userConfigs: { [packageRoot: string]:
};
}
(embroiderFirstMacrosTransform as any).embroiderMacrosASTMarker = true;
(embroiderFirstMacrosTransform as any).parallelBabel = {
requireFile: __filename,
buildUsing: 'makeFirstTransform',
get params() {
return {
userConfigs: opts.userConfigs,
baseDir: opts.baseDir,
};
},
};
return embroiderFirstMacrosTransform;
}

Expand Down Expand Up @@ -190,11 +188,6 @@ export function makeSecondTransform() {
};
}
(embroiderSecondMacrosTransform as any).embroiderMacrosASTMarker = true;
(embroiderSecondMacrosTransform as any).parallelBabel = {
requireFile: __filename,
buildUsing: 'makeSecondTransform',
params: undefined,
};
return embroiderSecondMacrosTransform;
}

Expand Down
15 changes: 13 additions & 2 deletions packages/macros/src/macros-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ export default class MacrosConfig {
return [join(__dirname, 'babel', 'macros-babel-plugin.js'), opts];
}

static astPlugins(owningPackageRoot?: string): { plugins: Function[]; setConfig: (config: MacrosConfig) => void } {
static astPlugins(owningPackageRoot?: string): {
plugins: Function[];
setConfig: (config: MacrosConfig) => void;
getConfigForPlugin(): any;
} {
let configs: MacrosConfig | undefined;
let plugins = [
makeFirstTransform({
Expand All @@ -279,7 +283,14 @@ export default class MacrosConfig {
function setConfig(c: MacrosConfig) {
configs = c;
}
return { plugins, setConfig };
function getConfigForPlugin() {
if (!configs) {
throw new Error(`Bug: @embroider/macros ast-transforms were not plugged into a MacrosConfig`);
}

return configs.userConfigs;
}
return { plugins, setConfig, getConfigForPlugin };
}

private mergerFor(pkgRoot: string) {
Expand Down

0 comments on commit b755b1d

Please sign in to comment.