Skip to content

Commit

Permalink
Merge pull request #890 from embroider-build/babel-cleanup
Browse files Browse the repository at this point in the history
Drop support for apps that use babel 6
  • Loading branch information
ef4 committed Jul 5, 2021
2 parents 81ca52f + 7f60d15 commit 3064442
Show file tree
Hide file tree
Showing 19 changed files with 27 additions and 372 deletions.
3 changes: 0 additions & 3 deletions packages/babel-loader-7/index.js

This file was deleted.

21 changes: 0 additions & 21 deletions packages/babel-loader-7/package.json

This file was deleted.

6 changes: 3 additions & 3 deletions packages/compat/src/v1-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ export default class V1App {
}

@Memoize()
babelMajorVersion(): 6 | 7 {
babelMajorVersion(): 7 {
let babelAddon = this.app.project.addons.find((a: any) => a.name === 'ember-cli-babel');
if (babelAddon) {
let major = Number(babelAddon.pkg.version.split('.')[0]);
if (major !== 6 && major !== 7) {
throw new Error(`@embroider/compat only supports v1 addons that use babel 6 or 7`);
if (major !== 7) {
throw new Error(`@embroider/compat only supports v1 addons that use babel 7`);
}
return major;
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@embroider/macros": "0.42.3",
"@embroider/shared-internals": "0.42.3",
"assert-never": "^1.2.1",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"broccoli-node-api": "^1.7.0",
"broccoli-persistent-filter": "^3.1.2",
"broccoli-plugin": "^4.0.7",
Expand Down
18 changes: 3 additions & 15 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface AppAdapter<TreeNames> {
babelConfig(): TransformOptions;

// the babel version that works with your babelConfig.
babelMajorVersion(): 6 | 7;
babelMajorVersion(): 7;

// The environment settings used to control Ember itself. In a classic app,
// this comes from the EmberENV property returned by config/environment.js.
Expand Down Expand Up @@ -362,13 +362,7 @@ export class AppBuilder<TreeNames> {

// Our stage3 code is always allowed to use dynamic import. We may emit it
// ourself when splitting routes.
babel.plugins.push(
require.resolve(
this.adapter.babelMajorVersion() === 6
? 'babel-plugin-syntax-dynamic-import'
: '@babel/plugin-syntax-dynamic-import'
)
);
babel.plugins.push(require.resolve('@babel/plugin-syntax-dynamic-import'));
return babel;
}

Expand All @@ -382,13 +376,7 @@ export class AppBuilder<TreeNames> {

// Our stage3 code is always allowed to use dynamic import. We may emit it
// ourself when splitting routes.
babel.plugins.push(
require.resolve(
this.adapter.babelMajorVersion() === 6
? 'babel-plugin-syntax-dynamic-import'
: '@babel/plugin-syntax-dynamic-import'
)
);
babel.plugins.push(require.resolve('@babel/plugin-syntax-dynamic-import'));

// https://github.com/webpack/webpack/issues/12154
babel.plugins.push(require.resolve('./rename-require-plugin'));
Expand Down
7 changes: 0 additions & 7 deletions packages/macros/src/babel/babel-context.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/macros/src/babel/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { buildLiterals, Evaluator } from './evaluate-json';
import type { CallExpression, ForOfStatement, Identifier } from '@babel/types';
import error from './error';
import State, { cloneDeep } from './state';
import { BabelContext } from './babel-context';
import type * as Babel from '@babel/core';

type CallEachExpression = NodePath<CallExpression> & {
get(callee: 'callee'): NodePath<Identifier>;
Expand All @@ -24,7 +24,7 @@ export function isEachPath(path: NodePath<ForOfStatement>): path is EachPath {
return false;
}

export function insertEach(path: EachPath, state: State, context: BabelContext) {
export function insertEach(path: EachPath, state: State, context: typeof Babel) {
let args = path.get('right').get('arguments');
if (args.length !== 1) {
throw error(path, `the each() macro accepts exactly one argument, you passed ${args.length}`);
Expand Down
9 changes: 5 additions & 4 deletions packages/macros/src/babel/evaluate-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import type {
MemberExpression,
Expression,
ExpressionStatement,
File,
CallExpression,
OptionalMemberExpression,
} from '@babel/types';
import type * as Babel from '@babel/core';
import State, { owningPackage } from './state';
import dependencySatisfies from './dependency-satisfies';
import moduleExists from './module-exists';
import getConfig from './get-config';
import { BabelContext } from './babel-context';

type OpValue = string | boolean | number;

Expand Down Expand Up @@ -421,11 +422,11 @@ export function assertArray<T>(input: T | T[]): T[] {
return input;
}

export function buildLiterals(value: unknown | undefined, babelContext: BabelContext): Identifier | ObjectExpression {
export function buildLiterals(value: unknown | undefined, babelContext: typeof Babel): Identifier | ObjectExpression {
if (typeof value === 'undefined') {
return babelContext.types.identifier('undefined');
}
let statement = babelContext.template(`a(${JSON.stringify(value)})`)() as ExpressionStatement;
let expression = statement.expression as CallExpression;
let statement = babelContext.parse(`a(${JSON.stringify(value)})`) as File;
let expression = (statement.program.body[0] as ExpressionStatement).expression as CallExpression;
return expression.arguments[0] as ObjectExpression;
}
8 changes: 4 additions & 4 deletions packages/macros/src/babel/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PackageCache, Package } from '@embroider/shared-internals';
import error from './error';
import { Evaluator, assertArray, buildLiterals, ConfidentResult } from './evaluate-json';
import assertNever from 'assert-never';
import { BabelContext } from './babel-context';
import type * as Babel from '@babel/core';

const packageCache = PackageCache.shared('embroider-stage3');
export type Mode = 'own' | 'getGlobalConfig' | 'package';
Expand Down Expand Up @@ -48,7 +48,7 @@ export default function getConfig(path: NodePath<CallExpression>, state: State,
// this is the imperative version that's invoked directly by the babel visitor
// when we encounter getConfig. It's implemented in terms of getConfig so we can
// be sure we have the same semantics.
export function insertConfig(path: NodePath<CallExpression>, state: State, mode: Mode, context: BabelContext) {
export function insertConfig(path: NodePath<CallExpression>, state: State, mode: Mode, context: typeof Babel) {
if (state.opts.mode === 'compile-time') {
let config = getConfig(path, state, mode);
let collapsed = collapse(path, config);
Expand Down Expand Up @@ -100,15 +100,15 @@ function collapse(path: NodePath, config: any) {
}
}

export function inlineRuntimeConfig(path: NodePath<FunctionDeclaration>, state: State, context: BabelContext) {
export function inlineRuntimeConfig(path: NodePath<FunctionDeclaration>, state: State, context: typeof Babel) {
path.get('body').node.body = [
context.types.returnStatement(
buildLiterals({ packages: state.opts.userConfigs, global: state.opts.globalConfig }, context)
),
];
}

function calleeName(path: NodePath<CallExpression>, context: BabelContext): string {
function calleeName(path: NodePath<CallExpression>, context: typeof Babel): string {
let callee = path.node.callee;
if (context.types.isIdentifier(callee)) {
return callee.name;
Expand Down
8 changes: 4 additions & 4 deletions packages/macros/src/babel/macros-babel-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { isEachPath, insertEach } from './each';
import error from './error';
import failBuild from './fail-build';
import { Evaluator, buildLiterals } from './evaluate-json';
import { BabelContext } from './babel-context';
import type * as Babel from '@babel/core';

const packageCache = PackageCache.shared('embroider-stage3');

export default function main(context: BabelContext): unknown {
export default function main(context: typeof Babel): unknown {
let t = context.types;
let visitor = {
Program: {
Expand Down Expand Up @@ -238,7 +238,7 @@ function pruneMacroImports(path: NodePath) {
}
}

function addRuntimeImports(path: NodePath<t.Program>, state: State, context: BabelContext) {
function addRuntimeImports(path: NodePath<t.Program>, state: State, context: typeof Babel) {
let t = context.types;
if (state.neededRuntimeImports.size > 0) {
path.node.body.push(
Expand All @@ -252,7 +252,7 @@ function addRuntimeImports(path: NodePath<t.Program>, state: State, context: Bab
}
}

function addEagerImports(path: NodePath<t.Program>, state: State, t: BabelContext['types']) {
function addEagerImports(path: NodePath<t.Program>, state: State, t: typeof Babel['types']) {
let createdNames = new Set<string>();
for (let [specifier, replacePaths] of state.neededEagerImports.entries()) {
let local = unusedNameLike('a', replacePaths, createdNames);
Expand Down
4 changes: 2 additions & 2 deletions packages/macros/tests/babel/eval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { allBabelVersions } from '@embroider/test-support';
import { Evaluator, buildLiterals } from '../../src/babel/evaluate-json';
import { VariableDeclarator, isIdentifier, Expression } from '@babel/types';
import { NodePath } from '@babel/traverse';
import { BabelContext } from '../../src/babel/babel-context';
import type * as Babel from '@babel/core';

describe('evaluation', function () {
allBabelVersions({
Expand Down Expand Up @@ -115,7 +115,7 @@ function isNodePathPresent(path: NodePath<Expression | null | undefined>): path
return path.node != null;
}

function testEval(babelContext: BabelContext) {
function testEval(babelContext: typeof Babel) {
let visitor = {
VariableDeclarator: {
exit(path: NodePath<VariableDeclarator>) {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-internals/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface AppMeta {
babel: {
filename: string;
isParallelSafe: boolean;
majorVersion: 6 | 7;
majorVersion: 7;
fileFilter: string;
};
'resolvable-extensions': string[];
Expand Down
2 changes: 0 additions & 2 deletions packages/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
"@babel/core": "^7.14.5",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.14.5",
"@babel/plugin-proposal-optional-chaining": "^7.14.5",
"@embroider/babel-loader-7": "0.42.3",
"@embroider/babel-loader-8": "0.42.3",
"@embroider/hbs-loader": "0.42.3",
"@embroider/shared-internals": "0.42.3",
"@types/loader-utils": "^2.0.2",
"@types/source-map": "^0.5.7",
"@types/supports-color": "^8.1.0",
"babel-core": "^6.26.3",
"babel-loader": "^8.2.2",
"babel-preset-env": "^1.7.0",
"supports-color": "^8.1.0",
Expand Down
15 changes: 1 addition & 14 deletions packages/webpack/src/ember-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
Packager,
PackagerConstructor,
Variant,
applyVariantToBabelConfig,
getAppMeta,
getPackagerCacheDir,
getOrCreate,
Expand Down Expand Up @@ -208,7 +207,6 @@ const Webpack: PackagerConstructor<Options> = class Webpack implements Packager
// wants to control those.
'thread-loader': require.resolve('thread-loader'),
'babel-loader-8': require.resolve('@embroider/babel-loader-8'),
'babel-loader-7': require.resolve('@embroider/babel-loader-7'),
'css-loader': require.resolve('css-loader'),
'style-loader': require.resolve('style-loader'),
},
Expand Down Expand Up @@ -574,23 +572,12 @@ function nonNullArray<T>(array: T[]): NonNullable<T>[] {
}

function babelLoaderOptions(
majorVersion: 6 | 7,
_majorVersion: 7,
variant: Variant,
appBabelConfigPath: string,
extraOptions: BabelLoaderOptions | undefined
) {
const cacheDirectory = getPackagerCacheDir('webpack-babel-loader');
if (majorVersion === 6) {
return {
loader: 'babel-loader-7',
options: {
// eslint-disable-next-line @typescript-eslint/no-require-imports
...applyVariantToBabelConfig(variant, require(appBabelConfigPath)),
cacheDirectory,
...extraOptions,
},
};
}
const options: BabelLoaderOptions & { variant: Variant; appBabelConfigPath: string } = {
variant,
appBabelConfigPath,
Expand Down
21 changes: 0 additions & 21 deletions test-packages/support/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { join } from 'path';
import 'jest';
import { transform as transform6, TransformOptions as Options6 } from 'babel-core';
import { transform as transform7, TransformOptions as Options7 } from '@babel/core';
import escapeRegExp from 'lodash/escapeRegExp';
import { createContext, Script } from 'vm';
Expand Down Expand Up @@ -58,31 +57,11 @@ export interface Transform {
}

export function allBabelVersions(params: {
babelConfig(major: 6): Options6;
babelConfig(major: 7): Options7;
createTests(transform: Transform): void;
includePresetsTests?: boolean;
}) {
function versions(usePresets: boolean) {
describe('babel6', function () {
function transform(code: string, opts?: { filename?: string }) {
let options6: Options6 = params.babelConfig(6);
if (!options6.filename) {
options6.filename = 'sample.js';
}
if (usePresets) {
options6.presets = presetsFor(6);
}
if (opts && opts.filename) {
options6.filename = opts.filename;
}
return transform6(code, options6).code!;
}
transform.babelMajorVersion = 6 as 6;
transform.usingPresets = usePresets;
params.createTests(transform);
});

describe('babel7', function () {
function transform(code: string, opts?: { filename?: string }) {
let options7: Options7 = params.babelConfig(7);
Expand Down
1 change: 0 additions & 1 deletion test-packages/support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"@glimmer/component": "^1.0.0",
"@types/fs-extra": "^5.0.4",
"@types/node": "^10.5.2",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"broccoli": "^3.4.2",
"console-ui": "^3.0.0",
Expand Down
Loading

0 comments on commit 3064442

Please sign in to comment.