babel-loader 6.2.9 → 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/lib/fs-cache.js CHANGED
@@ -1,14 +1,5 @@
1
1
  "use strict";
2
2
 
3
- /**
4
- * Filesystem cache
5
- *
6
- * Given a file and a transform function, cache the result into files
7
- * or retrieve the previously cached files if the given file is already known.
8
- *
9
- * @see https://github.com/babel/babel-loader/issues/34
10
- * @see https://github.com/babel/babel-loader/pull/41
11
- */
12
3
  var crypto = require("crypto");
13
4
  var mkdirp = require("mkdirp");
14
5
  var findCacheDir = require("find-cache-dir");
@@ -17,13 +8,6 @@ var os = require("os");
17
8
  var path = require("path");
18
9
  var zlib = require("zlib");
19
10
 
20
- /**
21
- * Read the contents from the compressed file.
22
- *
23
- * @async
24
- * @params {String} filename
25
- * @params {Function} callback
26
- */
27
11
  var read = function read(filename, callback) {
28
12
  return fs.readFile(filename, function (err, data) {
29
13
  if (err) {
@@ -48,14 +32,6 @@ var read = function read(filename, callback) {
48
32
  });
49
33
  };
50
34
 
51
- /**
52
- * Write contents into a compressed file.
53
- *
54
- * @async
55
- * @params {String} filename
56
- * @params {String} result
57
- * @params {Function} callback
58
- */
59
35
  var write = function write(filename, result, callback) {
60
36
  var content = JSON.stringify(result);
61
37
 
@@ -68,14 +44,6 @@ var write = function write(filename, result, callback) {
68
44
  });
69
45
  };
70
46
 
71
- /**
72
- * Build the filename for the cached file
73
- *
74
- * @params {String} source File source code
75
- * @params {Object} options Options used
76
- *
77
- * @return {String}
78
- */
79
47
  var filename = function filename(source, identifier, options) {
80
48
  var hash = crypto.createHash("SHA1");
81
49
  var contents = JSON.stringify({
@@ -89,72 +57,23 @@ var filename = function filename(source, identifier, options) {
89
57
  return hash.read().toString("hex") + ".json.gz";
90
58
  };
91
59
 
92
- /**
93
- * Retrieve file from cache, or create a new one for future reads
94
- *
95
- * @async
96
- * @param {Object} params
97
- * @param {String} params.directory Directory to store cached files
98
- * @param {String} params.identifier Unique identifier to bust cache
99
- * @param {String} params.source Original contents of the file to be cached
100
- * @param {Object} params.options Options to be given to the transform fn
101
- * @param {Function} params.transform Function that will transform the
102
- * original file and whose result will be
103
- * cached
104
- *
105
- * @param {Function<err, result>} callback
106
- *
107
- * @example
108
- *
109
- * cache({
110
- * directory: '.tmp/cache',
111
- * identifier: 'babel-loader-cachefile',
112
- * source: *source code from file*,
113
- * options: {
114
- * experimental: true,
115
- * runtime: true
116
- * },
117
- * transform: function(source, options) {
118
- * var content = *do what you need with the source*
119
- * return content;
120
- * }
121
- * }, function(err, result) {
122
- *
123
- * });
124
- */
125
- module.exports = function (params, callback) {
126
- // Spread params into named variables
127
- // Forgive user whenever possible
60
+ var handleCache = function handleCache(directory, params, callback) {
128
61
  var source = params.source;
129
62
  var options = params.options || {};
130
63
  var transform = params.transform;
131
64
  var identifier = params.identifier;
132
- var directory = void 0;
65
+ var shouldFallback = typeof params.directory !== "string" && directory !== os.tmpdir();
133
66
 
134
- if (typeof params.directory === "string") {
135
- directory = params.directory;
136
- } else {
137
- directory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
138
- }
67
+ mkdirp(directory, function (err) {
68
+ if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
139
69
 
140
- var file = path.join(directory, filename(source, identifier, options));
141
-
142
- // Make sure the directory exists.
143
- return mkdirp(directory, function (err) {
144
- if (err) {
145
- return callback(err);
146
- }
70
+ var file = path.join(directory, filename(source, identifier, options));
147
71
 
148
72
  return read(file, function (err, content) {
149
73
  var result = {};
150
- // No errors mean that the file was previously cached
151
- // we just need to return it
152
- if (!err) {
153
- return callback(null, content);
154
- }
155
74
 
156
- // Otherwise just transform the file
157
- // return it to the user asap and write it in cache
75
+ if (!err) return callback(null, content);
76
+
158
77
  try {
159
78
  result = transform(source, options);
160
79
  } catch (error) {
@@ -162,8 +81,22 @@ module.exports = function (params, callback) {
162
81
  }
163
82
 
164
83
  return write(file, result, function (err) {
165
- return callback(err, result);
84
+ if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
85
+
86
+ callback(null, result);
166
87
  });
167
88
  });
168
89
  });
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);
169
102
  };
package/lib/index.js CHANGED
@@ -13,9 +13,6 @@ var read = require("./utils/read")();
13
13
  var resolveRc = require("./resolve-rc.js");
14
14
  var pkg = require("./../package.json");
15
15
 
16
- /**
17
- * Error thrown by Babel formatted to conform to Webpack reporting.
18
- */
19
16
  function BabelLoaderError(name, message, codeFrame, hideStack, error) {
20
17
  Error.call(this);
21
18
  Error.captureStackTrace(this, BabelLoaderError);
@@ -75,11 +72,9 @@ module.exports = function (source, inputSourceMap) {
75
72
 
76
73
  var result = {};
77
74
 
78
- // Handle filenames (#106)
79
75
  var webpackRemainingChain = loaderUtils.getRemainingRequest(this).split("!");
80
76
  var filename = webpackRemainingChain[webpackRemainingChain.length - 1];
81
77
 
82
- // Handle options
83
78
  var globalOptions = this.options.babel || {};
84
79
  var loaderOptions = loaderUtils.parseQuery(this.query);
85
80
  var userOptions = assign({}, globalOptions, loaderOptions);
package/lib/resolve-rc.js CHANGED
@@ -1,13 +1,5 @@
1
1
  "use strict";
2
2
 
3
- /**
4
- * The purpose of this module, is to find the project's .babelrc and
5
- * use its contents to bust the babel-loader's internal cache whenever an option
6
- * changes.
7
- *
8
- * @see https://github.com/babel/babel-loader/issues/62
9
- * @see http://git.io/vLEvu
10
- */
11
3
  var path = require("path");
12
4
  var exists = require("./utils/exists")({});
13
5
  var read = require("./utils/read")({});
@@ -21,7 +13,6 @@ var find = function find(start, rel) {
21
13
 
22
14
  var up = path.dirname(start);
23
15
  if (up !== start) {
24
- // Reached root
25
16
  return find(up, rel);
26
17
  }
27
18
  };
@@ -1,14 +1,7 @@
1
1
  "use strict";
2
2
 
3
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
- */
4
+
12
5
  module.exports = function (cache) {
13
6
  cache = cache || {};
14
7
 
package/lib/utils/read.js CHANGED
@@ -1,14 +1,7 @@
1
1
  "use strict";
2
2
 
3
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
- */
4
+
12
5
  module.exports = function (cache) {
13
6
  cache = cache || {};
14
7
 
@@ -6,7 +6,6 @@ module.exports = function relative(sourceRoot, filename) {
6
6
  var rootPath = sourceRoot.replace(/\\/g, "/").split("/")[1];
7
7
  var fileRootPath = filename.replace(/\\/g, "/").split("/")[1];
8
8
 
9
- // If the file is in a completely different root folder use the absolute path of file.
10
9
  if (rootPath && rootPath !== fileRootPath) {
11
10
  return filename;
12
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-loader",
3
- "version": "6.2.9",
3
+ "version": "6.2.10",
4
4
  "description": "babel module loader for webpack",
5
5
  "files": [
6
6
  "lib"
@@ -14,7 +14,7 @@
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
20
  "ava": "^0.17.0",
@@ -33,14 +33,15 @@
33
33
  "eslint-plugin-flowtype": "^2.25.0",
34
34
  "nyc": "^10.0.0",
35
35
  "rimraf": "^2.4.3",
36
- "webpack": "^2.1.0-beta.22"
36
+ "webpack": "^2.2.0-rc"
37
37
  },
38
38
  "scripts": {
39
+ "clean": "rimraf lib/",
39
40
  "build": "babel src/ --out-dir lib/",
40
41
  "coverage": "nyc report --reporter=json && codecov -f coverage/coverage-final.json",
41
42
  "lint": "eslint src test",
42
43
  "preversion": "npm test",
43
- "prepublish": "npm run build",
44
+ "prepublish": "npm run clean && npm run build",
44
45
  "test": "npm run lint && cross-env BABEL_ENV=test npm run build && npm run test-only",
45
46
  "test-ci": "cross-env BABEL_ENV=test npm run build && npm run test-only",
46
47
  "test-only": "nyc ava"