diff --git a/README.md b/README.md index aa28cc8c..a687eec5 100755 --- a/README.md +++ b/README.md @@ -314,7 +314,7 @@ let app = new EmberAddon(defaults, { ```js //babel.config.js -const { emberPlugins } = require("ember-cli-babel"); +const { buildEmberPlugins } = require("ember-cli-babel"); module.exports = function (api) { api.cache(true); @@ -339,7 +339,7 @@ module.exports = function (api) { }, ], // this is where all the ember required plugins would reside - ...emberPlugins(__dirname, { ...options }), + ...buildEmberPlugins(__dirname, { /*customOptions if you want to pass in */ }), ], }; }; diff --git a/index.js b/index.js index 0a32e273..b2a37c54 100644 --- a/index.js +++ b/index.js @@ -19,15 +19,6 @@ const emberPlugins = require('./lib/ember-plugins'); const APP_BABEL_RUNTIME_VERSION = new WeakMap(); -// Ref: https://github.com/babel/babel/blob/c6aea4e85d2b8f3e82575642d30b01c8cbe112a9/packages/babel-core/src/config/files/configuration.js#L24 -// supported babel config root filenames. -const ROOT_CONFIG_FILENAMES = [ - "babel.config.js", - "babel.config.cjs", - "babel.config.mjs", - "babel.config.json", -]; - let count = 0; module.exports = { @@ -96,6 +87,7 @@ module.exports = { options.highlightCode = _shouldHighlightCode(this.parent); options.babelrc = false; + options.configFile = false; return options; }, @@ -108,16 +100,31 @@ module.exports = { let options = this._getDefaultBabelOptions(config); let output; - const babelConfigFile = ROOT_CONFIG_FILENAMES.find((fileName) => - fs.existsSync(path.resolve(this.parent.root, fileName)) - ); - const customAddonConfig = config['ember-cli-babel']; - const shouldUseBabelConfigFile = customAddonConfig && customAddonConfig.useBabelConfig && babelConfigFile; - + const shouldUseBabelConfigFile = customAddonConfig && customAddonConfig.useBabelConfig; + if (shouldUseBabelConfigFile) { + // Ref: https://github.com/babel/babel/blob/c6aea4e85d2b8f3e82575642d30b01c8cbe112a9/packages/babel-core/src/config/files/configuration.js#L24 + // supported babel config root filenames. + const ROOT_CONFIG_FILENAMES = [ + "babel.config.js", + "babel.config.cjs", + "babel.config.mjs", + "babel.config.json", + ].map(fileName => path.resolve(this.parent.root, fileName)); + + const babelConfigFile = ROOT_CONFIG_FILENAMES.find((fileName) => + fs.existsSync(fileName) + ); + if (!babelConfigFile) { + throw new Error( + "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(path.resolve(this.parent.root, babelConfigFile)) ], + presets: [ + require.resolve(babelConfigFile), + ], }); } else { options = Object.assign({}, options, this.buildBabelOptions(config)); diff --git a/lib/ember-plugins.js b/lib/ember-plugins.js index c54a5fa5..275cfb83 100644 --- a/lib/ember-plugins.js +++ b/lib/ember-plugins.js @@ -148,6 +148,22 @@ function _getProposalDecoratorsAndClassPlugins(config) { } } +/** + * This function allows returns all the required Ember specific babel plugins for the app to transpile correctly. + * As the first argument, you need to pass in the appRoot (which is usually the __dirname). + * As the second argument, which is optional, you can choose to turn the switch on and off of several plugins that this function returns. + * **List of supported configs** + * { + * disableModuleResolution: boolean, // determines if you want the module resolution enabled + * emberDataVersionRequiresPackagesPolyfill: boolean, // enable ember data's polyfill + * shouldIgnoreJQuery: boolean, // ignore jQuery + * shouldIgnoreEmberString: boolean, // ignore ember string + * shouldIgnoreDecoratorAndClassPlugins: boolean, // disable decorator plugins + * disableEmberModulesAPIPolyfill: boolean, // disable ember modules API polyfill + * } + * @param {string} appRoot - root directory of your project + * @param {object} config - config options to finetune the plugins + */ module.exports = function (appRoot, config = {}) { return [] .concat(