Skip to content

Commit

Permalink
read config and pass it to the options
Browse files Browse the repository at this point in the history
  • Loading branch information
suchitadoshi1987 committed Dec 13, 2020
1 parent 3f9a7a6 commit 30b20cd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,33 +316,33 @@ let app = new EmberAddon(defaults, {

const { buildEmberPlugins } = require("ember-cli-babel");

module.exports = function (api) {
api.cache(true);

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

// do we still need to disable this and manually add modules plugins in the plugins array below?
modules: false,
},
],
],
plugins: [
// if you want external helpers
// if they need/want external helpers
[
require.resolve("@babel/plugin-transform-runtime"),
"@babel/plugin-transform-runtime",
{
version: require("@babel/plugin-transform-runtime/package").version,
version: "7.12.5",
regenerator: false,
useESModules: true,
},
],
// this is where all the ember required plugins would reside
...buildEmberPlugins(__dirname, { /*customOptions if you want to pass in */ }),

// eslint-disable-next-line no-undef
...buildEmberPlugins(__dirname, {}),
],
};
};
```

#### Ember Plugins
Expand Down
34 changes: 29 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {

const VersionChecker = require('ember-cli-version-checker');
const clone = require('clone');
const babel = require('@babel/core');
const path = require('path');
const fs = require('fs');
const getBabelOptions = require('./lib/get-babel-options');
Expand Down Expand Up @@ -92,6 +93,33 @@ module.exports = {
return options;
},

/**
* Returns the JSON config of the babel config file.
* If its a json config, then return the parsed JSON back
* If its a JS config, then load it and return it based on whether its a function or an object.
* @param {string} fp babel config file path
*/
_readConfig(fp) {
const fileExt = path.extname(fp);
switch (fileExt) {
case ".json":
return JSON.parse(fs.readFileSync(fp));
case ".js":
try {
// eslint-disable-next-line global-require,import/no-dynamic-require
const configModule = require(fp);
const configJS =
configModule && configModule.__esModule
? configModule.default || undefined
: configModule;
// if the babel config is a function then invoke it, else return the json config.
return typeof configJS === "function" ? configJS() : configJS;
} catch (error) {
error.message = `${fp}: Error while loading config = ${error.message}`;
throw error;
}
}
},
transpileTree(inputTree, _config) {

let config = _config || this._getAddonOptions();
Expand Down Expand Up @@ -121,11 +149,7 @@ 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."
);
}
options = Object.assign({}, options, {
presets: [
require.resolve(babelConfigFile),
],
});
options = Object.assign({}, options, this._readConfig(babelConfigFile));
} else {
options = Object.assign({}, options, this.buildBabelOptions(config));
}
Expand Down
5 changes: 1 addition & 4 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1605,14 +1605,11 @@ describe('babel config file', function() {
return prepareAddon(addon);
});
let pkg = JSON.parse(fixturifyProject.toJSON('package.json'));
fixturifyProject.files['babel.config.js'] = `module.exports = function (api) {
api.cache(true);
return {
fixturifyProject.files['babel.config.js'] = `module.exports = {
plugins: [
${plugins}
],
};
};
`;
fixturifyProject.writeSync();

Expand Down

0 comments on commit 30b20cd

Please sign in to comment.