From 7267b2cdc32283c2727e9b3814f5f4eb2e856a36 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Thu, 18 Mar 2021 16:29:55 -0400 Subject: [PATCH] Fix compatibility with ember-auto-import. ember-cli-babel@7.24 accidentally broke compatibility with ember-auto-import when it fixed `buildBabelOptions` to only return custom babel config **not** broccoli-babel-transpiler config. This fixes that compatiblity in the case where `babelAddon.buildBabelOptions()` is called, but still preserves the ability to get _either_ the broccoli-babel-transpiler config _or_ the `@babel/core` config. --- README.md | 4 ++-- index.js | 28 +++++++++++++++++++---- node-tests/addon-test.js | 49 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ce6b8fbe..ad57ad63 100755 --- a/README.md +++ b/README.md @@ -380,7 +380,7 @@ interface EmberCLIBabel { /** Used to generate the options that will ultimately be passed to babel itself. */ - buildBabelOptions(config?: EmberCLIBabelConfig): Opaque; + buildBabelOptions(type: 'babel' | 'broccoli', config?: EmberCLIBabelConfig): Opaque; /** Supports easier transpilation of non-standard input paths (e.g. to transpile @@ -407,7 +407,7 @@ interface EmberCLIBabel { let babelAddon = this.addons.find(addon => addon.name === 'ember-cli-babel'); // create the babel options to use elsewhere based on the config above -let options = babelAddon.buildBabelOptions(config) +let options = babelAddon.buildBabelOptions('babel', config) // now you can pass these options off to babel or broccoli-babel-transpiler require('babel-core').transform('something', options); diff --git a/index.js b/index.js index 992d341d..92c6785d 100644 --- a/index.js +++ b/index.js @@ -44,12 +44,25 @@ module.exports = { } }, - buildBabelOptions(_config) { + buildBabelOptions(configOrType, _config) { + let resultType; + + if (typeof configOrType !== 'string') { + _config = configOrType; + resultType = 'broccoli'; + } else if (configOrType === 'broccoli') { + resultType = 'broccoli'; + } else if (configOrType === 'babel') { + resultType = 'babel'; + } + let config = _config || this._getAddonOptions(); const customAddonConfig = config['ember-cli-babel']; const shouldUseBabelConfigFile = customAddonConfig && customAddonConfig.useBabelConfig; + let options; + if (shouldUseBabelConfigFile) { let babelConfig = babel.loadPartialConfig({ root: this.parent.root, @@ -69,9 +82,16 @@ module.exports = { // If the babel config file is found, then pass the path into the options for the transpiler // parse and leverage the same. - return { configFile: babelConfig.config }; + options = { configFile: babelConfig.config }; + } else { + options = getBabelOptions(config, this); + } + + if (resultType === 'babel') { + return options; } else { - return getBabelOptions(config, this); + // legacy codepath + return Object.assign({}, this._buildBroccoliBabelTranspilerOptions(config), options); } }, @@ -128,7 +148,7 @@ module.exports = { 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 options = Object.assign({}, this._buildBroccoliBabelTranspilerOptions(config), this.buildBabelOptions('babel', config)); let output; const customAddonConfig = config['ember-cli-babel']; diff --git a/node-tests/addon-test.js b/node-tests/addon-test.js index d3d203be..033fefbf 100644 --- a/node-tests/addon-test.js +++ b/node-tests/addon-test.js @@ -1137,7 +1137,7 @@ describe('ember-cli-babel', function() { }); }); - describe('buildBabelOptions', function() { + describe('_buildBroccoliBabelTranspilerOptions', function() { this.timeout(0); it('disables reading `.babelrc`', function() { @@ -1195,6 +1195,51 @@ describe('ember-cli-babel', function() { expect(result.babelrc).to.be.false; }); + }); + + describe('buildBabelOptions', function() { + this.timeout(0); + + it('returns broccoli-babel-transpiler options by default', function() { + let result = this.addon.buildBabelOptions(); + + expect(result.moduleIds).to.be.true; + expect(result.annotation).to.be; + expect(result.babelrc).to.be.false; + expect(result.configFile).to.be.false; + }); + + it('returns broccoli-babel-transpiler options when asked for', function() { + let result = this.addon.buildBabelOptions('broccoli'); + + expect(result.moduleIds).to.be.true; + expect(result.annotation).to.be; + expect(result.babelrc).to.be.false; + expect(result.configFile).to.be.false; + }); + + it('returns broccoli-babel-transpiler options with customizations when provided', function() { + let result = this.addon.buildBabelOptions('broccoli', { + 'ember-cli-babel': { + annotation: 'hello!!!', + } + }); + + expect(result.annotation).to.equal('hello!!!'); + expect(result.moduleIds).to.be.true; + expect(result.annotation).to.be; + expect(result.babelrc).to.be.false; + expect(result.configFile).to.be.false; + }); + + it('returns babel options when asked for', function() { + let result = this.addon.buildBabelOptions('babel'); + + expect('moduleIds' in result).to.be.false; + expect('annotation' in result).to.be.false; + expect('babelrc' in result).to.be.false; + expect('configFile' in result).to.be.false; + }); it('does not include all provided options', function() { let babelOptions = { blah: true }; @@ -1202,7 +1247,7 @@ describe('ember-cli-babel', function() { babel: babelOptions }; - let result = this.addon.buildBabelOptions(options); + let result = this.addon.buildBabelOptions('babel', options); expect(result.blah).to.be.undefined; });