Socket
Socket
Sign inDemoInstall

babel-loader

Package Overview
Dependencies
213
Maintainers
2
Versions
77
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.0.0 to 5.1.0

.editorconfig

167

index.js

@@ -1,125 +0,64 @@

var loaderUtils = require('loader-utils'),
babel = require('babel-core'),
crypto = require('crypto'),
fs = require('fs'),
path = require('path'),
os = require('os'),
zlib = require('zlib'),
version = require('./package').version,
toBoolean = function (val) {
if (val === 'true') { return true; }
if (val === 'false') { return false; }
return val;
};
var assign = require('object-assign');
var babel = require('babel-core');
var cache = require('./lib/fs-cache.js');
var loaderUtils = require('loader-utils');
var pkg = require('./package.json');
module.exports = function (source, inputSourceMap) {
var transpile = function(source, options) {
var result = babel.transform(source, options);
var code = result.code;
var map = result.map;
var options = loaderUtils.parseQuery(this.query),
callback = this.async(),
result, cacheDirectory;
if (map) {
map.sourcesContent = [source];
}
if (this.cacheable) {
this.cacheable();
}
return {
code: code,
map: map,
};
};
// Convert 'true'/'false' to true/false
options = Object.keys(options).reduce(function (accumulator, key) {
accumulator[key] = toBoolean(options[key]);
return accumulator;
}, {});
module.exports = function(source, inputSourceMap) {
var callback = this.async();
var result = {};
// Handle options
var defaultOptions = {
inputSourceMap: inputSourceMap,
filename: loaderUtils.getRemainingRequest(this),
cacheIdentifier: JSON.stringify({
'babel-loader': pkg.version,
'babel-core': babel.version,
}),
};
var globalOptions = this.options.babel;
var loaderOptions = loaderUtils.parseQuery(this.query);
var options = assign({}, defaultOptions, globalOptions, loaderOptions);
if (options.sourceMap === undefined) {
options.sourceMap = this.sourceMap;
options.inputSourceMap = inputSourceMap;
options.filename = loaderUtils.getRemainingRequest(this);
}
cacheDirectory = options.cacheDirectory;
delete options.cacheDirectory;
cacheDirectory = options.cacheDirectory;
cacheIdentifier = options.cacheIdentifier;
if (cacheDirectory === true) cacheDirectory = os.tmpdir();
delete options.cacheDirectory;
delete options.cacheIdentifier;
if (cacheDirectory){
cachedTranspile(cacheDirectory, source, options, onResult);
} else {
onResult(null, transpile(source, options));
}
this.cacheable();
function onResult(err, result){
if (err) return callback(err);
callback(err, err ? null : result.code, err ? null : result.map);
}
if (cacheDirectory) {
cache({
directory: cacheDirectory,
identifier: cacheIdentifier,
source: source,
options: options,
transform: transpile,
}, function(err, result) {
callback(err, result.code, result.map);
});
} else {
result = transpile(source, options);
callback(null, result.code, result.map);
}
};
function transpile(source, options){
var result = babel.transform(source, options);
var code = result.code;
var map = result.map;
if (map) {
map.sourcesContent = [source];
}
return {
code: code,
map: map
};
}
function cachedTranspile(cacheDirectory, source, options, callback){
var cacheFile = path.join(cacheDirectory, buildCachePath(cacheDirectory, source, options));
readCache(cacheFile, function(err, result){
if (err){
try {
result = transpile(source, options);
} catch (e){
return callback(e);
}
writeCache(cacheFile, result, function(err){
callback(err, result);
});
} else {
callback(null, result);
}
});
}
function readCache(cacheFile, callback){
fs.readFile(cacheFile, function(err, data){
if (err) return callback(err);
zlib.gunzip(data, function(err, content){
if (err) return callback(err);
try {
content = JSON.parse(content);
} catch (e){
return callback(e);
}
callback(null, content);
});
});
}
function writeCache(cacheFile, result, callback){
var content = JSON.stringify(result);
zlib.gzip(content, function(err, data){
if (err) return callback(err);
fs.writeFile(cacheFile, data, callback);
});
}
function buildCachePath(dir, source, options){
var hash = crypto.createHash('SHA1');
hash.end(JSON.stringify({
loaderVersion: version,
babelVersion: babel.version,
source: source,
options: options
}));
return 'babel-loader-cache-' + hash.read().toString('hex') + '.json.gzip';
}
{
"name": "babel-loader",
"version": "5.0.0",
"version": "5.1.0",
"description": "babel module loader for webpack",
"main": "index.js",
"dependencies": {
"babel-core": "^5.0.0",
"loader-utils": "^0.2.5"
"babel-core": "^5.4.0",
"object-assign": "^2.0.0",
"loader-utils": "^0.2.7"
},

@@ -15,9 +16,12 @@ "peerDependencies": {

"devDependencies": {
"mocha": "^2.0.1",
"mocha-loader": "^0.7.0",
"should": "^4.3.1",
"webpack-dev-server": "^1.6.6"
"babel-core": "^5.4.2",
"expect.js": "^0.3.1",
"istanbul": "^0.3.14",
"mkdirp": "^0.5.1",
"mocha": "^2.2.5",
"rimraf": "^2.3.3",
"webpack": "^1.9.5"
},
"scripts": {
"test": "webpack-dev-server --config=test/webpack.config.js"
"test": "istanbul cover ./node_modules/.bin/_mocha -- test/*.test.js"
},

@@ -24,0 +28,0 @@ "repository": {

# babel-loader
> Babel is a compiler for writing next generation JavaScript.
> Turn ES6 code into vanilla ES5 with no runtime required using [babel](https://github.com/babel/babel);
This package allows the use babel with [webpack](https://github.com/webpack/webpack)
## Install
__Notes:__ Issues with the output should be reported on the babel [issue tracker](https://github.com/babel/babel/issues);
## Installation
```bash
npm install babel-loader --save-dev
```
$ npm install --save-dev babel-loader
```
__Note:__ [npm](https://npmjs.com) will deprecate [peerDependencies](https://github.com/npm/npm/issues/6565) on the next major release, so required dependencies like babel-core and webpack will have to be installed manually.
## Usage
Within your webpack configuration object, you'll need to add the babel-loader to the list of modules, like so:
```javascript
import Animal from 'babel!./Animal.js';
class Person extends Animal {
constructor(arg='default') {
this.eat = 'Happy Meal';
}
```javascript
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel'
}
]
}
```
export default Person;
```
### Options
```javascript
var Person = require('babel!./Person.js').default;
new Person();
```
See the `babel` [options](http://babeljs.io/docs/usage/options/).
Or within the webpack config:
You can pass options to the loader by writting them as a [query string](https://github.com/webpack/loader-utils):
```javascript
```javascript
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'}
]
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel?optional[]=runtime&stage=0'
}
]
}
```
```
and then import normally:
or by using the query property:
```javascript
import Person from './Person.js';
```
```javascript
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
optional: ['runtime'],
stage: 0
}
}
]
}
```
This loader also supports the following loader-specific option:
* `cacheDirectory`: When set, the given directory will be used to cache the results of the loader. Future webpack builds will attempt to read from the cache to avoid needing to run the potentially expensive Babel recompilation process on each run. A value of `true` will cause the loader to use the default OS temporary file directory.
* `cacheIdentifier`: When set, it will add the given identifier to the cached files. This can be used to force cache busting if the identifier changes. By default the identifier is made by using the babel-core's version and the babel-loader's version.
__Note:__ The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).
## Troubleshooting
#### babel-loader is slow!
### babel-loader is slow!
Make sure you are transforming as few files as possible. Because you are probably
matching `/\.js$/`, you might be transforming the `node_modules` folder or other unwanted
source. See the `exclude` option in the `loaders` config as documented above.
Make sure you are transforming as few files as possible. Because you are probably
matching `/\.js$/`, you might be transforming the `node_modules` folder or other unwanted
source.
#### babel is injecting helpers into each file and bloating my code!
See the `exclude` option in the `loaders` config as documented above.
babel uses very small helpers for common functions such as `_extend`. By default
this will be added to every file that requires it.
### babel is injecting helpers into each file and bloating my code!
You can instead require the babel runtime as a separate module to avoid the duplication.
babel uses very small helpers for common functions such as `_extend`. By default
this will be added to every file that requires it.
The following configuration disables automatic per-file runtime injection in babel, instead
requiring `babel-runtime` and making all helper references use it.
You can instead require the babel runtime as a separate module to avoid the duplication.
See the [docs](https://babeljs.io/docs/usage/runtime) for more information.
The following configuration disables automatic per-file runtime injection in babel, instead
requiring `babel-runtime` and making all helper references use it.
**NOTE:** You must run `npm install babel-runtime --save` to include this in your project.
See the [docs](https://babeljs.io/docs/usage/runtime) for more information.
**NOTE:** You must run `npm install babel-runtime --save` to include this in your project.
```javascript
loaders: [
// the optional 'runtime' transformer tells babel to require the runtime instead of inlining it.
{ test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader?experimental&optional=runtime' }
// the optional 'runtime' transformer tells babel to require the runtime
// instead of inlining it.
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader?optional[]=runtime'
}
]
```
## Options
#### custom polyfills (e.g. Promise library)
See the `babel` [options](http://babeljs.io/docs/usage/options/)
Since Babel includes a polyfill that includes a custom [regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) and [core.js](https://github.com/zloirock/core-js), the following usual shimming method using `webpack.ProvidePlugin` will not work:
This loader also supports the following loader-specific option:
```javascript
// ...
new webpack.ProvidePlugin({
'Promise': 'bluebird'
}),
// ...
```
* `cacheDirectory`: When set, the given directory will be used to cache the results of the loader.
Future webpack builds will attempt to read from the cache to avoid needing to run the potentially
expensive Babel recompilation process on each run. A value of `true` will cause the loader to
use the default OS temporary file directory.
The following approach will not work either:
Note: The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).
```javascript
require('babel-runtime/core-js/promise').default = require('bluebird');
## License
var promise = new Promise;
```
MIT © Luis Couto
which outputs to (using `runtime`):
```javascript
'use strict';
var _Promise = require('babel-runtime/core-js/promise')['default'];
require('babel-runtime/core-js/promise')['default'] = require('bluebird');
var promise = new _Promise();
```
The previous `Promise` library is referenced and used before it is overridden.
One approach is to have a "bootstrap" step in your application that would first override the default globals before your application:
```javascript
// bootstrap.js
require('babel-runtime/core-js/promise').default = require('bluebird');
// ...
require('./app');
```
## [License](http://couto.mit-license.org/)

@@ -1,3 +0,5 @@

import test from '../../!./test.js';
/*jshint esnext:true*/
import test from './import.js';
class App {

@@ -4,0 +6,0 @@ constructor(arg='test') {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc