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

Adds support for babel config file #371

Merged
merged 12 commits into from
Jan 4, 2021
50 changes: 50 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const emberPlugins = require("./lib/ember-plugins");
suchitadoshi1987 marked this conversation as resolved.
Show resolved Hide resolved

module.exports = function (api) {
const resolvePath = require("./lib/relative-module-paths")
.resolveRelativeModulePath;

api.cache(true);

return {
presets: [
[
require.resolve("@babel/preset-env"),
{
targets: require("./tests/dummy/config/targets"),

// do we still need to disable this and manually add modules plugins in the plugins array below?
modules: false,
},
],
],
plugins: [
// if they need/want external helpers
[
require.resolve("@babel/plugin-transform-runtime"),
{
version: require("@babel/plugin-transform-runtime/package").version,
regenerator: false,
useESModules: true,
},
],
[require.resolve("@babel/plugin-proposal-decorators"), { legacy: true }],
[require.resolve("@babel/plugin-proposal-class-properties")],
suchitadoshi1987 marked this conversation as resolved.
Show resolved Hide resolved

...emberPlugins({
emberDataVersionRequiresPackagesPolyfill: false,
shouldIgnoreJQuery: true,
shouldIgnoreEmberString: true,
disableEmberModulesAPIPolyfill: false,
}),

// configure module resolution plugins
// TODO: consider moving this into `...emberPlugins()` and allowing opt-out
[require.resolve("babel-plugin-module-resolver"), { resolvePath }],
[
require.resolve("@babel/plugin-transform-modules-amd"),
{ noInterop: true },
],
],
};
};
6 changes: 0 additions & 6 deletions .ember-cli
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
{
/**
Ember CLI sends analytics information by default. The data is completely
suchitadoshi1987 marked this conversation as resolved.
Show resolved Hide resolved
anonymous, but there are times when you might want to disable this behavior.

Setting `disableAnalytics` to true will prevent any data from being sent.
*/
suchitadoshi1987 marked this conversation as resolved.
Show resolved Hide resolved
"disableAnalytics": false
}
155 changes: 70 additions & 85 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
'use strict';

const {
_shouldCompileModules,
_shouldIncludeHelpers,
_shouldHandleTypeScript,
_getExtensions,
_parentName,
_shouldHighlightCode,
} = require("./lib/babel-options-util");

const VersionChecker = require('ember-cli-version-checker');
const clone = require('clone');
const path = require('path');
const semver = require('semver');

const defaultShouldIncludeHelpers = require('./lib/default-should-include-helpers');
const fs = require('fs');
const getBabelOptions = require('./lib/get-babel-options');
const findApp = require('./lib/find-app');

Expand All @@ -24,7 +31,7 @@ module.exports = {
let dep = checker.for('ember-cli', 'npm');

if (dep.lt('2.13.0')) {
throw new Error(`ember-cli-babel@7 (used by ${this._parentName()} at ${this.parent.root}) cannot be used by ember-cli versions older than 2.13, you used ${dep.version}`);
throw new Error(`ember-cli-babel@7 (used by ${_parentName(this.parent)} at ${this.parent.root}) cannot be used by ember-cli versions older than 2.13, you used ${dep.version}`);
}
},

Expand All @@ -35,31 +42,81 @@ module.exports = {

_debugTree() {
if (!this._cachedDebugTree) {
this._cachedDebugTree = require('broccoli-debug').buildDebugCallback(`ember-cli-babel:${this._parentName()}`);
this._cachedDebugTree = require('broccoli-debug').buildDebugCallback(`ember-cli-babel:${_parentName(this.parent)}`);
}

return this._cachedDebugTree.apply(null, arguments);
},

/**
* Default babel options
* @param {*} config
*/
_getDefaultBabelOptions(config = {}) {
let emberCLIBabelConfig = config["ember-cli-babel"];
let providedAnnotation;
let throwUnlessParallelizable;
let sourceMaps = false;
let shouldCompileModules = _shouldCompileModules(config, this.project);

if (emberCLIBabelConfig) {
providedAnnotation = emberCLIBabelConfig.annotation;
throwUnlessParallelizable = emberCLIBabelConfig.throwUnlessParallelizable;
}

if (config.babel && "sourceMaps" in config.babel) {
sourceMaps = config.babel.sourceMaps;
}

let options = {
annotation: providedAnnotation || `Babel: ${_parentName(this.parent)}`,
sourceMaps,
throwUnlessParallelizable,
filterExtensions: _getExtensions(config, this.parent),
plugins: [],
presets: []
};

if (shouldCompileModules) {
options.moduleIds = true;
options.getModuleId = require("./lib/relative-module-paths").getRelativeModulePath;
}

options.highlightCode = _shouldHighlightCode(this.parent);
options.babelrc = false;

return options;
},

transpileTree(inputTree, _config) {

let config = _config || this._getAddonOptions();
let description = `000${++count}`.slice(-3);
let postDebugTree = this._debugTree(inputTree, `${description}:input`);

let options = this.buildBabelOptions(config);
let options = this._getDefaultBabelOptions(config);
let output;
if (this._shouldDoNothing(options)) {
const babelConfigPath = path.resolve(this.parent.root, '.babelrc.js');
const isBabelConfigFilePresent = fs.existsSync(babelConfigPath)
suchitadoshi1987 marked this conversation as resolved.
Show resolved Hide resolved

if (isBabelConfigFilePresent) {
options = Object.assign({}, options, {
"presets":[ require.resolve(babelConfigPath) ],
suchitadoshi1987 marked this conversation as resolved.
Show resolved Hide resolved
});
} else {
options = Object.assign({}, options, this.buildBabelOptions(config));
}

if (!isBabelConfigFilePresent && this._shouldDoNothing(options)) {
output = postDebugTree;
} else {
let BabelTranspiler = require('broccoli-babel-transpiler');
let transpilationInput = postDebugTree;

if (this._shouldHandleTypeScript(config)) {
if (_shouldHandleTypeScript(config, this.parent)) {
let Funnel = require('broccoli-funnel');
let inputWithoutDeclarations = new Funnel(transpilationInput, { exclude: ['**/*.d.ts'] });
transpilationInput = this._debugTree(inputWithoutDeclarations, `${description}:filtered-input`);
}

output = new BabelTranspiler(transpilationInput, options);
}

Expand All @@ -69,7 +126,7 @@ module.exports = {
setupPreprocessorRegistry(type, registry) {
registry.add('js', {
name: 'ember-cli-babel',
ext: this._getExtensions(this._getAddonOptions()),
ext: _getExtensions(this._getAddonOptions(), this.parent),
toTree: (tree) => this.transpileTree(tree)
});
},
Expand Down Expand Up @@ -98,36 +155,6 @@ module.exports = {
}
},

_shouldIncludeHelpers(options) {
let appOptions = this._getAppOptions();
let customOptions = appOptions['ember-cli-babel'];

let shouldIncludeHelpers = false;

if (!this._shouldCompileModules(options)) {
// we cannot use external helpers if we are not transpiling modules
return false;
} else if (customOptions && 'includeExternalHelpers' in customOptions) {
shouldIncludeHelpers = customOptions.includeExternalHelpers === true;
} else {
// Check the project to see if we should include helpers based on heuristics.
shouldIncludeHelpers = defaultShouldIncludeHelpers(this.project);
}

let appEmberCliBabelPackage = this.project.addons.find(a => a.name === 'ember-cli-babel').pkg;
let appEmberCliBabelVersion = appEmberCliBabelPackage && appEmberCliBabelPackage.version;

if (appEmberCliBabelVersion && semver.gte(appEmberCliBabelVersion, '7.3.0-beta.1')) {
return shouldIncludeHelpers;
} else if (shouldIncludeHelpers) {
this.project.ui.writeWarnLine(
`${this._parentName()} attempted to include external babel helpers to make your build size smaller, but your root app's ember-cli-babel version is not high enough. Please update ember-cli-babel to v7.3.0-beta.1 or later.`
);
}

return false;
},

_getHelperVersion() {
if (!APP_BABEL_RUNTIME_VERSION.has(this.project)) {
let checker = new VersionChecker(this.project);
Expand All @@ -154,7 +181,7 @@ module.exports = {
// Helpers are a global config, so only the root application should bother
// generating and including the file.
let isRootBabel = this.parent === this.project;
let shouldIncludeHelpers = isRootBabel && this._shouldIncludeHelpers(this._getAppOptions());
let shouldIncludeHelpers = isRootBabel && _shouldIncludeHelpers(this._getAppOptions(), this);

if (!shouldIncludeHelpers) { return; }

Expand Down Expand Up @@ -233,37 +260,6 @@ module.exports = {
return (app && app.options) || {};
},

_parentName() {
let parentName;

if (this.parent) {
if (typeof this.parent.name === 'function') {
parentName = this.parent.name();
} else {
parentName = this.parent.name;
}
}

return parentName;
},

_getExtensions(config) {
let shouldHandleTypeScript = this._shouldHandleTypeScript(config);
let emberCLIBabelConfig = config['ember-cli-babel'] || {};
return emberCLIBabelConfig.extensions || (shouldHandleTypeScript ? ['js', 'ts'] : ['js']);
},

_shouldHandleTypeScript(config) {
let emberCLIBabelConfig = config['ember-cli-babel'] || {};
if (typeof emberCLIBabelConfig.enableTypeScriptTransform === 'boolean') {
return emberCLIBabelConfig.enableTypeScriptTransform;
}
let typeScriptAddon = this.parent.addons
&& this.parent.addons.find(a => a.name === 'ember-cli-typescript');
return typeof typeScriptAddon !== 'undefined'
&& semver.gte(typeScriptAddon.pkg.version, '4.0.0-alpha.1');
},

_getTargets() {
let targets = this.project && this.project.targets;

Expand All @@ -290,18 +286,7 @@ module.exports = {
* @method shouldCompileModules
*/
shouldCompileModules() {
return this._shouldCompileModules(this._getAddonOptions());
},

// will use any provided configuration
_shouldCompileModules(options) {
let addonOptions = options['ember-cli-babel'];

if (addonOptions && 'compileModules' in addonOptions) {
return addonOptions.compileModules;
} else {
return semver.gt(this.project.emberCLIVersion(), '2.12.0-alpha.1');
}
return _shouldCompileModules(this._getAddonOptions(), this.project);
},

// detect if running babel would do nothing... and do nothing instead
Expand Down
Loading