babel-loader 8.0.0-beta.5 → 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,36 +7,37 @@
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
 
25
- > webpack 3.x | babel-loader 8.x | babel 7.x
26
+ > webpack 4.x | babel-loader 8.x | babel 7.x
26
27
 
27
28
  ```bash
28
- npm install "babel-loader@^8.0.0-beta" @babel/core @babel/preset-env webpack
29
+ npm install -D babel-loader @babel/core @babel/preset-env webpack
29
30
  ```
30
31
 
31
- > webpack 3.x babel-loader 7.x | babel 6.x
32
+ > webpack 4.x | babel-loader 7.x | babel 6.x
32
33
 
33
34
  ```bash
34
- npm install babel-loader babel-core babel-preset-env webpack
35
+ npm install -D babel-loader@7 babel-core babel-preset-env webpack
35
36
  ```
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,46 +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
- * `babelrc`: Default `true`. When `false`, no options from `.babelrc` files will be used; only the options passed to
92
- `babel-loader` will be used.
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.
93
92
 
94
- __Note:__ The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).
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).
95
94
 
96
95
  ## Troubleshooting
97
96
 
98
97
  ### babel-loader is slow!
99
98
 
100
- Make sure you are transforming as few files as possible. Because you are probably
101
- matching `/\.js$/`, you might be transforming the `node_modules` folder or other unwanted
102
- 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.
103
100
 
104
101
  To exclude `node_modules`, see the `exclude` option in the `loaders` config as documented above.
105
102
 
106
- You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option.
107
- 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.
108
104
 
109
- ### babel is injecting helpers into each file and bloating my code!
105
+ ### Babel is injecting helpers into each file and bloating my code!
110
106
 
111
- babel uses very small helpers for common functions such as `_extend`. By default
112
- 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.
113
108
 
114
- 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.
115
110
 
116
- The following configuration disables automatic per-file runtime injection in babel, instead
117
- 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.
118
112
 
119
- See the [docs](http://babeljs.io/docs/plugins/transform-runtime/) for more information.
113
+ See the [docs](https://babeljs.io/docs/plugins/transform-runtime/) for more information.
120
114
 
121
- **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`.
122
116
 
123
117
  ```javascript
124
118
  rules: [
125
- // the 'transform-runtime' plugin tells babel to require the runtime
126
- // instead of inlining it.
119
+ // the 'transform-runtime' plugin tells Babel to
120
+ // require the runtime instead of inlining it.
127
121
  {
128
122
  test: /\.js$/,
129
123
  exclude: /(node_modules|bower_components)/,
@@ -138,9 +132,9 @@ rules: [
138
132
  ]
139
133
  ```
140
134
 
141
- #### **NOTE:** transform-runtime & custom polyfills (e.g. Promise library)
135
+ #### **NOTE**: transform-runtime & custom polyfills (e.g. Promise library)
142
136
 
143
- 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:
144
138
 
145
139
  ```javascript
146
140
  // ...
@@ -184,21 +178,21 @@ require('@babel/runtime/core-js/promise').default = require('bluebird');
184
178
  require('./app');
185
179
  ```
186
180
 
187
- ### The node API for `babel` has been moved to `babel-core`.
181
+ ### The Node.js API for `babel` has been moved to `babel-core`.
188
182
 
189
- 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):
190
- ```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
191
185
  {
192
186
  test: /\.js$/,
193
187
  loader: 'babel',
194
188
  }
195
189
  ```
196
190
 
197
- 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`.
198
192
 
199
- 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`.)
200
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:
201
- ```js
195
+ ```javascript
202
196
  {
203
197
  test: /\.js$/,
204
198
  loader: 'babel-loader',
@@ -281,4 +275,4 @@ be passed to `babel.transform`.
281
275
  Given Babel's result object, allow loaders to make additional tweaks to it.
282
276
 
283
277
 
284
- ## [License](http://couto.mit-license.org/)
278
+ ## [License](https://couto.mit-license.org/)
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
  }();
@@ -89,14 +89,14 @@ function () {
89
89
 
90
90
 
91
91
  const filename = function (source, identifier, options) {
92
- const hash = crypto.createHash("SHA1");
92
+ const hash = crypto.createHash("md4");
93
93
  const contents = JSON.stringify({
94
94
  source,
95
95
  options,
96
96
  identifier
97
97
  });
98
- hash.end(contents);
99
- return hash.read().toString("hex") + ".json.gz";
98
+ hash.update(contents);
99
+ return hash.digest("hex") + ".json";
100
100
  };
101
101
  /**
102
102
  * Handle the cache
@@ -114,8 +114,17 @@ function () {
114
114
  source,
115
115
  options = {},
116
116
  cacheIdentifier,
117
- cacheDirectory
117
+ cacheDirectory,
118
+ cacheCompression
118
119
  } = params;
120
+ const file = path.join(directory, filename(source, cacheIdentifier, options));
121
+
122
+ try {
123
+ // No errors mean that the file was previously cached
124
+ // we just need to return it
125
+ return yield read(file, cacheCompression);
126
+ } catch (err) {}
127
+
119
128
  const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
120
129
 
121
130
  try {
@@ -126,22 +135,14 @@ function () {
126
135
  }
127
136
 
128
137
  throw err;
129
- }
130
-
131
- const file = path.join(directory, filename(source, cacheIdentifier, options));
132
-
133
- try {
134
- // No errors mean that the file was previously cached
135
- // we just need to return it
136
- return yield read(file);
137
- } catch (err) {} // Otherwise just transform the file
138
+ } // Otherwise just transform the file
138
139
  // return it to the user asap and write it in cache
139
140
 
140
141
 
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
@@ -2,7 +2,23 @@
2
2
 
3
3
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _next(value) { step("next", value); } function _throw(err) { step("throw", err); } _next(); }); }; }
4
4
 
5
- const babel = require("@babel/core");
5
+ let babel;
6
+
7
+ try {
8
+ babel = require("@babel/core");
9
+ } catch (err) {
10
+ if (err.code === "MODULE_NOT_FOUND") {
11
+ err.message += "\n babel-loader@8 requires Babel 7.x (the package '@babel/core'). " + "If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.";
12
+ }
13
+
14
+ throw err;
15
+ } // Since we've got the reverse bridge package at @babel/core@6.x, give
16
+ // people useful feedback if they try to use it alongside babel-loader.
17
+
18
+
19
+ if (/^6\./.test(babel.version)) {
20
+ throw new Error("\n babel-loader@8 will not work with the '@babel/core@6' bridge package. " + "If you want to use Babel 6.x, install 'babel-loader@7'.");
21
+ }
6
22
 
7
23
  const pkg = require("../package.json");
8
24
 
@@ -10,8 +26,6 @@ const cache = require("./cache");
10
26
 
11
27
  const transform = require("./transform");
12
28
 
13
- const relative = require("./utils/relative");
14
-
15
29
  const loaderUtils = require("loader-utils");
16
30
 
17
31
  function subscribe(subscriber, metadata, context) {
@@ -55,24 +69,33 @@ function _loader() {
55
69
 
56
70
  if (typeof loaderOptions.babelrc === "string") {
57
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");
58
- } // 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
+
59
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
+ }
60
83
 
61
- const {
62
- sourceRoot = process.cwd(),
63
- sourceMap = this.sourceMap,
64
- sourceFileName = relative(sourceRoot, filename)
65
- } = loaderOptions;
66
84
  const programmaticOptions = Object.assign({}, loaderOptions, {
67
85
  filename,
68
86
  inputSourceMap: inputSourceMap || undefined,
69
- sourceRoot,
70
- sourceMap,
71
- 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
72
94
  }); // Remove loader related options
73
95
 
74
96
  delete programmaticOptions.cacheDirectory;
75
97
  delete programmaticOptions.cacheIdentifier;
98
+ delete programmaticOptions.cacheCompression;
76
99
  delete programmaticOptions.metadataSubscribers;
77
100
 
78
101
  if (!babel.loadPartialConfig) {
@@ -91,6 +114,16 @@ function _loader() {
91
114
  });
92
115
  }
93
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
+
94
127
  const {
95
128
  cacheDirectory = null,
96
129
  cacheIdentifier = JSON.stringify({
@@ -98,6 +131,7 @@ function _loader() {
98
131
  "@babel/core": transform.version,
99
132
  "@babel/loader": pkg.version
100
133
  }),
134
+ cacheCompression = true,
101
135
  metadataSubscribers = []
102
136
  } = loaderOptions;
103
137
  let result;
@@ -108,7 +142,8 @@ function _loader() {
108
142
  options,
109
143
  transform,
110
144
  cacheDirectory,
111
- cacheIdentifier
145
+ cacheIdentifier,
146
+ cacheCompression
112
147
  });
113
148
  } else {
114
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.0-beta.5",
3
+ "version": "8.0.2",
4
4
  "description": "babel module loader for webpack",
5
5
  "files": [
6
6
  "lib"
@@ -16,7 +16,7 @@
16
16
  "util.promisify": "^1.0.0"
17
17
  },
18
18
  "peerDependencies": {
19
- "@babel/core": "^7.0.0 || ^7.0.0-rc || ^7.0.0-beta.41",
19
+ "@babel/core": "^7.0.0",
20
20
  "webpack": ">=2"
21
21
  },
22
22
  "devDependencies": {
@@ -53,9 +53,6 @@
53
53
  "test": "yarn run lint && cross-env BABEL_ENV=test yarn run build && yarn run test-only",
54
54
  "test-only": "nyc ava"
55
55
  },
56
- "publishConfig": {
57
- "tag": "next"
58
- },
59
56
  "repository": {
60
57
  "type": "git",
61
58
  "url": "https://github.com/babel/babel-loader.git"
@@ -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
- };