From 89245f9562fff884def54c3120cf168f756836d0 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Tue, 18 May 2021 16:05:32 -0400 Subject: [PATCH] Fix combination usages of `compileModules` along with other flags. The fix that landed in e20c0e45 was not quite correct. Specifically, it did not address the scenario where you explicitly set `compileModules: false` but also set other flags that relate to modules behaviors (e.g. `disableDebugTooling` or `disableEmberModulesAPIPolyfill`). This adds a number of additional tests (which emulate "real world" usage from Embroider) and ensure they pass. Co-authored-by: Edward Faulkner --- lib/babel-options-util.js | 45 ++++-- node-tests/addon-test.js | 283 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 320 insertions(+), 8 deletions(-) diff --git a/lib/babel-options-util.js b/lib/babel-options-util.js index 6123a407..ff49d6ef 100644 --- a/lib/babel-options-util.js +++ b/lib/babel-options-util.js @@ -50,7 +50,8 @@ function _shouldHighlightCode(parent) { function _getDebugMacroPlugins(config, project) { let addonOptions = config["ember-cli-babel"] || {}; - if (addonOptions.disableDebugTooling) { + let disableDebugTooling = addonOptions.disableDebugTooling; + if (disableDebugTooling) { return; } @@ -85,21 +86,33 @@ function _getDebugMacroPlugins(config, project) { }, }; - // we have to use the global form when not compiling modules, because it is often used - // in the context of an `app.import` where there is no wrapped in an AMD module - if (addonOptions.compileModules === false || _emberVersionRequiresModulesAPIPolyfill(project)) { + let useModulesVersion; + + if (_emberVersionRequiresModulesAPIPolyfill(project)) { + useModulesVersion = false; + } else if (addonOptions.compileModules === false) { + // we have to use the global form when not compiling modules, because it is often used + // in the context of an `app.import` where there is no wrapped in an AMD module + // + // However, you can opt out of this behavior by explicitly specifying `disableDebugTooling` + useModulesVersion = disableDebugTooling === false; + } else { + useModulesVersion = true; + } + + if (useModulesVersion) { emberDebugOptions.externalizeHelpers = { - global: "Ember", + module: "@ember/debug", }; emberApplicationDeprecationsOptions.externalizeHelpers = { - global: "Ember", + module: "@ember/application/deprecations", }; } else { emberDebugOptions.externalizeHelpers = { - module: "@ember/debug", + global: "Ember", }; emberApplicationDeprecationsOptions.externalizeHelpers = { - module: "@ember/application/deprecations", + global: "Ember", }; } @@ -147,7 +160,23 @@ function _getEmberModulesAPIPolyfill(config, parent, project) { return; } + let useModulesVersion; + if (_emberVersionRequiresModulesAPIPolyfill(project)) { + useModulesVersion = false; + } else if (addonOptions.compileModules === false) { + // we have to use the global form when not compiling modules, because it is often used + // in the context of an `app.import` where there is no wrapped in an AMD module + // + // However, you can opt out of this behavior by explicitly specifying `disableEmberModulesAPIPolyfill` + useModulesVersion = addonOptions.disableEmberModulesAPIPolyfill === false; + } else { + useModulesVersion = true; + } + + // we have to use the global form when not compiling modules, because it is often used + // in the context of an `app.import` where there is no wrapped in an AMD module + if (!useModulesVersion) { const ignore = _getEmberModulesAPIIgnore(parent, project); return [ diff --git a/node-tests/addon-test.js b/node-tests/addon-test.js index 2a4a7d5b..97d28d98 100644 --- a/node-tests/addon-test.js +++ b/node-tests/addon-test.js @@ -458,6 +458,289 @@ describe('ember-cli-babel', function() { }); })); + it("when transpiling with compileModules: false it should use Ember global for previously 'fake' imports even on Ember 3.27+", co.wrap(function* () { + process.env.EMBER_ENV = 'development'; + + dependencies[ + "ember-source" + ] = POST_EMBER_MODULE_IMPORTS_VERSION; + input.write( + buildEmberSourceFixture(POST_EMBER_MODULE_IMPORTS_VERSION) + ); + + input.write({ + app: { + "foo.js": stripIndent` + import Component from '@ember/component'; + + export default class extends Component {} + `, + }, + }); + + this.addon.project.targets = { + browsers: ['last 2 chrome versions'] + }; + + subject = this.addon.transpileTree(input.path('app'), { + 'ember-cli-babel': { + compileModules: false, + } + }); + output = createBuilder(subject); + + yield output.build(); + + expect( + output.read() + ).to.deep.equal({ + "foo.js": `export default class extends Ember.Component {}`, + }); + })); + + it("when transpiling with compileModules: false, disableEmberModulesAPIPolyfill: true it should not use Ember global for previously 'fake' imports", co.wrap(function* () { + process.env.EMBER_ENV = 'development'; + + dependencies[ + "ember-source" + ] = POST_EMBER_MODULE_IMPORTS_VERSION; + input.write( + buildEmberSourceFixture(POST_EMBER_MODULE_IMPORTS_VERSION) + ); + + input.write({ + app: { + "foo.js": stripIndent` + import Component from '@ember/component'; + + export default class extends Component {} + `, + }, + }); + + this.addon.project.targets = { + browsers: ['last 2 chrome versions'] + }; + + subject = this.addon.transpileTree(input.path('app'), { + 'ember-cli-babel': { + compileModules: false, + disableEmberModulesAPIPolyfill: true, + } + }); + output = createBuilder(subject); + + yield output.build(); + + expect( + output.read() + ).to.deep.equal({ + "foo.js": `import Component from '@ember/component';\nexport default class extends Component {}`, + }); + })); + + it("when transpiling with compileModules: false, disableEmberModulesAPIPolyfill: false it should use global for Ember < 3.27", co.wrap(function* () { + process.env.EMBER_ENV = 'development'; + + dependencies[ + "ember-source" + ] = PRE_EMBER_MODULE_IMPORTS_VERSION; + input.write( + buildEmberSourceFixture(PRE_EMBER_MODULE_IMPORTS_VERSION) + ); + + input.write({ + app: { + "foo.js": stripIndent` + import Component from '@ember/component'; + + export default class extends Component {} + `, + }, + }); + + this.addon.project.targets = { + browsers: ['last 2 chrome versions'] + }; + + subject = this.addon.transpileTree(input.path('app'), { + 'ember-cli-babel': { + compileModules: false, + disableEmberModulesAPIPolyfill: false, + } + }); + output = createBuilder(subject); + + yield output.build(); + + expect( + output.read() + ).to.deep.equal({ + "foo.js": `export default class extends Ember.Component {}`, + }); + })); + + it("when transpiling with compileModules: false, disableEmberModulesAPIPolyfill: false it should use global for Ember > 3.27", co.wrap(function* () { + process.env.EMBER_ENV = 'development'; + + dependencies[ + "ember-source" + ] = POST_EMBER_MODULE_IMPORTS_VERSION; + input.write( + buildEmberSourceFixture(POST_EMBER_MODULE_IMPORTS_VERSION) + ); + + input.write({ + app: { + "foo.js": stripIndent` + import Component from '@ember/component'; + + export default class extends Component {} + `, + }, + }); + + this.addon.project.targets = { + browsers: ['last 2 chrome versions'] + }; + + subject = this.addon.transpileTree(input.path('app'), { + 'ember-cli-babel': { + compileModules: false, + disableEmberModulesAPIPolyfill: false, + } + }); + output = createBuilder(subject); + + yield output.build(); + + expect( + output.read() + ).to.deep.equal({ + "foo.js": `import Component from '@ember/component';\nexport default class extends Component {}`, + }); + })); + + it("when transpiling with compileModules: false, disableDebugTooling: false it should use modules for debug tooling", co.wrap(function* () { + process.env.EMBER_ENV = 'development'; + + dependencies[ + "ember-source" + ] = POST_EMBER_MODULE_IMPORTS_VERSION; + input.write( + buildEmberSourceFixture(POST_EMBER_MODULE_IMPORTS_VERSION) + ); + + input.write({ + app: { + "foo.js": stripIndent` + import { assert } from '@ember/debug'; + assert('stuff here', isNotBad()); + `, + "bar.js": stripIndent` + import { deprecate } from '@ember/debug'; + deprecate( + 'foo bar baz', + false, + { + id: 'some-id', + until: '1.0.0', + } + ); + `, + "baz.js": stripIndent` + import { deprecate } from '@ember/application/deprecations'; + deprecate( + 'foo bar baz', + false, + { + id: 'some-id', + until: '1.0.0', + } + ); + `, + }, + }); + + subject = this.addon.transpileTree(input.path('app'), { + 'ember-cli-babel': { + compileModules: false, + disableDebugTooling: false, + } + }); + output = createBuilder(subject); + + yield output.build(); + + expect( + output.read() + ).to.deep.equal({ + "bar.js": `import { deprecate } from '@ember/debug';\n(true && !(false) && deprecate('foo bar baz', false, {\n id: 'some-id',\n until: '1.0.0'\n}));`, + "baz.js": `import { deprecate } from '@ember/application/deprecations';\n(true && !(false) && deprecate('foo bar baz', false, {\n id: 'some-id',\n until: '1.0.0'\n}));`, + "foo.js": `import { assert } from '@ember/debug';\n(true && !(isNotBad()) && assert('stuff here', isNotBad()));`, + }); + })); + + it("when transpiling with compileModules: false, disableDebugTooling: true it should not use Ember global for debug tooling", co.wrap(function* () { + process.env.EMBER_ENV = 'development'; + + dependencies[ + "ember-source" + ] = POST_EMBER_MODULE_IMPORTS_VERSION; + input.write( + buildEmberSourceFixture(POST_EMBER_MODULE_IMPORTS_VERSION) + ); + + input.write({ + app: { + "foo.js": stripIndent` + import { assert } from '@ember/debug'; + assert('stuff here', isNotBad()); + `, + "bar.js": stripIndent` + import { deprecate } from '@ember/debug'; + deprecate( + 'foo bar baz', + false, + { + id: 'some-id', + until: '1.0.0', + } + ); + `, + "baz.js": stripIndent` + import { deprecate } from '@ember/application/deprecations'; + deprecate( + 'foo bar baz', + false, + { + id: 'some-id', + until: '1.0.0', + } + ); + `, + }, + }); + + subject = this.addon.transpileTree(input.path('app'), { + 'ember-cli-babel': { + compileModules: false, + disableDebugTooling: true, + } + }); + output = createBuilder(subject); + + yield output.build(); + + expect( + output.read() + ).to.deep.equal({ + "bar.js": `import { deprecate } from '@ember/debug';\ndeprecate('foo bar baz', false, {\n id: 'some-id',\n until: '1.0.0'\n});`, + "baz.js": `import { deprecate } from '@ember/application/deprecations';\ndeprecate('foo bar baz', false, {\n id: 'some-id',\n until: '1.0.0'\n});`, + "foo.js": `import { assert } from '@ember/debug';\nassert('stuff here', isNotBad());`, + }); + })); + it("when transpiling with compileModules: false, it should use Ember global even on Ember 3.27+", co.wrap(function* () { process.env.EMBER_ENV = 'development';