Skip to content

Commit

Permalink
Merge pull request #747 from embroider-build/better-custom-babel-skip…
Browse files Browse the repository at this point in the history
…ping

better skipping of unnecessary stage1 transforms
  • Loading branch information
ef4 authored Mar 28, 2021
2 parents fb86676 + 7e22920 commit a22223d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 24 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
},
"outputCapture": "std"
},
{
"type": "node",
"request": "launch",
"name": "Build fastboot-app",
"program": "/Users/edward/hacking/sample/node_modules/.bin/ember",
"cwd": "${workspaceFolder}/test-packages/fastboot-app",
"args": ["build"],
"env": {
"JOBS": "1"
},
"outputCapture": "std"
},
{
"type": "node",
"request": "launch",
Expand Down
37 changes: 37 additions & 0 deletions packages/compat/src/detect-babel-plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PluginItem } from '@babel/core';

export function isEmberAutoImportDynamic(item: PluginItem): boolean {
let pluginPath: string;
if (typeof item === 'string') {
pluginPath = item;
} else if (Array.isArray(item) && item.length > 0 && typeof item[0] === 'string') {
pluginPath = item[0];
} else {
return false;
}
return /(^|\/)ember-auto-import\//.test(pluginPath);
}

export function isCompactReexports(item: PluginItem): boolean {
let pluginPath: string;
if (typeof item === 'string') {
pluginPath = item;
} else if (Array.isArray(item) && item.length > 0 && typeof item[0] === 'string') {
pluginPath = item[0];
} else {
return false;
}
return /(^|\/)babel-plugin-compact-reexports\//.test(pluginPath);
}

export function isColocationPlugin(item: PluginItem): boolean {
let pluginPath: string;
if (typeof item === 'string') {
pluginPath = item;
} else if (Array.isArray(item) && item.length > 0 && typeof item[0] === 'string') {
pluginPath = item[0];
} else {
return false;
}
return /(^|\/)ember-cli-htmlbars\/lib\/colocated-babel-plugin/.test(pluginPath);
}
13 changes: 0 additions & 13 deletions packages/compat/src/detect-ember-auto-import.ts

This file was deleted.

31 changes: 21 additions & 10 deletions packages/compat/src/v1-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import V1App from './v1-app';
import modulesCompat from './modules-compat';
import writeFile from 'broccoli-file-creator';
import SynthesizeTemplateOnlyComponents from './synthesize-template-only-components';
import { isEmberAutoImportDynamic } from './detect-ember-auto-import';
import { isCompactReexports } from './detect-compact-reexports';
import { isEmberAutoImportDynamic, isCompactReexports, isColocationPlugin } from './detect-babel-plugins';
import { ResolvedDep } from '@embroider/core/src/resolver';

const stockTreeNames = Object.freeze([
Expand Down Expand Up @@ -179,12 +178,18 @@ export default class V1Addon {
},
});

if (this.needsCustomBabel()) {
// there is customized babel behavior needed, so we will leave
// ember-cli-babel in place, but modify its config so it doesn't do the
// things we don't want to do in stage1.
this.updateBabelConfig();
} else {
// first, look into the babel config and related packages to decide whether
// we need to run babel at all in this stage.
let needsCustomBabel = this.needsCustomBabel();

// regardless of the answer, we modify the babel config, because even if
// we're unregistering ember-cli-babel, some addons manually invoke
// ember-cli-babel in their custom hooks, and in that case we want to be
// sure we've taken out the babel plugins that really shouldn't run at this
// stage.
this.updateBabelConfig();

if (!needsCustomBabel) {
// no custom babel behavior, so we don't run the ember-cli-babel
// preprocessor at all. We still need to register a no-op preprocessor to
// prevent ember-cli from emitting a deprecation warning.
Expand Down Expand Up @@ -247,8 +252,7 @@ export default class V1Addon {
return true;
}

let babelConfig = this.options.babel as TransformOptions | undefined;
if (babelConfig && babelConfig.plugins && babelConfig.plugins.length > 0) {
if ((this.options.babel?.plugins?.filter(babelPluginAllowedInStage1)?.length ?? 0) > 0) {
// this addon has custom babel plugins, so we need to run them here in
// stage1
return true;
Expand Down Expand Up @@ -1016,6 +1020,13 @@ function babelPluginAllowedInStage1(plugin: PluginItem) {
return false;
}

if (isColocationPlugin(plugin)) {
// template co-location is a first-class feature we support directly, so
// whether or not the app brought a plugin for it we're going to do it our
// way.
return false;
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/compat/src/v1-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import resolvePackagePath from 'resolve-package-path';
import Concat from 'broccoli-concat';
import mapKeys from 'lodash/mapKeys';
import SynthesizeTemplateOnlyComponents from './synthesize-template-only-components';
import { isEmberAutoImportDynamic } from './detect-ember-auto-import';
import { isEmberAutoImportDynamic } from './detect-babel-plugins';

// This controls and types the interface between our new world and the classic
// v1 app instance.
Expand Down
3 changes: 3 additions & 0 deletions test-packages/fastboot-addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ module.exports = {
manifest.vendorFiles.push('fastboot-addon/sample.js');
return manifest;
},
included(...args) {
this._super.included.call(this, ...args);
},
};

0 comments on commit a22223d

Please sign in to comment.