Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable parallelization of @embroider/macros in non-Embroider builds #865

Merged
merged 4 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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