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

Refactor buildBabelOptions and introduce getSupportedExtensions. #390

Merged
merged 3 commits into from
Mar 18, 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ allow you to use latest Javascript in your Ember CLI project.
+ [Adding Custom Plugins](#adding-custom-plugins)
+ [Additional Trees](#additional-trees)
+ [`buildBabelOptions` usage](#buildbabeloptions-usage)
+ [`getSupportedExtensions` usage](#getsupportedextensions-usage)
+ [`transpileTree` usage](#transpiletree-usage)
* [Debug Tooling](#debug-tooling)
+ [Debug Macros](#debug-macros)
Expand Down Expand Up @@ -412,6 +413,16 @@ let options = babelAddon.buildBabelOptions(config)
require('babel-core').transform('something', options);
```

#### `getSupportedExtensions` usage

```js
// find your babel addon (can use `this.findAddonByName('ember-cli-babel')` in [email protected] and newer)
let babelAddon = this.addons.find(addon => addon.name === 'ember-cli-babel');

// create the babel options to use elsewhere based on the config above
let extensions = babelAddon.getSupportedExtensions(config)
```

#### `transpileTree` usage

```js
Expand Down
124 changes: 64 additions & 60 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,64 +46,6 @@ module.exports = {

buildBabelOptions(_config) {
let config = _config || this._getAddonOptions();
return getBabelOptions(config, this);
},

_debugTree() {
if (!this._cachedDebugTree) {
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: []
};

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

options.highlightCode = _shouldHighlightCode(this.parent);
options.babelrc = false;
options.configFile = 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._getDefaultBabelOptions(config);
let output;

const customAddonConfig = config['ember-cli-babel'];
const shouldUseBabelConfigFile = customAddonConfig && customAddonConfig.useBabelConfig;
Expand All @@ -124,13 +66,74 @@ module.exports = {
"Missing babel config file in the project root. Please double check if the babel config file exists or turn off the `useBabelConfig` option in your ember-cli-build.js file."
);
}

// If the babel config file is found, then pass the path into the options for the transpiler
// parse and leverage the same.
options = Object.assign({}, options, { configFile: babelConfig.config });
return { configFile: babelConfig.config };
} else {
options = Object.assign({}, options, this.buildBabelOptions(config));
return getBabelOptions(config, this);
}
},

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

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

getSupportedExtensions(config) {
return _getExtensions(config, this.parent);
},

_buildBroccoliBabelTranspilerOptions(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: this.getSupportedExtensions(config),
plugins: []
};

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

options.highlightCode = _shouldHighlightCode(this.parent);
options.babelrc = false;
options.configFile = 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 = Object.assign({}, this._buildBroccoliBabelTranspilerOptions(config), this.buildBabelOptions(config));
let output;

const customAddonConfig = config['ember-cli-babel'];
const shouldUseBabelConfigFile = customAddonConfig && customAddonConfig.useBabelConfig;

if (!shouldUseBabelConfigFile && this._shouldDoNothing(options)) {
output = postDebugTree;
} else {
Expand All @@ -142,6 +145,7 @@ module.exports = {
let inputWithoutDeclarations = new Funnel(transpilationInput, { exclude: ['**/*.d.ts'] });
transpilationInput = this._debugTree(inputWithoutDeclarations, `${description}:filtered-input`);
}

output = new BabelTranspiler(transpilationInput, options);
}

Expand Down
12 changes: 6 additions & 6 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ describe('ember-cli-babel', function() {
it('disables reading `.babelrc`', function() {
let options = {};

let result = this.addon._getDefaultBabelOptions(options);
let result = this.addon._buildBroccoliBabelTranspilerOptions(options);

expect(result.babelrc).to.be.false;
});
Expand All @@ -1153,7 +1153,7 @@ describe('ember-cli-babel', function() {
name: 'derpy-herpy',
dependencies() { return {}; },
});
let result = this.addon._getDefaultBabelOptions();
let result = this.addon._buildBroccoliBabelTranspilerOptions();
expect(result.annotation).to.include('derpy-herpy');
});

Expand All @@ -1162,7 +1162,7 @@ describe('ember-cli-babel', function() {
name: 'derpy-herpy',
dependencies() { return {}; },
});
let result = this.addon._getDefaultBabelOptions();
let result = this.addon._buildBroccoliBabelTranspilerOptions();
expect(result.annotation).to.include('derpy-herpy');
});

Expand All @@ -1173,7 +1173,7 @@ describe('ember-cli-babel', function() {
}
};

let result = this.addon._getDefaultBabelOptions(options);
let result = this.addon._buildBroccoliBabelTranspilerOptions(options);
expect(result.annotation).to.equal('Hello World!');
});

Expand All @@ -1184,14 +1184,14 @@ describe('ember-cli-babel', function() {
}
};

let result = this.addon._getDefaultBabelOptions(options);
let result = this.addon._buildBroccoliBabelTranspilerOptions(options);
expect(result.sourceMaps).to.equal('inline');
});

it('disables reading `.babelrc`', function() {
let options = {};

let result = this.addon._getDefaultBabelOptions(options);
let result = this.addon._buildBroccoliBabelTranspilerOptions(options);

expect(result.babelrc).to.be.false;
});
Expand Down