babel-loader 8.0.1 → 8.0.2

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- > This readme is for babel-loader v8 + Babel v7
1
+ > This README is for babel-loader v8 + Babel v7
2
2
  > Check the [7.x branch](https://github.com/babel/babel-loader/tree/7.x) for docs with Babel v6
3
3
 
4
4
  [![NPM Status](https://img.shields.io/npm/v/babel-loader.svg?style=flat)](https://www.npmjs.com/package/babel-loader)
@@ -7,18 +7,19 @@
7
7
  [![codecov](https://codecov.io/gh/babel/babel-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/babel/babel-loader)
8
8
 
9
9
  <div align="center">
10
- <a href="https://github.com/babel/babel/">
11
- <img width="200" height="200" src="https://rawgit.com/babel/logo/master/babel.svg">
10
+ <a href="https://github.com/babel/babel">
11
+ <img src="https://rawgit.com/babel/logo/master/babel.svg" alt="Babel logo" width="200" height="200">
12
12
  </a>
13
13
  <a href="https://github.com/webpack/webpack">
14
- <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
14
+ <img src="https://webpack.js.org/assets/icon-square-big.svg" alt="webpack logo" width="200" height="200">
15
15
  </a>
16
- <h1>Babel Loader</h1>
17
16
  </div>
18
17
 
18
+ <h1 align="center">Babel Loader</h1>
19
+
19
20
  This package allows transpiling JavaScript files using [Babel](https://github.com/babel/babel) and [webpack](https://github.com/webpack/webpack).
20
21
 
21
- __Notes:__ Issues with the output should be reported on the babel [issue tracker](https://github.com/babel/babel/issues).
22
+ **Note**: Issues with the output should be reported on the Babel [Issues](https://github.com/babel/babel/issues) tracker.
22
23
 
23
24
  <h2 align="center">Install</h2>
24
25
 
@@ -36,7 +37,7 @@ npm install -D babel-loader@7 babel-core babel-preset-env webpack
36
37
 
37
38
  <h2 align="center">Usage</h2>
38
39
 
39
- [Documentation: Using loaders](https://webpack.js.org/loaders/)
40
+ webpack documentation: [Loaders](https://webpack.js.org/loaders/)
40
41
 
41
42
  Within your webpack configuration object, you'll need to add the babel-loader to the list of modules, like so:
42
43
 
@@ -61,8 +62,7 @@ module: {
61
62
 
62
63
  See the `babel` [options](https://babeljs.io/docs/usage/api/#options).
63
64
 
64
-
65
- You can pass options to the loader by using the [options property](https://webpack.js.org/configuration/module/#rule-options-rule-query):
65
+ You can pass options to the loader by using the [`options`](https://webpack.js.org/configuration/module/#rule-options-rule-query) property:
66
66
 
67
67
  ```javascript
68
68
  module: {
@@ -84,43 +84,40 @@ module: {
84
84
 
85
85
  This loader also supports the following loader-specific option:
86
86
 
87
- * `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.
87
+ * `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.
88
88
 
89
- * `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.
89
+ * `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.
90
90
 
91
- __Note:__ The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).
91
+ * `cacheCompression`: Default `true`. When set, each Babel transform output will be compressed with Gzip. If you want to opt-out of cache compression, set it to `false` -- your project may benefit from this if it transpiles thousands of files.
92
+
93
+ **Note**: The `sourceMap` option is ignored. Instead, source maps are automatically enabled when webpack is configured to use them (via the [`devtool`](https://webpack.js.org/configuration/devtool/#devtool) config option).
92
94
 
93
95
  ## Troubleshooting
94
96
 
95
97
  ### babel-loader is slow!
96
98
 
97
- Make sure you are transforming as few files as possible. Because you are probably
98
- matching `/\.js$/`, you might be transforming the `node_modules` folder or other unwanted
99
- source.
99
+ 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.
100
100
 
101
101
  To exclude `node_modules`, see the `exclude` option in the `loaders` config as documented above.
102
102
 
103
- You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option.
104
- This will cache transformations to the filesystem.
103
+ You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option. This will cache transformations to the filesystem.
105
104
 
106
- ### babel is injecting helpers into each file and bloating my code!
105
+ ### Babel is injecting helpers into each file and bloating my code!
107
106
 
108
- babel uses very small helpers for common functions such as `_extend`. By default
109
- this will be added to every file that requires it.
107
+ Babel uses very small helpers for common functions such as `_extend`. By default, this will be added to every file that requires it.
110
108
 
111
- You can instead require the babel runtime as a separate module to avoid the duplication.
109
+ You can instead require the Babel runtime as a separate module to avoid the duplication.
112
110
 
113
- The following configuration disables automatic per-file runtime injection in babel, instead
114
- requiring `babel-plugin-transform-runtime` and making all helper references use it.
111
+ The following configuration disables automatic per-file runtime injection in Babel, requiring `babel-plugin-transform-runtime` instead and making all helper references use it.
115
112
 
116
113
  See the [docs](https://babeljs.io/docs/plugins/transform-runtime/) for more information.
117
114
 
118
- **NOTE:** You must run `npm install @babel/plugin-transform-runtime --save-dev` to include this in your project and `babel-runtime` itself as a dependency with `npm install @babel/runtime --save`.
115
+ **NOTE**: You must run `npm install -D @babel/plugin-transform-runtime` to include this in your project and `babel-runtime` itself as a dependency with `npm install @babel/runtime`.
119
116
 
120
117
  ```javascript
121
118
  rules: [
122
- // the 'transform-runtime' plugin tells babel to require the runtime
123
- // instead of inlining it.
119
+ // the 'transform-runtime' plugin tells Babel to
120
+ // require the runtime instead of inlining it.
124
121
  {
125
122
  test: /\.js$/,
126
123
  exclude: /(node_modules|bower_components)/,
@@ -135,9 +132,9 @@ rules: [
135
132
  ]
136
133
  ```
137
134
 
138
- #### **NOTE:** transform-runtime & custom polyfills (e.g. Promise library)
135
+ #### **NOTE**: transform-runtime & custom polyfills (e.g. Promise library)
139
136
 
140
- 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:
137
+ 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:
141
138
 
142
139
  ```javascript
143
140
  // ...
@@ -181,21 +178,21 @@ require('@babel/runtime/core-js/promise').default = require('bluebird');
181
178
  require('./app');
182
179
  ```
183
180
 
184
- ### The node API for `babel` has been moved to `babel-core`.
181
+ ### The Node.js API for `babel` has been moved to `babel-core`.
185
182
 
186
- 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):
187
- ```js
183
+ If you receive this message, it means that you have the npm package `babel` installed and are using the short notation of the loader in the webpack config (which is not valid anymore as of webpack 2.x):
184
+ ```javascript
188
185
  {
189
186
  test: /\.js$/,
190
187
  loader: 'babel',
191
188
  }
192
189
  ```
193
190
 
194
- Webpack then tries to load the `babel` package instead of the `babel-loader`.
191
+ webpack then tries to load the `babel` package instead of the `babel-loader`.
195
192
 
196
- To fix this you should uninstall the npm package `babel` as it is deprecated in babel v6. (instead install `babel-cli` or `babel-core`)
193
+ To fix this, you should uninstall the npm package `babel`, as it is deprecated in Babel v6. (Instead, install `babel-cli` or `babel-core`.)
197
194
  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:
198
- ```js
195
+ ```javascript
199
196
  {
200
197
  test: /\.js$/,
201
198
  loader: 'babel-loader',
package/lib/cache.js CHANGED
@@ -46,13 +46,13 @@ const mkdirp = promisify(mkdirpOrig);
46
46
  const read =
47
47
  /*#__PURE__*/
48
48
  function () {
49
- var _ref = _asyncToGenerator(function* (filename) {
50
- const data = yield readFile(filename);
51
- const content = yield gunzip(data);
52
- return JSON.parse(content);
49
+ var _ref = _asyncToGenerator(function* (filename, compress) {
50
+ const data = yield readFile(filename + (compress ? ".gz" : ""));
51
+ const content = compress ? yield gunzip(data) : data;
52
+ return JSON.parse(content.toString());
53
53
  });
54
54
 
55
- return function read(_x) {
55
+ return function read(_x, _x2) {
56
56
  return _ref.apply(this, arguments);
57
57
  };
58
58
  }();
@@ -68,13 +68,13 @@ function () {
68
68
  const write =
69
69
  /*#__PURE__*/
70
70
  function () {
71
- var _ref2 = _asyncToGenerator(function* (filename, result) {
71
+ var _ref2 = _asyncToGenerator(function* (filename, compress, result) {
72
72
  const content = JSON.stringify(result);
73
- const data = yield gzip(content);
74
- return yield writeFile(filename, data);
73
+ const data = compress ? yield gzip(content) : content;
74
+ return yield writeFile(filename + (compress ? ".gz" : ""), data);
75
75
  });
76
76
 
77
- return function write(_x2, _x3) {
77
+ return function write(_x3, _x4, _x5) {
78
78
  return _ref2.apply(this, arguments);
79
79
  };
80
80
  }();
@@ -96,7 +96,7 @@ const filename = function (source, identifier, options) {
96
96
  identifier
97
97
  });
98
98
  hash.update(contents);
99
- return hash.digest("hex") + ".json.gz";
99
+ return hash.digest("hex") + ".json";
100
100
  };
101
101
  /**
102
102
  * Handle the cache
@@ -114,14 +114,15 @@ function () {
114
114
  source,
115
115
  options = {},
116
116
  cacheIdentifier,
117
- cacheDirectory
117
+ cacheDirectory,
118
+ cacheCompression
118
119
  } = params;
119
120
  const file = path.join(directory, filename(source, cacheIdentifier, options));
120
121
 
121
122
  try {
122
123
  // No errors mean that the file was previously cached
123
124
  // we just need to return it
124
- return yield read(file);
125
+ return yield read(file, cacheCompression);
125
126
  } catch (err) {}
126
127
 
127
128
  const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
@@ -141,7 +142,7 @@ function () {
141
142
  const result = yield transform(source, options);
142
143
 
143
144
  try {
144
- yield write(file, result);
145
+ yield write(file, cacheCompression, result);
145
146
  } catch (err) {
146
147
  if (fallback) {
147
148
  // Fallback to tmpdir if node_modules folder not writable
@@ -154,7 +155,7 @@ function () {
154
155
  return result;
155
156
  });
156
157
 
157
- return function handleCache(_x4, _x5) {
158
+ return function handleCache(_x6, _x7) {
158
159
  return _ref3.apply(this, arguments);
159
160
  };
160
161
  }();
@@ -176,6 +177,7 @@ function () {
176
177
  * cache({
177
178
  * directory: '.tmp/cache',
178
179
  * identifier: 'babel-loader-cachefile',
180
+ * cacheCompression: false,
179
181
  * source: *source code from file*,
180
182
  * options: {
181
183
  * experimental: true,
@@ -212,7 +214,7 @@ function () {
212
214
  return yield handleCache(directory, params);
213
215
  });
214
216
 
215
- return function (_x6) {
217
+ return function (_x8) {
216
218
  return _ref4.apply(this, arguments);
217
219
  };
218
220
  }();
package/lib/index.js CHANGED
@@ -26,8 +26,6 @@ const cache = require("./cache");
26
26
 
27
27
  const transform = require("./transform");
28
28
 
29
- const relative = require("./utils/relative");
30
-
31
29
  const loaderUtils = require("loader-utils");
32
30
 
33
31
  function subscribe(subscriber, metadata, context) {
@@ -71,24 +69,33 @@ function _loader() {
71
69
 
72
70
  if (typeof loaderOptions.babelrc === "string") {
73
71
  console.warn("The option `babelrc` should not be set to a string anymore in the babel-loader config. " + "Please update your configuration and set `babelrc` to true or false.\n" + "If you want to specify a specific babel config file to inherit config from " + "please use the `extends` option.\nFor more information about this options see " + "https://babeljs.io/docs/core-packages/#options");
74
- } // Set babel-loader's default options.
72
+ } // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
73
+ // users may safely use either one alongside our default use of
74
+ // 'this.sourceMap' below without getting error about conflicting aliases.
75
+
75
76
 
77
+ if (Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMap") && !Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMaps")) {
78
+ loaderOptions = Object.assign({}, loaderOptions, {
79
+ sourceMaps: loaderOptions.sourceMap
80
+ });
81
+ delete loaderOptions.sourceMap;
82
+ }
76
83
 
77
- const {
78
- sourceRoot = process.cwd(),
79
- sourceMap = this.sourceMap,
80
- sourceFileName = relative(sourceRoot, filename)
81
- } = loaderOptions;
82
84
  const programmaticOptions = Object.assign({}, loaderOptions, {
83
85
  filename,
84
86
  inputSourceMap: inputSourceMap || undefined,
85
- sourceRoot,
86
- sourceMap,
87
- sourceFileName
87
+ // Set the default sourcemap behavior based on Webpack's mapping flag,
88
+ // but allow users to override if they want.
89
+ sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
90
+ // Ensure that Webpack will get a full absolute path in the sourcemap
91
+ // so that it can properly map the module back to its internal cached
92
+ // modules.
93
+ sourceFileName: filename
88
94
  }); // Remove loader related options
89
95
 
90
96
  delete programmaticOptions.cacheDirectory;
91
97
  delete programmaticOptions.cacheIdentifier;
98
+ delete programmaticOptions.cacheCompression;
92
99
  delete programmaticOptions.metadataSubscribers;
93
100
 
94
101
  if (!babel.loadPartialConfig) {
@@ -107,6 +114,16 @@ function _loader() {
107
114
  });
108
115
  }
109
116
 
117
+ if (options.sourceMaps === "inline") {
118
+ // Babel has this weird behavior where if you set "inline", we
119
+ // inline the sourcemap, and set 'result.map = null'. This results
120
+ // in bad behavior from Babel since the maps get put into the code,
121
+ // which Webpack does not expect, and because the map we return to
122
+ // Webpack is null, which is also bad. To avoid that, we override the
123
+ // behavior here so "inline" just behaves like 'true'.
124
+ options.sourceMaps = true;
125
+ }
126
+
110
127
  const {
111
128
  cacheDirectory = null,
112
129
  cacheIdentifier = JSON.stringify({
@@ -114,6 +131,7 @@ function _loader() {
114
131
  "@babel/core": transform.version,
115
132
  "@babel/loader": pkg.version
116
133
  }),
134
+ cacheCompression = true,
117
135
  metadataSubscribers = []
118
136
  } = loaderOptions;
119
137
  let result;
@@ -124,7 +142,8 @@ function _loader() {
124
142
  options,
125
143
  transform,
126
144
  cacheDirectory,
127
- cacheIdentifier
145
+ cacheIdentifier,
146
+ cacheCompression
128
147
  });
129
148
  } else {
130
149
  result = yield transform(source, options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-loader",
3
- "version": "8.0.1",
3
+ "version": "8.0.2",
4
4
  "description": "babel module loader for webpack",
5
5
  "files": [
6
6
  "lib"
@@ -1,15 +0,0 @@
1
- "use strict";
2
-
3
- const path = require("path");
4
-
5
- module.exports = function relative(root, file) {
6
- const rootPath = root.replace(/\\/g, "/").split("/")[1];
7
- const filePath = file.replace(/\\/g, "/").split("/")[1]; // If the file is in a completely different root folder
8
- // use the absolute path of the file
9
-
10
- if (rootPath && rootPath !== filePath) {
11
- return file;
12
- }
13
-
14
- return path.relative(root, file);
15
- };