Skip to content

Commit

Permalink
Do not transform imports to Ember Global on certain version
Browse files Browse the repository at this point in the history
  • Loading branch information
patocallaghan committed Mar 16, 2021
1 parent 0b4cef2 commit a6c3862
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 13 deletions.
13 changes: 7 additions & 6 deletions lib/babel-options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ function _getDebugMacroPlugins(config) {
];
}

function _emberVersionRequiresModulesAPIPolyfill() {
// once a version of Ember ships with the
// emberjs/rfcs#176 modules natively this will
// be updated to detect that and return false
return true;
function _emberVersionRequiresModulesAPIPolyfill(parent) {
let checker = new VersionChecker(parent).for("ember-source", "npm");
if (!checker.exists()) {
return true;
}
return checker.lt("3.27.0-alpha.1");
}

function _emberDataVersionRequiresPackagesPolyfill(project) {
Expand All @@ -125,7 +126,7 @@ function _getEmberModulesAPIPolyfill(config, parent, project) {
return;
}

if (_emberVersionRequiresModulesAPIPolyfill()) {
if (_emberVersionRequiresModulesAPIPolyfill(parent)) {
const ignore = _getEmberModulesAPIIgnore(parent, project);

return [
Expand Down
17 changes: 10 additions & 7 deletions lib/ember-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,22 @@ function _getDebugMacroPlugins() {
],
];
}
function _emberVersionRequiresModulesAPIPolyfill() {
// once a version of Ember ships with the
// emberjs/rfcs#176 modules natively this will
// be updated to detect that and return false
return true;
function _emberVersionRequiresModulesAPIPolyfill(appRoot) {
let packagePath = resolvePackagePath("ember-source", appRoot);
if (packagePath === null) {
return true;
}

let pkg = require(packagePath);
return pkg && semver.lt(pkg.version, "3.27.0-alpha.1");
}

function _getEmberModulesAPIPolyfill(appRoot, config) {
if (config.disableEmberModulesAPIPolyfill) {
return;
}

if (_emberVersionRequiresModulesAPIPolyfill()) {
if (_emberVersionRequiresModulesAPIPolyfill(appRoot)) {
const ignore = _getEmberModulesAPIIgnore(appRoot, config);

return [
Expand Down Expand Up @@ -128,7 +131,7 @@ function _getEmberDataPackagesPolyfill(appRoot, config) {
function _getModuleResolutionPlugins(config) {
if (!config.disableModuleResolution) {
const resolvePath = require("../lib/relative-module-paths")
.resolveRelativeModulePath;
.resolveRelativeModulePath;
return [
[require.resolve("babel-plugin-module-resolver"), { resolvePath }],
[
Expand Down
106 changes: 106 additions & 0 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,112 @@ describe('ember-cli-babel', function() {
}));
});

describe("Opting out of the ember modules API polyfill", function () {
let dependencies;

beforeEach(function () {
dependencies = {};
let project = {
root: input.path(),
emberCLIVersion: () => "2.16.2",
dependencies() {
return dependencies;
},
addons: [],
targets: {
browsers: ["ie 11"],
},
};

this.addon = new Addon({
project,
parent: project,
ui: this.ui,
});

project.addons.push(this.addon);
});

function buildEmberSourceFixture(version) {
return {
node_modules: {
"ember-source": {
"package.json": JSON.stringify({ name: "ember-source", version }),
"index.js": "module.exports = {};",
},
},
};
}

it(
"should replace imports with Ember Globals",
co.wrap(function* () {
const PRE_GLOBAL_RESOLVER_DEPRECATION_VERSION = "3.26.0";
dependencies[
"ember-source"
] = PRE_GLOBAL_RESOLVER_DEPRECATION_VERSION;
input.write(
buildEmberSourceFixture(PRE_GLOBAL_RESOLVER_DEPRECATION_VERSION)
);
input.write({
"foo.js": `import Component from '@ember/component'; Component.extend()`,
"app.js": `import Application from '@ember/application'; Application.extend()`,
});

subject = this.addon.transpileTree(input.path());
output = createBuilder(subject);

yield output.build();

expect(output.read()).to.deep.equal({
"foo.js": `define("foo", [], function () {\n "use strict";\n\n Ember.Component.extend();\n});`,
"app.js": `define("app", [], function () {\n "use strict";\n\n Ember.Application.extend();\n});`,
node_modules: {
"ember-source": {
"index.js":
'define("node_modules/ember-source/index", [], function () {\n "use strict";\n\n module.exports = {};\n});',
"package.json": '{"name":"ember-source","version":"3.26.0"}',
},
},
});
})
);

it(
"should not replace the imports with Ember Globals when using an ember-source version that supports it",
co.wrap(function* () {
const POST_GLOBAL_RESOLVER_DEPRECATION_VERSION = "3.27.0";
dependencies[
"ember-source"
] = POST_GLOBAL_RESOLVER_DEPRECATION_VERSION;
input.write(
buildEmberSourceFixture(POST_GLOBAL_RESOLVER_DEPRECATION_VERSION)
);
input.write({
"foo.js": `import Component from '@ember/component'; Component.extend()`,
"app.js": `import Application from '@ember/application'; Application.extend()`,
});

subject = this.addon.transpileTree(input.path());
output = createBuilder(subject);

yield output.build();

expect(output.read()).to.deep.equal({
"foo.js": `define("foo", ["@ember/component"], function (_component) {\n "use strict";\n\n _component.default.extend();\n});`,
"app.js": `define("app", ["@ember/application"], function (_application) {\n "use strict";\n\n _application.default.extend();\n});`,
node_modules: {
"ember-source": {
"index.js":
'define("node_modules/ember-source/index", [], function () {\n "use strict";\n\n module.exports = {};\n});',
"package.json": '{"name":"ember-source","version":"3.27.0"}',
},
},
});
})
);
});

describe('debug macros', function() {
it("can opt-out via ember-cli-babel.disableDebugTooling", co.wrap(function* () {
process.env.EMBER_ENV = 'development';
Expand Down

0 comments on commit a6c3862

Please sign in to comment.