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

New babel config #2016

Closed
wants to merge 7 commits into from
71 changes: 71 additions & 0 deletions packages/compat/src/babel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { locateEmbroiderWorkingDir } from '@embroider/core';
import { existsSync, readJSONSync } from 'fs-extra';
import { join } from 'path';

export function loadLegacyPlugins() {
const path = join(locateEmbroiderWorkingDir(process.cwd()), '_babel_config_.json');
if (!existsSync(path)) {
throw new Error(`Could not load the Babel plugins required by classic addons from ${path}`);
}

const { plugins } = readJSONSync(path);
_warnIfNoLegacyPlugins(plugins);
return plugins ?? [];
}

export function loadPluginDebugMacros() {
return [
[
'babel-plugin-debug-macros',
{
flags: [
{
source: '@glimmer/env',
flags: {
DEBUG: true,
CI: false,
},
},
],
debugTools: {
isDebug: true,
source: '@ember/debug',
assertPredicateIndex: 1,
},
externalizeHelpers: {
module: '@ember/debug',
},
},
'@ember/debug stripping',
],
[
'babel-plugin-debug-macros',
{
externalizeHelpers: {
module: '@ember/application/deprecations',
},
debugTools: {
isDebug: true,
source: '@ember/application/deprecations',
assertPredicateIndex: 1,
},
},
'@ember/application/deprecations stripping',
],
];
}

function _warnIfNoLegacyPlugins(legacyPlugins: any) {
if (!legacyPlugins || !legacyPlugins.length) {
console.warn(`
Your Ember app doesn't use any classic addon that requires Babel plugins.
In your babel.config.cjs, you can safely remove the usage of loadLegacyPlugins.

Remove:
- const { loadLegacyPlugins } = require('@embroider/compat');
- ...loadLegacyPlugins(),

If you install a classic addon in your app afterward, make sure to add any Babel config it may require to babel.config.cjs.
`);
Comment on lines +58 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry that this is premature. Long after someone has seen this message and removed the legacy plugin support, someone on their team could add an addon that needs it and be puzzled why the addon doesn't work.

@mansona just suggested to me that perhaps it would be better to wait to tell people to remove the legacy plugin support until we can do a deprecation against any v1 addon trying to add babel plugins, and that sounds reasonable.

}
}
3 changes: 3 additions & 0 deletions packages/compat/src/compat-app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ export class CompatAppBuilder {
`module.exports = ${JSON.stringify(pconfig.config, null, 2)}`,
'utf8'
);
outputJSONSync(join(locateEmbroiderWorkingDir(this.compatApp.root), '_babel_config_.json'), pconfig.config, {
spaces: 2,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is adding a new output location without removing the previous one on the lines above?

Also, similar to my comment in combat-app.ts, this value we're creating no longer needs to be a whole babel config. It's just a file that exports a plugins array (and eventually it will probably also export an AST transforms array, but that is a separate topic.).

writeFileSync(
join(locateEmbroiderWorkingDir(this.compatApp.root), '_babel_filter_.js'),
babelFilterTemplate({ skipBabel: this.options.skipBabel, appRoot: this.origAppPackage.root }),
Expand Down
31 changes: 15 additions & 16 deletions packages/compat/src/compat-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,8 @@ export default class CompatApp {

@Memoize()
babelConfig(): TransformOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole method can change name and return type because its only remaining responsibility is to capture the plugins added by v1 addons. It could become captureLegacyBabelPlugins(): { plugins: PluginItem[] }

// this finds all the built-in babel configuration that comes with ember-cli-babel
const babelAddon = (this.legacyEmberAppInstance.project as any).findAddonByName('ember-cli-babel');
const babelConfig = babelAddon.buildBabelOptions({
'ember-cli-babel': {
...this.legacyEmberAppInstance.options['ember-cli-babel'],
includeExternalHelpers: true,
compileModules: false,
disableDebugTooling: false,
disablePresetEnv: false,
disableEmberModulesAPIPolyfill: false,
},
});
BlueCutOfficial marked this conversation as resolved.
Show resolved Hide resolved

let plugins = babelConfig.plugins as any[];
let presets = babelConfig.presets;
let plugins: any[] = [];
let presets: any[] = [];

// this finds any custom babel configuration that's on the app (either
// because the app author explicitly added some, or because addons have
Expand All @@ -250,7 +237,19 @@ export default class CompatApp {
plugins = appBabel.plugins.concat(plugins);
}
if (appBabel.presets) {
presets = appBabel.presets.concat(presets);
if (this.legacyEmberAppInstance.options.useCustomBabelPresets) {
throw new Error(`
The following Babel presets have been found on the app custom Babel configuration:

${presets}

Either these presets have been added explicitly to the app (e.g. through ember-cli-babel options in ember-cli-build.js), either classic addons have pushed these presets into the app.
With Embroider, you have full control over the Babel config via babel.config.cjs, and babel.config.cjs should be the only source of truth regarding Babel configuration; so classic addons no longer have the ability to push Babel presets.

1. Add the presets you want to use to the babel.config.cjs.
2. Once babel.config.cjs has the presets you need, remove the present error by setting "useCustomBabelPresets" to false in the build options.
`);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/compat/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { loadLegacyPlugins, loadPluginDebugMacros } from './babel';
export { default as App } from './compat-app';
export { default as Addons } from './compat-addons';
export { default as Options, recommendedOptions } from './options';
Expand Down
9 changes: 9 additions & 0 deletions packages/compat/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ export default interface Options extends CoreOptions {
// switching to Embroider, but is no longer necessary once content-for
// 'config-module' code has been properly moved to the app-side.
useAddonConfigModule?: boolean;

// Allows you to cancel the error that custom Babel presets have been
// found. This error notifies developers switching to Embroider that
// presets pushed by classic addons will no longer work. It also brings
// awareness about how to migrate from Babel build options to Babel config,
// but is no longer necessary once presets have been properly defined in the
// Babel config.
useCustomBabelPresets?: boolean;
}

const defaults = Object.assign(coreWithDefaults(), {
Expand All @@ -127,6 +135,7 @@ const defaults = Object.assign(coreWithDefaults(), {
availableContentForTypes: [],
useAddonAppBoot: true,
useAddonConfigModule: true,
useCustomBabelPresets: true,
});

export function optionsWithDefaults(options?: Options): Required<Options> {
Expand Down
Loading
Loading