babel-plugin-angularjs-digest-await
Advanced tools
Comparing version 1.0.0 to 2.0.0
68
index.js
@@ -5,28 +5,5 @@ module.exports = function(babel) { | ||
var defaultOptions = { | ||
helperFunctionName: '$$await', | ||
$rootScope: 'angular.element(document.body).injector().get("$rootScope")' | ||
helperFunctionName: '$$await' | ||
}; | ||
// Key that marks that a $$await helper function has been | ||
// added to the topmost scope already. | ||
var HELPERS_DECLARED_KEY = 'angularjsDigestAwait/helpers-declared'; | ||
// Template for the $$await helper function. | ||
// Function name and the expression to get $rootScope are configurable. | ||
/* eslint-disable */ | ||
var buildHelperFunction = babel.template((function HELPER_FUNCTION_NAME(v) { | ||
var $rootScope; | ||
try { | ||
$rootScope = HELPER_FUNCTION_NAME.$rootScope || | ||
(HELPER_FUNCTION_NAME.$rootScope = ROOT_SCOPE); | ||
} catch(e) {} | ||
$rootScope && $rootScope.$$phase == null && $rootScope.$applyAsync(); | ||
return v; | ||
}).toString()); | ||
/* eslint-enable */ | ||
function getOption(opts, key) { | ||
return opts && opts.hasOwnProperty(key) ? opts[key] : defaultOptions[key]; | ||
} | ||
// Checks if an AwaitExpression path has already been | ||
@@ -44,12 +21,2 @@ // wrapped by this plugin. | ||
function getTopScope(path) { | ||
var p = path, parent; | ||
while((parent = p.parentPath)) p = parent; | ||
return p; | ||
} | ||
// If the default template for $rootScope is used, | ||
// we can cache the result of babel.template() call. | ||
var cachedRootScopeTemplate; | ||
return { | ||
@@ -59,36 +26,5 @@ visitor: { | ||
var opts = state.opts; | ||
var helperFunctionName = getOption(opts, 'helperFunctionName'); | ||
var helperFunctionName = opts.helperFunctionName || defaultOptions.helperFunctionName; | ||
if (noWrappingNeeded(path, helperFunctionName)) return; | ||
// Generate $$await helper function to the top of the file. | ||
var topScope = getTopScope(path); | ||
var helpersDeclared = topScope.getData(HELPERS_DECLARED_KEY); | ||
if (!helpersDeclared) { | ||
var rootScopeTemplate = getOption(opts, '$rootScope'); | ||
var rootScopeExpression; | ||
try { | ||
if (rootScopeTemplate === defaultOptions.$rootScope) { | ||
if (!cachedRootScopeTemplate) { | ||
cachedRootScopeTemplate = babel.template(defaultOptions.$rootScope); | ||
} | ||
rootScopeExpression = cachedRootScopeTemplate(); | ||
} else { | ||
rootScopeExpression = babel.template(rootScopeTemplate)(); | ||
} | ||
} catch (err) { | ||
var errorMsg = 'Building $rootScope expression ' | ||
+ rootScopeTemplate | ||
+ ' failed with error:\n' | ||
+ err; | ||
throw new Error(errorMsg); | ||
} | ||
var ast = buildHelperFunction({ | ||
ROOT_SCOPE: rootScopeExpression, | ||
HELPER_FUNCTION_NAME: t.identifier(helperFunctionName) | ||
}); | ||
topScope.unshiftContainer('body', ast); | ||
topScope.setData(HELPERS_DECLARED_KEY, true); | ||
} | ||
var newPath = t.callExpression( | ||
@@ -95,0 +31,0 @@ t.identifier(helperFunctionName), |
{ | ||
"name": "babel-plugin-angularjs-digest-await", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -28,13 +28,2 @@ # babel-plugin-angularjs-digest-await | ||
```javascript | ||
function $$await(v) { | ||
var $rootScope; | ||
try { | ||
$rootScope = $$await.$rootScope || ($$await.$rootScope = angular.element(document.body).injector().get("$rootScope")); | ||
} catch (e) {} | ||
$rootScope && $rootScope.$$phase == null && $rootScope.$applyAsync(); | ||
return v; | ||
} | ||
async function foo() { | ||
@@ -46,10 +35,19 @@ const data = $$await((await getData())); | ||
### Available plugin options | ||
## Installation | ||
``` | ||
npm i -D babel-plugin-angularjs-digest-await | ||
``` | ||
.babelrc | ||
```json | ||
{ "plugins": ["angularjs-digest-await"] } | ||
``` | ||
Include `node_modules/babel-plugin-angularjs-digest-await/$$await.js` to your build | ||
using whatever method you use to bundle source files (Webpack, Gulp, etc.) or roll | ||
your own global `$$await` helper function if the provided default solution doesn't | ||
work for your AngularJS app. | ||
## Available plugin options | ||
* `helperFunctionName`. Name of the helper function that gets added to | ||
the top of the file. | ||
Default: `$$await`. | ||
* `$rootScope`. JavaScript expression to get `$rootScope`, which is needed | ||
to invoke the `$digest` cycle. The provided value here will be evaluated | ||
as code when the build output is run. | ||
Default: `angular.element(document.body).injector().get("$rootScope")`. | ||
Default: `$$await`. |
6
4652
45
51