Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
suchitadoshi1987 committed Dec 9, 2020
1 parent a5dfa5a commit 09aaf01
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 20 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,8 @@ module.exports = function (api) {
useESModules: true,
},
],
[require.resolve("@babel/plugin-proposal-decorators"), { legacy: true }],
[require.resolve("@babel/plugin-proposal-class-properties")],

// this is where all the ember required plugins would reside
...emberPlugins({ ...options }),
...emberPlugins(__dirname, { ...options }),
],
};
};
Expand All @@ -351,15 +348,16 @@ module.exports = function (api) {
#### Ember Plugins

Ember Plugins is a helper function that returns a list of plugins that are required for transpiling Ember correctly. You can import this helper function and add it to your existing `babel.config` file.

The first argument is **required** which is the path to the root of your project (generally `__dirname`).
**Config options:**

```json
```js
{
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
}
```
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ let count = 0;
module.exports = {
name: 'ember-cli-babel',
configKey: 'ember-cli-babel',
emberPlugins,
// Note: This is not used internally for this addon, this is added for users to import this function for getting the ember specific
// babel plugins. Eg: adding ember specific babel plugins in their babel.config.js.
buildEmberPlugins: emberPlugins,

init() {
this._super.init && this._super.init.apply(this, arguments);
Expand Down Expand Up @@ -84,8 +86,7 @@ module.exports = {
sourceMaps,
throwUnlessParallelizable,
filterExtensions: _getExtensions(config, this.parent),
plugins: [],
presets: []
plugins: []
};

if (shouldCompileModules) {
Expand Down
60 changes: 49 additions & 11 deletions lib/ember-plugins.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const semver = require("semver");
const resolvePackagePath = require("resolve-package-path");

function _getDebugMacroPlugins() {
const isProduction = process.env.EMBER_ENV === "production";
const isDebug = !isProduction;
Expand Down Expand Up @@ -50,27 +53,49 @@ function _emberVersionRequiresModulesAPIPolyfill() {
return true;
}

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

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

return [
[require.resolve("babel-plugin-ember-modules-api-polyfill"), { ignore }],
];
}
}

function _getEmberModulesAPIIgnore(config) {
function _shouldIgnoreEmberString(appRoot) {
return resolvePackagePath("@ember/string", appRoot) !== null;
}

function _shouldIgnoreJQuery(appRoot) {
let packagePath = resolvePackagePath("@ember/jquery", appRoot);
if (packagePath === null) {
return true;
}
let pkg = require(packagePath);
return pkg && semver.gt(pkg.version, "0.6.0");
}

function _emberDataVersionRequiresPackagesPolyfill(appRoot) {
let packagePath = resolvePackagePath("ember-data", appRoot);
if (packagePath === null) {
return false;
}
let pkg = require(packagePath);
return pkg && semver.lt(pkg.version, "3.12.0-alpha.0");
}

function _getEmberModulesAPIIgnore(appRoot, config) {
const ignore = {
"@ember/debug": ["assert", "deprecate", "warn"],
"@ember/application/deprecations": ["deprecate"],
};

if (config.shouldIgnoreEmberString) {
if (config.shouldIgnoreEmberString || _shouldIgnoreEmberString(appRoot)) {
ignore["@ember/string"] = [
"fmt",
"loc",
Expand All @@ -86,23 +111,24 @@ function _getEmberModulesAPIIgnore(config) {
"getString",
];
}
if (config.shouldIgnoreJQuery) {
if (config.shouldIgnoreJQuery || _shouldIgnoreJQuery(appRoot)) {
ignore["jquery"] = ["default"];
}

return ignore;
}

function _getEmberDataPackagesPolyfill(config) {
function _getEmberDataPackagesPolyfill(appRoot, config) {
if (config.emberDataVersionRequiresPackagesPolyfill) {
return [[require.resolve("babel-plugin-ember-data-packages-polyfill")]];
}
return _emberDataVersionRequiresPackagesPolyfill(appRoot);
}

function _getModuleResolutionPlugins(config) {
if (!config.disableModuleResolution) {
const resolvePath = require("../lib/relative-module-paths")
.resolveRelativeModulePath;
.resolveRelativeModulePath;
return [
[require.resolve("babel-plugin-module-resolver"), { resolvePath }],
[
Expand All @@ -113,17 +139,29 @@ function _getModuleResolutionPlugins(config) {
}
}

module.exports = function (config = {}) {
function _getProposalDecoratorsAndClassPlugins(config) {
if (!config.shouldIgnoreDecoratorAndClassPlugins) {
return [
["@babel/plugin-proposal-decorators", { legacy: true }],
["@babel/plugin-proposal-class-properties"],
];
}
}

module.exports = function (appRoot, config = {}) {
return []
.concat(
_getDebugMacroPlugins(),
_getEmberModulesAPIPolyfill(config),
_getEmberDataPackagesPolyfill(config),
_getProposalDecoratorsAndClassPlugins(config),
_getDebugMacroPlugins(appRoot),
_getEmberModulesAPIPolyfill(appRoot, config),
_getEmberDataPackagesPolyfill(appRoot, config),
_getModuleResolutionPlugins(config)
)
.filter(Boolean);
};

module.exports.getDebugMacroPlugins = _getDebugMacroPlugins;
module.exports.getEmberModulesAPIPolyfill = _getEmberModulesAPIPolyfill;
module.exports.getEmberDataPackagesPolyfill = _getEmberDataPackagesPolyfill;
module.exports.getModuleResolutionPlugins = _getModuleResolutionPlugins;
module.exports.getProposalDecoratorsAndClassPlugins = _getProposalDecoratorsAndClassPlugins;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"ember-cli-version-checker": "^4.1.0",
"ensure-posix-path": "^1.0.2",
"fixturify-project": "^1.10.0",
"resolve-package-path": "^3.1.0",
"rimraf": "^3.0.1",
"semver": "^5.5.0"
},
Expand Down
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5572,6 +5572,13 @@ [email protected], is-ci@^2.0.0:
dependencies:
ci-info "^2.0.0"

is-core-module@^2.1.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.2.0.tgz#97037ef3d52224d85163f5597b2b63d9afed981a"
integrity sha512-XRAfAdyyY5F5cOXn7hYQDqh2Xmii+DEfIcQGxK/uNwMHhIkPWO0g8msXcbzLe+MpGoR951MlqM/2iIlU4vKDdQ==
dependencies:
has "^1.0.3"

is-data-descriptor@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz#0b5ee648388e2c860282e793f1856fec3f301b56"
Expand Down Expand Up @@ -8236,6 +8243,14 @@ resolve-package-path@^2.0.0:
path-root "^0.1.1"
resolve "^1.13.1"

resolve-package-path@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/resolve-package-path/-/resolve-package-path-3.1.0.tgz#35faaa5d54a9c7dd481eb7c4b2a44410c9c763d8"
integrity sha512-2oC2EjWbMJwvSN6Z7DbDfJMnD8MYEouaLn5eIX0j8XwPsYCVIyY9bbnX88YHVkbr8XHqvZrYbxaLPibfTYKZMA==
dependencies:
path-root "^0.1.1"
resolve "^1.17.0"

resolve-url@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
Expand All @@ -8255,6 +8270,14 @@ resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.3.
dependencies:
path-parse "^1.0.6"

resolve@^1.17.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.19.0.tgz#1af5bf630409734a067cae29318aac7fa29a267c"
integrity sha512-rArEXAgsBG4UgRGcynxWIWKFvh/XZCcS8UJdHhwy91zwAvCZIbcs+vAbflgBnNjYMs/i/i+/Ux6IZhML1yPvxg==
dependencies:
is-core-module "^2.1.0"
path-parse "^1.0.6"

[email protected], responselike@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"
Expand Down

0 comments on commit 09aaf01

Please sign in to comment.