Skip to content

Commit

Permalink
Merge pull request #407 from babel/more-compileModules-combinations
Browse files Browse the repository at this point in the history
Fix combination usages of `compileModules` along with other flags.
  • Loading branch information
rwjblue committed May 18, 2021
2 parents b704321 + 89245f9 commit 085c408
Show file tree
Hide file tree
Showing 2 changed files with 320 additions and 8 deletions.
45 changes: 37 additions & 8 deletions lib/babel-options-util.js
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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",
};
}

Expand Down Expand Up @@ -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 [
Expand Down
283 changes: 283 additions & 0 deletions node-tests/addon-test.js
Expand Up @@ -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';

Expand Down

0 comments on commit 085c408

Please sign in to comment.