broccoli-babel-transpiler
Advanced tools
Comparing version 7.0.0-alpha.1 to 7.0.0-beta.1
35
index.js
'use strict'; | ||
var transpiler = require('babel-core'); | ||
var Filter = require('broccoli-persistent-filter'); | ||
var clone = require('clone'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
@@ -13,3 +11,5 @@ var stringify = require('json-stable-stringify'); | ||
var hashForDep = require('hash-for-dep'); | ||
var ParallelApi = require('./lib/parallel-api'); | ||
function getExtensionsRegex(extensions) { | ||
@@ -36,5 +36,7 @@ return extensions.map(function(extension) { | ||
options.persist = 'persist' in options ? options.persist : true; | ||
options.async = true; | ||
Filter.call(this, inputTree, options); | ||
delete options.persist; | ||
delete options.async; | ||
delete options.annotation; | ||
@@ -80,3 +82,3 @@ delete options.description; | ||
Babel.prototype.transform = function(string, options) { | ||
return transpiler.transform(string, options); | ||
return ParallelApi.transformString(string, options); | ||
}; | ||
@@ -145,2 +147,8 @@ | ||
continue; | ||
} else if (type === 'object' && (typeof item.baseDir === 'function')) { | ||
hash.plugins.push(hashForDep(item.baseDir())); | ||
if (typeof item.cacheKey === 'function') { | ||
hash.plugins.push(item.cacheKey()); | ||
} | ||
} else if (type === 'object') { | ||
@@ -181,13 +189,16 @@ // iterate all keys in the item and push them into the cache | ||
var transpiled = this.transform(string, options); | ||
var plugin = this; | ||
return this.transform(string, options) | ||
.then(function (transpiled) { | ||
if (this.helperWhiteList) { | ||
var invalidHelpers = transpiled.metadata.usedHelpers.filter(function(helper) { | ||
return this.helperWhiteList.indexOf(helper) === -1; | ||
}, this); | ||
if (plugin.helperWhiteList) { | ||
var invalidHelpers = transpiled.metadata.usedHelpers.filter(function(helper) { | ||
return plugin.helperWhiteList.indexOf(helper) === -1; | ||
}, plugin); | ||
validateHelpers(invalidHelpers, relativePath); | ||
} | ||
validateHelpers(invalidHelpers, relativePath); | ||
} | ||
return transpiled.code; | ||
return transpiled.code; | ||
}); | ||
}; | ||
@@ -218,3 +229,3 @@ | ||
message = relativePath + ' was transformed and relies on `' + helpers + '`, which were not included in the helper whitelist. Either add these helpers to the whitelist or refactor to not be dependent on these runtime helper.'; | ||
message = relativePath + ' was transformed and relies on `' + helpers + '`, which were not included in the helper whitelist. Either add these helpers to the whitelist or refactor to not be dependent on these runtime helpers.'; | ||
} | ||
@@ -221,0 +232,0 @@ |
{ | ||
"name": "broccoli-babel-transpiler", | ||
"version": "7.0.0-alpha.1", | ||
"version": "7.0.0-beta.1", | ||
"description": "A Broccoli plugin which transpile ES6 to readable ES5 by using babel.", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"lib" | ||
], | ||
"scripts": { | ||
"test": "mocha test.js", | ||
"test:debug": "mocha debug test.js" | ||
"test:debug": "mocha debug test.js", | ||
"test:slow": "mocha test_slow.js", | ||
"test:all": "mocha test.js test_slow.js" | ||
}, | ||
@@ -34,15 +37,19 @@ "repository": { | ||
"dependencies": { | ||
"babel-core": "^7.0.0-alpha.9", | ||
"babel-core": "^7.0.0-beta.0", | ||
"broccoli-funnel": "^1.0.0", | ||
"broccoli-merge-trees": "^1.0.0", | ||
"broccoli-persistent-filter": "^1.0.1", | ||
"broccoli-persistent-filter": "^1.4.0", | ||
"clone": "^2.0.0", | ||
"hash-for-dep": "^1.0.2", | ||
"json-stable-stringify": "^1.0.0" | ||
"heimdalljs-logger": "^0.1.7", | ||
"json-stable-stringify": "^1.0.0", | ||
"rsvp": "^3.5.0", | ||
"workerpool": "^2.2.1" | ||
}, | ||
"devDependencies": { | ||
"babel-plugin-transform-es2015-block-scoping": "^6.4.0", | ||
"babel-plugin-transform-es2015-classes": "^6.9.0", | ||
"babel-plugin-transform-es2015-modules-amd": "^6.4.3", | ||
"babel-plugin-transform-strict-mode": "^6.3.13", | ||
"amd-name-resolver": "0.0.6", | ||
"babel-plugin-transform-es2015-block-scoping": "^7.0.0-beta.0", | ||
"babel-plugin-transform-es2015-classes": "^7.0.0-beta.0", | ||
"babel-plugin-transform-es2015-modules-amd": "^7.0.0-beta.0", | ||
"babel-plugin-transform-strict-mode": "^7.0.0-beta.0", | ||
"broccoli": "^0.16.3", | ||
@@ -56,6 +63,3 @@ "broccoli-test-helpers": "0.0.8", | ||
"node": ">= 4" | ||
}, | ||
"publishConfig": { | ||
"tag": "next" | ||
} | ||
} |
165
README.md
@@ -7,4 +7,3 @@ # broccoli-babel-transpiler | ||
A [Broccoli](https://github.com/broccolijs/broccoli) plugin which | ||
transpiles ES6 to readable ES5 by using [babel](https://github.com/babel/babel). | ||
A [Broccoli](https://github.com/broccolijs/broccoli) plugin which transpiles ES6 to readable ES5 (and much more) by using [Babel](https://github.com/babel/babel). | ||
@@ -23,8 +22,30 @@ ## How to install? | ||
var esTranspiler = require('broccoli-babel-transpiler'); | ||
var scriptTree = esTranspiler(inputTree, options); | ||
var scriptTree = esTranspiler(inputTree, babelOptions); | ||
``` | ||
You can find [options](https://babeljs.io/docs/usage/options) at babel's | ||
github repo. | ||
Note that, since Babel 6 (and v6 of this plugin), you need to be specific as to what your transpilation target is. Running `esTranspiler` with empty options will not transpile anything. You will need: | ||
* Explicit options, such as `presets`. See available [options](https://babeljs.io/docs/usage/options) at Babel's GitHub repo. | ||
* Babel plugins that implement the transforms you require. | ||
For a quick running example, install this plugin: | ||
```sh | ||
$ npm install babel-preset-env | ||
``` | ||
And then run the transform like this: | ||
```js | ||
var scriptTree = babel(inputTree, { | ||
presets: [ | ||
['env', { | ||
'targets': { | ||
'browsers': ["last 2 versions"] | ||
} | ||
}] | ||
] | ||
}); | ||
``` | ||
### Examples | ||
@@ -104,1 +125,135 @@ | ||
plugin's files change (including its deps). Each plugin should implement `baseDir` as: `Plugin.prototype.baseDir = function() { return \_\_dirname; };`. | ||
## Parallel Transpilation | ||
broccoli-babel-transpiler can run multiple babel transpiles in parallel using a pool of workers, to take advantage of multi-core systems. | ||
Because these workers are separate processes, the plugins and callback functions that are normally passed as options to babel must be specified in a serializable form. | ||
To enable this parallelization there is an API to tell the worker how to construct the plugin or callback in its process. | ||
Plugins are specified as an object with a `_parallelBabel` property: | ||
```js | ||
var plugin = { | ||
_parallelBabel: { | ||
requireFile: '/full/path/to/the/file', | ||
useMethod: 'methodName', | ||
buildUsing: 'buildFunction', | ||
params: { ok: 'this object will be passed to buildFunction()' } | ||
} | ||
}; | ||
``` | ||
Callbacks can be specified like plugins, or as functions with a `_parallelBabel` property: | ||
```js | ||
var callback = function() { /* do something */ }; | ||
callback._parallelBabel = { | ||
requireFile: '/full/path/to/the/file', | ||
useMethod: 'methodName', | ||
buildUsing: 'buildFunction', | ||
params: { ok: 'this object will be passed to buildFunction()' } | ||
}; | ||
``` | ||
### requireFile (required) | ||
This property specifies the file to require in the worker process to create the plugin or callback. | ||
This must be given as an absolute path. | ||
```js | ||
var esTranspiler = require('broccoli-babel-transpiler'); | ||
var somePlugin = { | ||
_parallelBabel: { | ||
requireFile: '/full/path/to/the/file' | ||
} | ||
}); | ||
var scriptTree = esTranspiler(inputTree, { | ||
plugins: [ | ||
'transform-strict-mode', // plugins that are given as strings will automatically be parallelized | ||
somePlugin | ||
] | ||
}); | ||
``` | ||
### useMethod (optional) | ||
This property specifies the method to use from the file that is required. | ||
If you have a plugin defined like this: | ||
```js | ||
// some_plugin.js | ||
module.exports = { | ||
pluginFunction(babel) { | ||
// do plugin things | ||
} | ||
}; | ||
``` | ||
You can tell broccoli-babel-transpiler to use that function in the worker processes like so: | ||
```js | ||
var esTranspiler = require('broccoli-babel-transpiler'); | ||
var somePlugin = { | ||
_parallelBabel: { | ||
requireFile: '/path/to/some_plugin', | ||
useMethod: 'pluginFunction' | ||
} | ||
}); | ||
var scriptTree = esTranspiler(inputTree, { | ||
plugins: [ somePlugin ] | ||
}); | ||
``` | ||
### buildUsing and params (optional) | ||
These properties specify a function to run to build the plugin (or callback), and any parameters | ||
to pass to that function. | ||
If the plugin needs to be built dynamically, you can do that like so: | ||
```js | ||
// some_plugin.js | ||
module.exports = { | ||
buildPlugin(params) { | ||
var pluginInstance = doSomethingWith(params.text); | ||
return pluginInstance; | ||
} | ||
}; | ||
``` | ||
This will tell the worker process to require the plugin and call the `buildPlugin` function with the `params` object as an argument: | ||
```js | ||
var esTranspiler = require('broccoli-babel-transpiler'); | ||
var somePlugin = { | ||
_parallelBabel: { | ||
requireFile: '/path/to/some_plugin', | ||
buildUsing: 'buildPlugin', | ||
params: { text: 'some text' } | ||
} | ||
}); | ||
var scriptTree = esTranspiler(inputTree, { | ||
plugins: [ somePlugin ] | ||
}); | ||
``` | ||
Note: If both `useMethod` and `buildUsing` are specified, `useMethod` takes precedence. | ||
### Number of jobs | ||
The number of parallel jobs defaults to the number of detected CPUs - 1. | ||
This can be changed with the `JOBS` environment variable: | ||
``` | ||
JOBS=4 ember build | ||
``` |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
22604
6
299
257
10
10
3
+ Addedheimdalljs-logger@^0.1.7
+ Addedrsvp@^3.5.0
+ Addedworkerpool@^2.2.1
+ Addedworkerpool@2.3.4(transitive)
Updatedbabel-core@^7.0.0-beta.0