babel-loader 6.2.6 → 6.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/README.md +42 -85
- package/lib/fs-cache.js +54 -108
- package/lib/index.js +135 -0
- package/lib/resolve-rc.js +9 -22
- package/lib/utils/exists.js +18 -0
- package/lib/utils/read.js +18 -0
- package/lib/utils/relative.js +14 -0
- package/package.json +50 -17
- package/index.js +0 -133
- package/lib/helpers/exists.js +0 -23
- package/lib/helpers/read.js +0 -25
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,57 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v6.2.9
|
4
|
+
|
5
|
+
### 😢 Regression
|
6
|
+
|
7
|
+
Source maps on windows did not work correctly with v6.2.8.
|
8
|
+
Thanks @josephst
|
9
|
+
|
10
|
+
### 🏠 Internal
|
11
|
+
|
12
|
+
- Add AppVeyor to run tests on windows @danez
|
13
|
+
- Fix tests on windows (#343) @danez
|
14
|
+
|
15
|
+
## v6.2.8
|
16
|
+
|
17
|
+
### 🐛 Bug Fix
|
18
|
+
|
19
|
+
- gzipped files should have `.gz` as the extension, not `.gzip` (#326) @bjornstar
|
20
|
+
- fix options.sourceFileName gennerate bug (#260) @creeperyang
|
21
|
+
|
22
|
+
### 📝 Documentation
|
23
|
+
|
24
|
+
- Update README docs for cacheDirectory's actual behaviour (#245) @sohkai
|
25
|
+
- updates docs re: transform-runtime (#197) @gbrassey
|
26
|
+
|
27
|
+
### 🏠 Internal
|
28
|
+
|
29
|
+
- Use eslint and nyc (#321) @danez
|
30
|
+
- Adjust travis config (#320) @danez
|
31
|
+
- Use babel to compile babel-loader (#319) @danez
|
32
|
+
|
33
|
+
## v6.2.7
|
34
|
+
|
35
|
+
### 😢 Regression
|
36
|
+
|
37
|
+
Fallback to `os.tmpdir()` if no cachedir found (#318) (fixes #317) @danez
|
38
|
+
|
39
|
+
Fixes an issue with v6.2.6 when using `babel-loader` as a global package.
|
40
|
+
|
41
|
+
## v6.2.6
|
42
|
+
|
43
|
+
### 🐛 Bug Fix
|
44
|
+
|
45
|
+
- Use standard cache dir as default `cacheDirectory` (#301) @fson
|
46
|
+
|
47
|
+
Use the common cache directory, `./node_modules/.cache/babel-loader`, as the default cache directory (when the cacheDirectory setting is enabled).
|
48
|
+
|
49
|
+
```js
|
50
|
+
query: {
|
51
|
+
cacheDirectory: true
|
52
|
+
}
|
53
|
+
```
|
54
|
+
|
3
55
|
## v6.2.5
|
4
56
|
|
5
57
|
- Don't show the call stack for a Babel error (such as when you have a syntax error)
|
package/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
# babel-loader
|
1
|
+
# babel-loader
|
2
|
+
[](https://www.npmjs.com/package/babel-loader)
|
3
|
+
[](https://travis-ci.org/babel/babel-loader)
|
4
|
+
[](https://ci.appveyor.com/project/danez/babel-loader/branch/master)
|
5
|
+
[](https://codecov.io/gh/babel/babel-loader)
|
2
6
|
> Babel is a compiler for writing next generation JavaScript.
|
3
7
|
|
4
8
|
This package allows transpiling JavaScript files using [Babel](https://github.com/babel/babel) and [webpack](https://github.com/webpack/webpack).
|
@@ -8,7 +12,13 @@
|
|
8
12
|
## Installation
|
9
13
|
|
10
14
|
```bash
|
11
|
-
npm install babel-loader babel-core babel-preset-es2015 --save-dev
|
15
|
+
npm install babel-loader babel-core babel-preset-es2015 webpack --save-dev
|
16
|
+
```
|
17
|
+
|
18
|
+
or
|
19
|
+
|
20
|
+
```bash
|
21
|
+
yarn add babel-loader babel-core babel-preset-es2015 webpack --dev
|
12
22
|
```
|
13
23
|
|
14
24
|
__Note:__ [npm](https://npmjs.com) deprecated [auto-installing of peerDependencies](https://github.com/npm/npm/issues/6565) since npm@3, so required peer dependencies like babel-core and webpack must be listed explicitly in your `package.json`.
|
@@ -27,7 +37,7 @@ module: {
|
|
27
37
|
{
|
28
38
|
test: /\.js$/,
|
29
39
|
exclude: /(node_modules|bower_components)/,
|
30
|
-
loader: 'babel
|
40
|
+
loader: 'babel-loader',
|
31
41
|
query: {
|
32
42
|
presets: ['es2015']
|
33
43
|
}
|
@@ -48,13 +58,13 @@ module: {
|
|
48
58
|
{
|
49
59
|
test: /\.js$/,
|
50
60
|
exclude: /(node_modules|bower_components)/,
|
51
|
-
loader: 'babel?presets[]=es2015'
|
61
|
+
loader: 'babel-loader?presets[]=es2015'
|
52
62
|
}
|
53
63
|
]
|
54
64
|
}
|
55
65
|
```
|
56
66
|
|
57
|
-
or by using the query property:
|
67
|
+
or by using the [query property](https://webpack.github.io/docs/using-loaders.html#query-parameters):
|
58
68
|
|
59
69
|
```javascript
|
60
70
|
module: {
|
@@ -62,7 +72,7 @@ module: {
|
|
62
72
|
{
|
63
73
|
test: /\.js$/,
|
64
74
|
exclude: /(node_modules|bower_components)/,
|
65
|
-
loader: 'babel',
|
75
|
+
loader: 'babel-loader',
|
66
76
|
query: {
|
67
77
|
presets: ['es2015']
|
68
78
|
}
|
@@ -73,10 +83,11 @@ module: {
|
|
73
83
|
|
74
84
|
This loader also supports the following loader-specific option:
|
75
85
|
|
76
|
-
* `cacheDirectory`: Default `false`. 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. If the value is blank (`loader: 'babel-loader?cacheDirectory'`) the loader will use the default cache directory in `node_modules/.cache/babel-loader
|
86
|
+
* `cacheDirectory`: Default `false`. 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. If the value is blank (`loader: 'babel-loader?cacheDirectory'`) or `true` (`loader: babel-loader?cacheDirectory=true`) the loader will use the default cache directory in `node_modules/.cache/babel-loader` or fallback to the default OS temporary file directory if no `node_modules` folder could be found in any root directory.
|
77
87
|
|
78
88
|
* `cacheIdentifier`: Default is a string composed by the babel-core's version, the babel-loader's version, the contents of .babelrc file if it exists and the value of the environment variable `BABEL_ENV` with a fallback to the `NODE_ENV` environment variable. This can be set to a custom value to force cache busting if the identifier changes.
|
79
89
|
|
90
|
+
* `babelrc`: Default `true`. When `false`, will ignore `.babelrc` files (except those referenced by the `extends` option).
|
80
91
|
|
81
92
|
__Note:__ The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).
|
82
93
|
|
@@ -114,7 +125,7 @@ loaders: [
|
|
114
125
|
{
|
115
126
|
test: /\.js$/,
|
116
127
|
exclude: /(node_modules|bower_components)/,
|
117
|
-
loader: 'babel',
|
128
|
+
loader: 'babel-loader',
|
118
129
|
query: {
|
119
130
|
presets: ['es2015'],
|
120
131
|
plugins: ['transform-runtime']
|
@@ -123,84 +134,9 @@ loaders: [
|
|
123
134
|
]
|
124
135
|
```
|
125
136
|
|
126
|
-
|
127
|
-
|
128
|
-
If you receive this message it means that you have the npm package `babel` installed and use the short notation of the loader in the webpack config:
|
129
|
-
```js
|
130
|
-
{
|
131
|
-
test: /\.js$/,
|
132
|
-
loader: 'babel',
|
133
|
-
}
|
134
|
-
```
|
135
|
-
|
136
|
-
Webpack then tries to load the `babel` package instead of the `babel-loader`.
|
137
|
-
|
138
|
-
To fix this you should uninstall the npm package `babel` as it is deprecated in babel v6. (instead install `babel-cli` or `babel-core`)
|
139
|
-
In the case one of your dependencies is installing `babel` and you cannot uninstall it yourself, use the complete name of the loader in the webpack config:
|
140
|
-
```js
|
141
|
-
{
|
142
|
-
test: /\.js$/,
|
143
|
-
loader: 'babel-loader',
|
144
|
-
}
|
145
|
-
```
|
146
|
-
|
147
|
-
|
148
|
-
### using `cacheDirectory` fails with ENOENT Error
|
149
|
-
|
150
|
-
If using cacheDirectory results in an error similar to the following:
|
151
|
-
|
152
|
-
```bash
|
153
|
-
ERROR in ./frontend/src/main.js
|
154
|
-
Module build failed: Error: ENOENT, open 'true/350c59cae6b7bce3bb58c8240147581bfdc9cccc.json.gzip'
|
155
|
-
@ multi app
|
156
|
-
```
|
157
|
-
(notice the `true/` in the filepath)
|
158
|
-
|
159
|
-
That means that most likely, you're not setting the options correctly, and you're doing something similar to:
|
160
|
-
|
161
|
-
```javascript
|
162
|
-
loaders: [
|
163
|
-
{
|
164
|
-
test: /\.js$/,
|
165
|
-
exclude: /(node_modules|bower_components)/,
|
166
|
-
loader: 'babel?cacheDirectory=true'
|
167
|
-
}
|
168
|
-
]
|
169
|
-
```
|
137
|
+
#### **NOTE:** transform-runtime & custom polyfills (e.g. Promise library)
|
170
138
|
|
171
|
-
|
172
|
-
|
173
|
-
```javascript
|
174
|
-
loaders: [
|
175
|
-
{
|
176
|
-
test: /\.js$/,
|
177
|
-
exclude: /(node_modules|bower_components)/,
|
178
|
-
loader: 'babel?cacheDirectory'
|
179
|
-
}
|
180
|
-
]
|
181
|
-
```
|
182
|
-
|
183
|
-
or use the [query](https://webpack.github.io/docs/using-loaders.html#query-parameters) property:
|
184
|
-
|
185
|
-
```javascript
|
186
|
-
loaders: [
|
187
|
-
// the optional 'runtime' transformer tells babel to require the runtime
|
188
|
-
// instead of inlining it.
|
189
|
-
{
|
190
|
-
test: /\.js$/,
|
191
|
-
exclude: /(node_modules|bower_components)/,
|
192
|
-
loader: 'babel',
|
193
|
-
query: {
|
194
|
-
cacheDirectory: true
|
195
|
-
}
|
196
|
-
}
|
197
|
-
]
|
198
|
-
```
|
199
|
-
|
200
|
-
|
201
|
-
### custom polyfills (e.g. Promise library)
|
202
|
-
|
203
|
-
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:
|
139
|
+
Since [babel-plugin-transform-runtime](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime) includes a polyfill that includes a custom [regenerator runtime](https://github.com/facebook/regenerator/blob/master/packages/regenerator-runtime/runtime.js) and [core.js](https://github.com/zloirock/core-js), the following usual shimming method using `webpack.ProvidePlugin` will not work:
|
204
140
|
|
205
141
|
```javascript
|
206
142
|
// ...
|
@@ -244,4 +180,25 @@ require('babel-runtime/core-js/promise').default = require('bluebird');
|
|
244
180
|
require('./app');
|
245
181
|
```
|
246
182
|
|
183
|
+
### The node API for `babel` has been moved to `babel-core`.
|
184
|
+
|
185
|
+
If you receive this message it means that you have the npm package `babel` installed and use the short notation of the loader in the webpack config (which is not valid anymore as of webpack 2.x):
|
186
|
+
```js
|
187
|
+
{
|
188
|
+
test: /\.js$/,
|
189
|
+
loader: 'babel',
|
190
|
+
}
|
191
|
+
```
|
192
|
+
|
193
|
+
Webpack then tries to load the `babel` package instead of the `babel-loader`.
|
194
|
+
|
195
|
+
To fix this you should uninstall the npm package `babel` as it is deprecated in babel v6. (instead install `babel-cli` or `babel-core`)
|
196
|
+
In the case one of your dependencies is installing `babel` and you cannot uninstall it yourself, use the complete name of the loader in the webpack config:
|
197
|
+
```js
|
198
|
+
{
|
199
|
+
test: /\.js$/,
|
200
|
+
loader: 'babel-loader',
|
201
|
+
}
|
202
|
+
```
|
203
|
+
|
247
204
|
## [License](http://couto.mit-license.org/)
|
package/lib/fs-cache.js
CHANGED
@@ -1,37 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
var zlib = require('zlib');
|
19
|
-
|
20
|
-
/**
|
21
|
-
* Read the contents from the compressed file.
|
22
|
-
*
|
23
|
-
* @async
|
24
|
-
* @params {String} filename
|
25
|
-
* @params {Function} callback
|
26
|
-
*/
|
27
|
-
var read = function(filename, callback) {
|
28
|
-
return fs.readFile(filename, function(err, data) {
|
29
|
-
if (err) { return callback(err); }
|
30
|
-
|
31
|
-
return zlib.gunzip(data, function(err, content) {
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var crypto = require("crypto");
|
4
|
+
var mkdirp = require("mkdirp");
|
5
|
+
var findCacheDir = require("find-cache-dir");
|
6
|
+
var fs = require("fs");
|
7
|
+
var os = require("os");
|
8
|
+
var path = require("path");
|
9
|
+
var zlib = require("zlib");
|
10
|
+
|
11
|
+
var read = function read(filename, callback) {
|
12
|
+
return fs.readFile(filename, function (err, data) {
|
13
|
+
if (err) {
|
14
|
+
return callback(err);
|
15
|
+
}
|
16
|
+
|
17
|
+
return zlib.gunzip(data, function (err, content) {
|
32
18
|
var result = {};
|
33
19
|
|
34
|
-
if (err) {
|
20
|
+
if (err) {
|
21
|
+
return callback(err);
|
22
|
+
}
|
35
23
|
|
36
24
|
try {
|
37
25
|
result = JSON.parse(content);
|
@@ -44,113 +32,71 @@ var read = function(filename, callback) {
|
|
44
32
|
});
|
45
33
|
};
|
46
34
|
|
47
|
-
|
48
|
-
/**
|
49
|
-
* Write contents into a compressed file.
|
50
|
-
*
|
51
|
-
* @async
|
52
|
-
* @params {String} filename
|
53
|
-
* @params {String} result
|
54
|
-
* @params {Function} callback
|
55
|
-
*/
|
56
|
-
var write = function(filename, result, callback) {
|
35
|
+
var write = function write(filename, result, callback) {
|
57
36
|
var content = JSON.stringify(result);
|
58
37
|
|
59
|
-
return zlib.gzip(content, function(err, data) {
|
60
|
-
if (err) {
|
38
|
+
return zlib.gzip(content, function (err, data) {
|
39
|
+
if (err) {
|
40
|
+
return callback(err);
|
41
|
+
}
|
61
42
|
|
62
43
|
return fs.writeFile(filename, data, callback);
|
63
44
|
});
|
64
45
|
};
|
65
46
|
|
66
|
-
|
67
|
-
|
68
|
-
* Build the filename for the cached file
|
69
|
-
*
|
70
|
-
* @params {String} source File source code
|
71
|
-
* @params {Object} options Options used
|
72
|
-
*
|
73
|
-
* @return {String}
|
74
|
-
*/
|
75
|
-
var filename = function(source, identifier, options) {
|
76
|
-
var hash = crypto.createHash('SHA1');
|
47
|
+
var filename = function filename(source, identifier, options) {
|
48
|
+
var hash = crypto.createHash("SHA1");
|
77
49
|
var contents = JSON.stringify({
|
78
50
|
source: source,
|
79
51
|
options: options,
|
80
|
-
identifier: identifier
|
52
|
+
identifier: identifier
|
81
53
|
});
|
82
54
|
|
83
55
|
hash.end(contents);
|
84
56
|
|
85
|
-
return hash.read().toString(
|
57
|
+
return hash.read().toString("hex") + ".json.gz";
|
86
58
|
};
|
87
59
|
|
88
|
-
|
89
|
-
* Retrieve file from cache, or create a new one for future reads
|
90
|
-
*
|
91
|
-
* @async
|
92
|
-
* @param {Object} params
|
93
|
-
* @param {String} params.directory Directory to store cached files
|
94
|
-
* @param {String} params.identifier Unique identifier to bust cache
|
95
|
-
* @param {String} params.source Original contents of the file to be cached
|
96
|
-
* @param {Object} params.options Options to be given to the transform fn
|
97
|
-
* @param {Function} params.transform Function that will transform the
|
98
|
-
* original file and whose result will be
|
99
|
-
* cached
|
100
|
-
*
|
101
|
-
* @param {Function<err, result>} callback
|
102
|
-
*
|
103
|
-
* @example
|
104
|
-
*
|
105
|
-
* cache({
|
106
|
-
* directory: '.tmp/cache',
|
107
|
-
* identifier: 'babel-loader-cachefile',
|
108
|
-
* source: *source code from file*,
|
109
|
-
* options: {
|
110
|
-
* experimental: true,
|
111
|
-
* runtime: true
|
112
|
-
* },
|
113
|
-
* transform: function(source, options) {
|
114
|
-
* var content = *do what you need with the source*
|
115
|
-
* return content;
|
116
|
-
* }
|
117
|
-
* }, function(err, result) {
|
118
|
-
*
|
119
|
-
* });
|
120
|
-
*/
|
121
|
-
var cache = module.exports = function(params, callback) {
|
122
|
-
// Spread params into named variables
|
123
|
-
// Forgive user whenever possible
|
60
|
+
var handleCache = function handleCache(directory, params, callback) {
|
124
61
|
var source = params.source;
|
125
62
|
var options = params.options || {};
|
126
63
|
var transform = params.transform;
|
127
64
|
var identifier = params.identifier;
|
128
|
-
var
|
129
|
-
params.directory :
|
130
|
-
findCacheDir({ name: 'babel-loader' });
|
131
|
-
var file = path.join(directory, filename(source, identifier, options));
|
65
|
+
var shouldFallback = typeof params.directory !== "string" && directory !== os.tmpdir();
|
132
66
|
|
133
|
-
|
134
|
-
|
135
|
-
if (err) { return callback(err); }
|
67
|
+
mkdirp(directory, function (err) {
|
68
|
+
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
|
136
69
|
|
137
|
-
|
70
|
+
var file = path.join(directory, filename(source, identifier, options));
|
71
|
+
|
72
|
+
return read(file, function (err, content) {
|
138
73
|
var result = {};
|
139
|
-
// No errors mean that the file was previously cached
|
140
|
-
// we just need to return it
|
141
|
-
if (!err) { return callback(null, content); }
|
142
74
|
|
143
|
-
|
144
|
-
|
75
|
+
if (!err) return callback(null, content);
|
76
|
+
|
145
77
|
try {
|
146
78
|
result = transform(source, options);
|
147
79
|
} catch (error) {
|
148
80
|
return callback(error);
|
149
81
|
}
|
150
82
|
|
151
|
-
return write(file, result, function(err) {
|
152
|
-
return
|
83
|
+
return write(file, result, function (err) {
|
84
|
+
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
|
85
|
+
|
86
|
+
callback(null, result);
|
153
87
|
});
|
154
88
|
});
|
155
89
|
});
|
156
90
|
};
|
91
|
+
|
92
|
+
module.exports = function (params, callback) {
|
93
|
+
var directory = void 0;
|
94
|
+
|
95
|
+
if (typeof params.directory === "string") {
|
96
|
+
directory = params.directory;
|
97
|
+
} else {
|
98
|
+
directory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
|
99
|
+
}
|
100
|
+
|
101
|
+
handleCache(directory, params, callback);
|
102
|
+
};
|
package/lib/index.js
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
4
|
+
|
5
|
+
var assign = require("object-assign");
|
6
|
+
var babel = require("babel-core");
|
7
|
+
var loaderUtils = require("loader-utils");
|
8
|
+
var path = require("path");
|
9
|
+
var cache = require("./fs-cache.js");
|
10
|
+
var exists = require("./utils/exists")();
|
11
|
+
var relative = require("./utils/relative");
|
12
|
+
var read = require("./utils/read")();
|
13
|
+
var resolveRc = require("./resolve-rc.js");
|
14
|
+
var pkg = require("./../package.json");
|
15
|
+
|
16
|
+
function BabelLoaderError(name, message, codeFrame, hideStack, error) {
|
17
|
+
Error.call(this);
|
18
|
+
Error.captureStackTrace(this, BabelLoaderError);
|
19
|
+
|
20
|
+
this.name = "BabelLoaderError";
|
21
|
+
this.message = formatMessage(name, message, codeFrame);
|
22
|
+
this.hideStack = hideStack;
|
23
|
+
this.error = error;
|
24
|
+
}
|
25
|
+
|
26
|
+
BabelLoaderError.prototype = Object.create(Error.prototype);
|
27
|
+
BabelLoaderError.prototype.constructor = BabelLoaderError;
|
28
|
+
|
29
|
+
var STRIP_FILENAME_RE = /^[^:]+: /;
|
30
|
+
|
31
|
+
var formatMessage = function formatMessage(name, message, codeFrame) {
|
32
|
+
return (name ? name + ": " : "") + message + "\n\n" + codeFrame + "\n";
|
33
|
+
};
|
34
|
+
|
35
|
+
var transpile = function transpile(source, options) {
|
36
|
+
var result = void 0;
|
37
|
+
try {
|
38
|
+
result = babel.transform(source, options);
|
39
|
+
} catch (error) {
|
40
|
+
if (error.message && error.codeFrame) {
|
41
|
+
var message = error.message;
|
42
|
+
var name = void 0;
|
43
|
+
var hideStack = void 0;
|
44
|
+
if (error instanceof SyntaxError) {
|
45
|
+
message = message.replace(STRIP_FILENAME_RE, "");
|
46
|
+
name = "SyntaxError";
|
47
|
+
hideStack = true;
|
48
|
+
} else if (error instanceof TypeError) {
|
49
|
+
message = message.replace(STRIP_FILENAME_RE, "");
|
50
|
+
hideStack = true;
|
51
|
+
}
|
52
|
+
throw new BabelLoaderError(name, message, error.codeFrame, hideStack, error);
|
53
|
+
} else {
|
54
|
+
throw error;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
var code = result.code;
|
58
|
+
var map = result.map;
|
59
|
+
|
60
|
+
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
|
61
|
+
map.sourcesContent = [source];
|
62
|
+
}
|
63
|
+
|
64
|
+
return {
|
65
|
+
code: code,
|
66
|
+
map: map
|
67
|
+
};
|
68
|
+
};
|
69
|
+
|
70
|
+
module.exports = function (source, inputSourceMap) {
|
71
|
+
var _this = this;
|
72
|
+
|
73
|
+
var result = {};
|
74
|
+
|
75
|
+
var webpackRemainingChain = loaderUtils.getRemainingRequest(this).split("!");
|
76
|
+
var filename = webpackRemainingChain[webpackRemainingChain.length - 1];
|
77
|
+
|
78
|
+
var globalOptions = this.options.babel || {};
|
79
|
+
var loaderOptions = loaderUtils.parseQuery(this.query);
|
80
|
+
var userOptions = assign({}, globalOptions, loaderOptions);
|
81
|
+
var defaultOptions = {
|
82
|
+
inputSourceMap: inputSourceMap,
|
83
|
+
sourceRoot: process.cwd(),
|
84
|
+
filename: filename,
|
85
|
+
cacheIdentifier: JSON.stringify({
|
86
|
+
"babel-loader": pkg.version,
|
87
|
+
"babel-core": babel.version,
|
88
|
+
babelrc: exists(userOptions.babelrc) ? read(userOptions.babelrc) : resolveRc(path.dirname(filename)),
|
89
|
+
env: process.env.BABEL_ENV || process.env.NODE_ENV
|
90
|
+
})
|
91
|
+
};
|
92
|
+
|
93
|
+
var options = assign({}, defaultOptions, userOptions);
|
94
|
+
|
95
|
+
if (userOptions.sourceMap === undefined) {
|
96
|
+
options.sourceMap = this.sourceMap;
|
97
|
+
}
|
98
|
+
|
99
|
+
if (options.sourceFileName === undefined) {
|
100
|
+
options.sourceFileName = relative(options.sourceRoot, options.filename);
|
101
|
+
}
|
102
|
+
|
103
|
+
var cacheDirectory = options.cacheDirectory;
|
104
|
+
var cacheIdentifier = options.cacheIdentifier;
|
105
|
+
|
106
|
+
delete options.cacheDirectory;
|
107
|
+
delete options.cacheIdentifier;
|
108
|
+
|
109
|
+
this.cacheable();
|
110
|
+
|
111
|
+
if (cacheDirectory) {
|
112
|
+
var _ret = function () {
|
113
|
+
var callback = _this.async();
|
114
|
+
return {
|
115
|
+
v: cache({
|
116
|
+
directory: cacheDirectory,
|
117
|
+
identifier: cacheIdentifier,
|
118
|
+
source: source,
|
119
|
+
options: options,
|
120
|
+
transform: transpile
|
121
|
+
}, function (err, result) {
|
122
|
+
if (err) {
|
123
|
+
return callback(err);
|
124
|
+
}
|
125
|
+
return callback(null, result.code, result.map);
|
126
|
+
})
|
127
|
+
};
|
128
|
+
}();
|
129
|
+
|
130
|
+
if ((typeof _ret === "undefined" ? "undefined" : _typeof(_ret)) === "object") return _ret.v;
|
131
|
+
}
|
132
|
+
|
133
|
+
result = transpile(source, options);
|
134
|
+
this.callback(null, result.code, result.map);
|
135
|
+
};
|
package/lib/resolve-rc.js
CHANGED
@@ -1,37 +1,24 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
* changes.
|
7
|
-
*
|
8
|
-
* @see https://github.com/babel/babel-loader/issues/62
|
9
|
-
* @see http://git.io/vLEvu
|
10
|
-
*/
|
11
|
-
var fs = require('fs');
|
12
|
-
var path = require('path');
|
13
|
-
var assign = require('object-assign');
|
14
|
-
var exists = require('./helpers/exists')({});
|
15
|
-
var read = require('./helpers/read')({});
|
3
|
+
var path = require("path");
|
4
|
+
var exists = require("./utils/exists")({});
|
5
|
+
var read = require("./utils/read")({});
|
16
6
|
|
17
7
|
var find = function find(start, rel) {
|
18
8
|
var file = path.join(start, rel);
|
19
|
-
var opts = {};
|
20
|
-
var up = '';
|
21
9
|
|
22
10
|
if (exists(file)) {
|
23
11
|
return read(file);
|
24
12
|
}
|
25
13
|
|
26
|
-
up = path.dirname(start);
|
14
|
+
var up = path.dirname(start);
|
27
15
|
if (up !== start) {
|
28
|
-
// Reached root
|
29
16
|
return find(up, rel);
|
30
17
|
}
|
31
|
-
|
32
18
|
};
|
33
19
|
|
34
|
-
module.exports = function(loc, rel) {
|
35
|
-
rel = rel ||
|
20
|
+
module.exports = function (loc, rel) {
|
21
|
+
rel = rel || ".babelrc";
|
22
|
+
|
36
23
|
return find(loc, rel);
|
37
|
-
};
|
24
|
+
};
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var fs = require("fs");
|
4
|
+
|
5
|
+
module.exports = function (cache) {
|
6
|
+
cache = cache || {};
|
7
|
+
|
8
|
+
return function (filename) {
|
9
|
+
|
10
|
+
if (!filename) {
|
11
|
+
return false;
|
12
|
+
}
|
13
|
+
|
14
|
+
cache[filename] = cache[filename] || fs.existsSync(filename);
|
15
|
+
|
16
|
+
return cache[filename];
|
17
|
+
};
|
18
|
+
};
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var fs = require("fs");
|
4
|
+
|
5
|
+
module.exports = function (cache) {
|
6
|
+
cache = cache || {};
|
7
|
+
|
8
|
+
return function (filename) {
|
9
|
+
|
10
|
+
if (!filename) {
|
11
|
+
throw new Error("filename must be a string");
|
12
|
+
}
|
13
|
+
|
14
|
+
cache[filename] = cache[filename] || fs.readFileSync(filename, "utf8");
|
15
|
+
|
16
|
+
return cache[filename];
|
17
|
+
};
|
18
|
+
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var path = require("path");
|
4
|
+
|
5
|
+
module.exports = function relative(sourceRoot, filename) {
|
6
|
+
var rootPath = sourceRoot.replace(/\\/g, "/").split("/")[1];
|
7
|
+
var fileRootPath = filename.replace(/\\/g, "/").split("/")[1];
|
8
|
+
|
9
|
+
if (rootPath && rootPath !== fileRootPath) {
|
10
|
+
return filename;
|
11
|
+
}
|
12
|
+
|
13
|
+
return path.relative(sourceRoot, filename);
|
14
|
+
};
|
package/package.json
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "babel-loader",
|
3
|
-
"version": "6.2.
|
3
|
+
"version": "6.2.10",
|
4
4
|
"description": "babel module loader for webpack",
|
5
5
|
"files": [
|
6
|
-
"index.js",
|
7
6
|
"lib"
|
8
7
|
],
|
8
|
+
"main": "lib/index.js",
|
9
9
|
"dependencies": {
|
10
10
|
"find-cache-dir": "^0.1.1",
|
11
11
|
"loader-utils": "^0.2.11",
|
@@ -14,25 +14,37 @@
|
|
14
14
|
},
|
15
15
|
"peerDependencies": {
|
16
16
|
"babel-core": "^6.0.0",
|
17
|
-
"webpack": "1 || ^2.1.0-beta"
|
17
|
+
"webpack": "1 || 2 || ^2.1.0-beta || ^2.2.0-rc"
|
18
18
|
},
|
19
19
|
"devDependencies": {
|
20
|
+
"ava": "^0.17.0",
|
21
|
+
"babel-cli": "^6.18.0",
|
20
22
|
"babel-core": "^6.0.0",
|
23
|
+
"babel-eslint": "^7.1.0",
|
24
|
+
"babel-plugin-istanbul": "^3.0.0",
|
21
25
|
"babel-preset-es2015": "^6.0.0",
|
22
|
-
"
|
23
|
-
"
|
24
|
-
"
|
25
|
-
"
|
26
|
-
"
|
27
|
-
"
|
26
|
+
"babel-preset-latest": "^6.16.0",
|
27
|
+
"babel-register": "^6.18.0",
|
28
|
+
"codecov": "^1.0.1",
|
29
|
+
"cross-env": "^2.0.0",
|
30
|
+
"eslint": "^3.8.1",
|
31
|
+
"eslint-config-babel": "^3.0.0",
|
32
|
+
"eslint-plugin-babel": "^4.0.0",
|
33
|
+
"eslint-plugin-flowtype": "^2.25.0",
|
34
|
+
"nyc": "^10.0.0",
|
35
|
+
"rimraf": "^2.4.3",
|
36
|
+
"webpack": "^2.2.0-rc"
|
28
37
|
},
|
29
38
|
"scripts": {
|
30
|
-
"
|
31
|
-
"
|
32
|
-
"
|
33
|
-
"
|
34
|
-
"
|
35
|
-
"
|
39
|
+
"clean": "rimraf lib/",
|
40
|
+
"build": "babel src/ --out-dir lib/",
|
41
|
+
"coverage": "nyc report --reporter=json && codecov -f coverage/coverage-final.json",
|
42
|
+
"lint": "eslint src test",
|
43
|
+
"preversion": "npm test",
|
44
|
+
"prepublish": "npm run clean && npm run build",
|
45
|
+
"test": "npm run lint && cross-env BABEL_ENV=test npm run build && npm run test-only",
|
46
|
+
"test-ci": "cross-env BABEL_ENV=test npm run build && npm run test-only",
|
47
|
+
"test-only": "nyc ava"
|
36
48
|
},
|
37
49
|
"repository": {
|
38
50
|
"type": "git",
|
@@ -41,7 +53,6 @@
|
|
41
53
|
"keywords": [
|
42
54
|
"webpack",
|
43
55
|
"loader",
|
44
|
-
"6to5",
|
45
56
|
"babel",
|
46
57
|
"es6",
|
47
58
|
"transpiler",
|
@@ -52,5 +63,27 @@
|
|
52
63
|
"bugs": {
|
53
64
|
"url": "https://github.com/babel/babel-loader/issues"
|
54
65
|
},
|
55
|
-
"homepage": "https://github.com/babel/babel-loader"
|
66
|
+
"homepage": "https://github.com/babel/babel-loader",
|
67
|
+
"nyc": {
|
68
|
+
"all": true,
|
69
|
+
"include": [
|
70
|
+
"src/**/*.js"
|
71
|
+
],
|
72
|
+
"require": [
|
73
|
+
"babel-register"
|
74
|
+
],
|
75
|
+
"sourceMap": false,
|
76
|
+
"instrument": false
|
77
|
+
},
|
78
|
+
"ava": {
|
79
|
+
"files": [
|
80
|
+
"test/**/*.test.js",
|
81
|
+
"!test/fixtures/**/*",
|
82
|
+
"!test/helpers/**/*"
|
83
|
+
],
|
84
|
+
"source": [
|
85
|
+
"src/**/*.js"
|
86
|
+
],
|
87
|
+
"babel": "inherit"
|
88
|
+
}
|
56
89
|
}
|
package/index.js
DELETED
@@ -1,133 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
var assign = require('object-assign');
|
4
|
-
var babel = require('babel-core');
|
5
|
-
var loaderUtils = require('loader-utils');
|
6
|
-
var cache = require('./lib/fs-cache.js');
|
7
|
-
var exists = require('./lib/helpers/exists')();
|
8
|
-
var read = require('./lib/helpers/read')();
|
9
|
-
var resolveRc = require('./lib/resolve-rc.js');
|
10
|
-
var pkg = require('./package.json');
|
11
|
-
var path = require('path');
|
12
|
-
|
13
|
-
/**
|
14
|
-
* Error thrown by Babel formatted to conform to Webpack reporting.
|
15
|
-
*/
|
16
|
-
function BabelLoaderError(name, message, codeFrame, hideStack, error) {
|
17
|
-
Error.call(this);
|
18
|
-
Error.captureStackTrace(this, BabelLoaderError);
|
19
|
-
|
20
|
-
this.name = 'BabelLoaderError';
|
21
|
-
this.message = formatMessage(name, message, codeFrame);
|
22
|
-
this.hideStack = hideStack;
|
23
|
-
this.error = error;
|
24
|
-
}
|
25
|
-
|
26
|
-
BabelLoaderError.prototype = Object.create(Error.prototype);
|
27
|
-
BabelLoaderError.prototype.constructor = BabelLoaderError;
|
28
|
-
|
29
|
-
var STRIP_FILENAME_RE = /^[^:]+: /;
|
30
|
-
|
31
|
-
var formatMessage = function(name, message, codeFrame) {
|
32
|
-
return (name ? name + ': ' : '') + message + '\n\n' + codeFrame + '\n';
|
33
|
-
};
|
34
|
-
|
35
|
-
var transpile = function(source, options) {
|
36
|
-
var result;
|
37
|
-
try {
|
38
|
-
result = babel.transform(source, options);
|
39
|
-
} catch (error) {
|
40
|
-
if (error.message && error.codeFrame) {
|
41
|
-
var message = error.message;
|
42
|
-
var name;
|
43
|
-
var hideStack;
|
44
|
-
if (error instanceof SyntaxError) {
|
45
|
-
message = message.replace(STRIP_FILENAME_RE, '');
|
46
|
-
name = 'SyntaxError';
|
47
|
-
hideStack = true;
|
48
|
-
} else if (error instanceof TypeError) {
|
49
|
-
message = message.replace(STRIP_FILENAME_RE, '');
|
50
|
-
hideStack = true;
|
51
|
-
}
|
52
|
-
throw new BabelLoaderError(
|
53
|
-
name, message, error.codeFrame, hideStack, error);
|
54
|
-
} else {
|
55
|
-
throw error;
|
56
|
-
}
|
57
|
-
}
|
58
|
-
var code = result.code;
|
59
|
-
var map = result.map;
|
60
|
-
|
61
|
-
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
|
62
|
-
map.sourcesContent = [source];
|
63
|
-
}
|
64
|
-
|
65
|
-
return {
|
66
|
-
code: code,
|
67
|
-
map: map,
|
68
|
-
};
|
69
|
-
};
|
70
|
-
|
71
|
-
module.exports = function(source, inputSourceMap) {
|
72
|
-
var result = {};
|
73
|
-
|
74
|
-
// Handle filenames (#106)
|
75
|
-
var webpackRemainingChain = loaderUtils.getRemainingRequest(this).split('!');
|
76
|
-
var filename = webpackRemainingChain[webpackRemainingChain.length - 1];
|
77
|
-
|
78
|
-
// Handle options
|
79
|
-
var globalOptions = this.options.babel || {};
|
80
|
-
var loaderOptions = loaderUtils.parseQuery(this.query);
|
81
|
-
var userOptions = assign({}, globalOptions, loaderOptions);
|
82
|
-
var defaultOptions = {
|
83
|
-
inputSourceMap: inputSourceMap,
|
84
|
-
sourceRoot: process.cwd(),
|
85
|
-
filename: filename,
|
86
|
-
cacheIdentifier: JSON.stringify({
|
87
|
-
'babel-loader': pkg.version,
|
88
|
-
'babel-core': babel.version,
|
89
|
-
babelrc: exists(userOptions.babelrc) ?
|
90
|
-
read(userOptions.babelrc) :
|
91
|
-
resolveRc(path.dirname(filename)),
|
92
|
-
env: process.env.BABEL_ENV || process.env.NODE_ENV,
|
93
|
-
}),
|
94
|
-
};
|
95
|
-
|
96
|
-
var options = assign({}, defaultOptions, userOptions);
|
97
|
-
|
98
|
-
if (userOptions.sourceMap === undefined) {
|
99
|
-
options.sourceMap = this.sourceMap;
|
100
|
-
}
|
101
|
-
|
102
|
-
if (options.sourceFileName === undefined) {
|
103
|
-
options.sourceFileName = path.relative(
|
104
|
-
options.sourceRoot,
|
105
|
-
options.filename
|
106
|
-
);
|
107
|
-
}
|
108
|
-
|
109
|
-
var cacheDirectory = options.cacheDirectory;
|
110
|
-
var cacheIdentifier = options.cacheIdentifier;
|
111
|
-
|
112
|
-
delete options.cacheDirectory;
|
113
|
-
delete options.cacheIdentifier;
|
114
|
-
|
115
|
-
this.cacheable();
|
116
|
-
|
117
|
-
if (cacheDirectory) {
|
118
|
-
var callback = this.async();
|
119
|
-
return cache({
|
120
|
-
directory: cacheDirectory,
|
121
|
-
identifier: cacheIdentifier,
|
122
|
-
source: source,
|
123
|
-
options: options,
|
124
|
-
transform: transpile,
|
125
|
-
}, function(err, result) {
|
126
|
-
if (err) { return callback(err); }
|
127
|
-
return callback(null, result.code, result.map);
|
128
|
-
});
|
129
|
-
}
|
130
|
-
|
131
|
-
result = transpile(source, options);
|
132
|
-
this.callback(null, result.code, result.map);
|
133
|
-
};
|
package/lib/helpers/exists.js
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
var fs = require('fs');
|
4
|
-
/**
|
5
|
-
* Check if file exists and cache the result
|
6
|
-
* return the result in cache
|
7
|
-
*
|
8
|
-
* @example
|
9
|
-
* var exists = require('./helpers/fsExists')({});
|
10
|
-
* exists('.babelrc'); // false
|
11
|
-
*/
|
12
|
-
module.exports = function(cache) {
|
13
|
-
cache = cache || {};
|
14
|
-
|
15
|
-
return function(filename) {
|
16
|
-
|
17
|
-
if (!filename) { return false; }
|
18
|
-
|
19
|
-
cache[filename] = cache[filename] || fs.existsSync(filename);
|
20
|
-
|
21
|
-
return cache[filename];
|
22
|
-
};
|
23
|
-
};
|
package/lib/helpers/read.js
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
var fs = require('fs');
|
4
|
-
/**
|
5
|
-
* Read the file and cache the result
|
6
|
-
* return the result in cache
|
7
|
-
*
|
8
|
-
* @example
|
9
|
-
* var read = require('./helpers/fsExists')({});
|
10
|
-
* read('.babelrc'); // file contents...
|
11
|
-
*/
|
12
|
-
module.exports = function(cache) {
|
13
|
-
cache = cache || {};
|
14
|
-
|
15
|
-
return function(filename) {
|
16
|
-
|
17
|
-
if (!filename) {
|
18
|
-
throw new Error('filename must be a string');
|
19
|
-
}
|
20
|
-
|
21
|
-
cache[filename] = cache[filename] || fs.readFileSync(filename, 'utf8');
|
22
|
-
|
23
|
-
return cache[filename];
|
24
|
-
};
|
25
|
-
};
|