diff --git a/README.md b/README.md index 2ae6b7a6..aa28cc8c 100755 --- a/README.md +++ b/README.md @@ -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 }), ], }; }; @@ -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 } ``` diff --git a/index.js b/index.js index 8d712ca6..0a32e273 100644 --- a/index.js +++ b/index.js @@ -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); @@ -84,8 +86,7 @@ module.exports = { sourceMaps, throwUnlessParallelizable, filterExtensions: _getExtensions(config, this.parent), - plugins: [], - presets: [] + plugins: [] }; if (shouldCompileModules) { diff --git a/lib/ember-plugins.js b/lib/ember-plugins.js index 8037de37..c54a5fa5 100644 --- a/lib/ember-plugins.js +++ b/lib/ember-plugins.js @@ -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; @@ -50,13 +53,13 @@ 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 }], @@ -64,13 +67,35 @@ function _getEmberModulesAPIPolyfill(config) { } } -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", @@ -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 }], [ @@ -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; diff --git a/package.json b/package.json index a65763de..ed9edbb2 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/yarn.lock b/yarn.lock index 1f6901ba..0fe555de 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5572,6 +5572,13 @@ is-ci@2.0.0, 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" @@ -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" @@ -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" + responselike@1.0.2, responselike@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"