Skip to content

Commit 7417521

Browse files
committed
Update to allow for much more specific rules for including the nodent
runtime in compiled files. See README.md for more details.
1 parent 5149a1a commit 7417521

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ With options:
4646
"compiler": {
4747
"promises": true,
4848
"generators": false
49-
}
49+
},
50+
"runtimePattern":null
5051
}]
5152
]
5253
}
@@ -73,10 +74,27 @@ but since much of the parsing is done by Babel some are unused.
7374
compiler:{
7475
promises:true, // Use nodent's "Promises" mode. Set to false if your execution environment does not support Promises.
7576
generators:false // Transform into 'Generators'.
76-
}
77+
},
78+
runtimePattern:null // See below
7779

7880
For more information on the compiler options, see [ES7 and Promises](https://github.com/matatbread/nodent#es7-and-promises) in the nodent documentation.
7981

82+
__runtimePattern__
83+
By default, fast-async will put the nodent runtime into every file it compiles. If your project is made up of more than one file, the constant
84+
redefinition of the runtime is a waste of time and space. You can specify that you want the runtime in particular file(s) by setting the 'runtimePattern' to a regular expression (in quotes). Only files that match the regular expression will have the runtime defined (which is global, so you only need it once). For example:
85+
86+
"babel": {
87+
"plugins": [
88+
"syntax-async-functions",
89+
["fast-async",{
90+
"runtimePattern":"test-input\\.js"
91+
}]
92+
]
93+
}
94+
95+
Alternatively, if you set runtimePattern to `"directive"`, the statement `"use runtime-nodent";` will be replaced with the runtime during compilation.
96+
97+
8098
Useful Links
8199
------------
82100

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "fast-async",
3-
"version": "6.0.19",
3+
"version": "6.0.20",
44
"dependencies": {
5-
"nodent": "^2.5.0"
5+
"nodent": "^2.5.1"
66
},
77
"description": "fast-async/await transformer Babel plugin",
88
"main": "plugin.js",

plugin.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,27 @@ module.exports = function (types) {
5050

5151
return ast ;
5252
}
53-
pr.ast.body.unshift(getRuntime('Function.prototype.$asyncbind',Function.prototype.$asyncbind)) ;
53+
54+
if (!state.opts.runtimePattern) {
55+
pr.ast.body.unshift(getRuntime('Function.prototype.$asyncbind',Function.prototype.$asyncbind)) ;
56+
} else {
57+
if (state.opts.runtimePattern==='directive') {
58+
if (path.node.directives) {
59+
for (var i=0; i<path.node.directives.length; i++) {
60+
if (path.node.directives[i].value.type==="DirectiveLiteral" && path.node.directives[i].value.value==="use runtime-nodent") {
61+
pr.ast.body.unshift(getRuntime('Function.prototype.$asyncbind',Function.prototype.$asyncbind)) ;
62+
path.node.directives.splice(i,1) ;
63+
break ;
64+
}
65+
}
66+
}
67+
} else {
68+
var pattern = new RegExp(state.opts.runtimePattern) ;
69+
if (state.file.parserOpts.filename.match(pattern)) {
70+
pr.ast.body.unshift(getRuntime('Function.prototype.$asyncbind',Function.prototype.$asyncbind)) ;
71+
}
72+
}
73+
}
5474
}
5575
}
5676
};

tests/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fast-async-test",
3-
"version": "6.0.11",
3+
"version": "6.0.12",
44
"dependencies": {
55
"babel-core": "^6.7.4",
66
"babel-plugin-syntax-async-functions": "^6.5.0",
@@ -20,7 +20,9 @@
2020
"babel": {
2121
"plugins": [
2222
"syntax-async-functions",
23-
"fast-async"
23+
["fast-async",{
24+
"runtimePattern":"directive"
25+
}]
2426
]
2527
}
2628
}

tests/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var testCode = require('fs').readFileSync(__dirname+'/test-input.js').toString()
2222
var babel = require("babel-core") ;
2323

2424
var transformers = {
25-
'fast-async':{plugins:[[require('../plugin.js'),{env:{dontMapStackTraces:true},compiler:{promises:true}}]]},
25+
'fast-async':{plugins:[[require('../plugin.js'),{runtimePatten:'directive',env:{dontMapStackTraces:true},compiler:{promises:true}}]]},
2626
};
2727

2828
var requires ;

0 commit comments

Comments
 (0)