Skip to content

Commit

Permalink
Merge pull request #865 from thoov/parallelizable-macros
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Jun 24, 2021
2 parents b4269b1 + efe1ac7 commit 8e07721
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
24 changes: 20 additions & 4 deletions packages/macros/src/ember-addon-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,34 @@ 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,
baseDir() {
return join(__dirname, '..');
parallelBabel: {
requireFile: join(__dirname, 'glimmer', 'ast-transform.js'),
buildUsing: 'buildPlugin',
params: {
name,
get configs() {
return getConfigForPlugin();
},
methodName: index === 0 ? 'makeSecondTransform' : 'makeFirstTransform',
projectRoot: projectRoot,
baseDir,
},
},
baseDir: () => baseDir,
});
});
}
},

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

export function buildPlugin(params: {
name: string;
baseDir: string;
projectRoot: string;
methodName: string;
configs: any;
}) {
return {
name: params.name,
plugin:
params.methodName === 'makeFirstTransform'
? makeFirstTransform({ userConfigs: params.configs, baseDir: params.projectRoot })
: makeSecondTransform(),
baseDir: () => params.baseDir,
};
}

export function makeFirstTransform(opts: { userConfigs: { [packageRoot: string]: unknown }; baseDir?: string }) {
function embroiderFirstMacrosTransform(env: {
syntax: { builders: any };
Expand Down Expand Up @@ -97,7 +114,6 @@ export function makeFirstTransform(opts: { userConfigs: { [packageRoot: string]:
export function makeSecondTransform() {
function embroiderSecondMacrosTransform(env: { syntax: { builders: any } }) {
let scopeStack: string[][] = [];

return {
name: '@embroider/macros/second',

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
5 changes: 3 additions & 2 deletions tests/scenarios/macro-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ appScenarios
});

test(`yarn test`, async function (assert) {
let result = await app.execute(`yarn test`);
let result = await app.execute(`cross-env THROW_UNLESS_PARALLELIZABLE=1 yarn test`);
assert.equal(result.exitCode, 0, result.output);
});

test(`CLASSIC=true yarn test`, async function (assert) {
let result = await app.execute(`cross-env CLASSIC=true yarn test`);
// throw_unless_parallelizable is enabled to ensure that @embroider/macros is parallelizable
let result = await app.execute(`cross-env THROW_UNLESS_PARALLELIZABLE=1 CLASSIC=true yarn test`);
assert.equal(result.exitCode, 0, result.output);
});
});
Expand Down

0 comments on commit 8e07721

Please sign in to comment.