This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a Babel plugin which renames `service` imports to `inject` if the current Ember version doesn't support this import yet. This is a polyfill for RFC 752. More information can be found in the RFC PR: emberjs/rfcs#752
- Loading branch information
Showing
12 changed files
with
3,263 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,28 @@ | ||
'use strict'; | ||
|
||
const { hasPlugin, addPlugin } = require('ember-cli-babel-plugin-helpers'); | ||
const { resolve } = require('path'); | ||
const VersionChecker = require('ember-cli-version-checker'); | ||
|
||
module.exports = { | ||
name: require('./package').name, | ||
|
||
included(parent) { | ||
this._super.included.apply(this, arguments); | ||
|
||
if (this.shouldPolyfillServiceImport()) { | ||
let pluginPath = resolve(__dirname, './lib/plugin-rename-service-import'); | ||
|
||
if (!hasPlugin(parent, pluginPath)) { | ||
addPlugin(parent, pluginPath); | ||
} | ||
} | ||
}, | ||
|
||
shouldPolyfillServiceImport() { | ||
let checker = new VersionChecker(this.project); | ||
|
||
// https://github.com/emberjs/ember.js/pull/19776/commits/12b89c42d1987fd782d06f1f170eb7ce25612558 | ||
return checker.for('ember-source').lt('v4.1.0-alpha.5'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
module.exports = function () { | ||
return { | ||
name: 'plugin-rename-service-import', | ||
visitor: { | ||
ImportSpecifier(path) { | ||
let { node: importSpecifier, parent: importDeclaration } = path; | ||
|
||
if ( | ||
isEmberServiceModuleImport(importDeclaration) && | ||
isNewImportName(importSpecifier) | ||
) { | ||
renameServiceImportToInject(importSpecifier); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
function isEmberServiceModuleImport(importDeclaration) { | ||
return importDeclaration.source.value === '@ember/service'; | ||
} | ||
|
||
function isNewImportName(importSpecifier) { | ||
return importSpecifier.imported.name === 'service'; | ||
} | ||
|
||
function renameServiceImportToInject(importSpecifier) { | ||
importSpecifier.imported.name = 'inject'; | ||
} |
Oops, something went wrong.