babel-loader 8.0.3 → 8.1.0

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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014-2016 Luís Couto <hello@luiscouto.pt>
1
+ Copyright (c) 2014-2019 Luís Couto <hello@luiscouto.pt>
2
2
 
3
3
  MIT License
4
4
 
package/README.md CHANGED
@@ -29,12 +29,6 @@ This package allows transpiling JavaScript files using [Babel](https://github.co
29
29
  npm install -D babel-loader @babel/core @babel/preset-env webpack
30
30
  ```
31
31
 
32
- > webpack 4.x | babel-loader 7.x | babel 6.x
33
-
34
- ```bash
35
- npm install -D babel-loader@7 babel-core babel-preset-env webpack
36
- ```
37
-
38
32
  <h2 align="center">Usage</h2>
39
33
 
40
34
  webpack documentation: [Loaders](https://webpack.js.org/loaders/)
@@ -74,7 +68,7 @@ module: {
74
68
  loader: 'babel-loader',
75
69
  options: {
76
70
  presets: ['@babel/preset-env'],
77
- plugins: [require('@babel/plugin-proposal-object-rest-spread')]
71
+ plugins: ['@babel/plugin-proposal-object-rest-spread']
78
72
  }
79
73
  }
80
74
  }
@@ -84,15 +78,13 @@ module: {
84
78
 
85
79
  This loader also supports the following loader-specific option:
86
80
 
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.
81
+ * `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 set to `true` in options (`{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
82
 
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.
83
+ * `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
84
 
91
85
  * `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
86
 
93
- * `customize`: Default `null`. The path of a module that exports a `custom` callback [like the one that you'd pass to `.custom()`](#customized-loader). Since you already have to make a new file to use this, it is recommended that you instead use `.custom` to create a wrapper loader. Only use this is you _must_ continue using `babel-loader` directly, but still want to customize.
94
-
95
- **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).
87
+ * `customize`: Default `null`. The path of a module that exports a `custom` callback [like the one that you'd pass to `.custom()`](#customized-loader). Since you already have to make a new file to use this, it is recommended that you instead use `.custom` to create a wrapper loader. Only use this if you _must_ continue using `babel-loader` directly, but still want to customize.
96
88
 
97
89
  ## Troubleshooting
98
90
 
@@ -110,11 +102,11 @@ Babel uses very small helpers for common functions such as `_extend`. By default
110
102
 
111
103
  You can instead require the Babel runtime as a separate module to avoid the duplication.
112
104
 
113
- The following configuration disables automatic per-file runtime injection in Babel, requiring `babel-plugin-transform-runtime` instead and making all helper references use it.
105
+ The following configuration disables automatic per-file runtime injection in Babel, requiring `@babel/plugin-transform-runtime` instead and making all helper references use it.
114
106
 
115
107
  See the [docs](https://babeljs.io/docs/plugins/transform-runtime/) for more information.
116
108
 
117
- **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`.
109
+ **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`.
118
110
 
119
111
  ```javascript
120
112
  rules: [
@@ -136,7 +128,7 @@ rules: [
136
128
 
137
129
  #### **NOTE**: transform-runtime & custom polyfills (e.g. Promise library)
138
130
 
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:
131
+ 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:
140
132
 
141
133
  ```javascript
142
134
  // ...
@@ -192,7 +184,7 @@ If you receive this message, it means that you have the npm package `babel` inst
192
184
 
193
185
  webpack then tries to load the `babel` package instead of the `babel-loader`.
194
186
 
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`.)
187
+ 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
188
  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
189
  ```javascript
198
190
  {
@@ -201,6 +193,37 @@ In the case one of your dependencies is installing `babel` and you cannot uninst
201
193
  }
202
194
  ```
203
195
 
196
+ ## Customize config based on webpack target
197
+
198
+ Webpack supports bundling multiple [targets](https://webpack.js.org/concepts/targets/). For cases where you may want different Babel configurations for each target (like `web` _and_ `node`), this loader provides a `target` property via Babel's [caller](https://babeljs.io/docs/en/config-files#apicallercb) API.
199
+
200
+ For example, to change the environment targets passed to `@babel/preset-env` based on the webpack target:
201
+
202
+ ```javascript
203
+ // babel.config.js
204
+
205
+ module.exports = api => {
206
+ return {
207
+ plugins: [
208
+ "@babel/plugin-proposal-nullish-coalescing-operator",
209
+ "@babel/plugin-proposal-optional-chaining"
210
+ ],
211
+ presets: [
212
+ [
213
+ "@babel/preset-env",
214
+ {
215
+ useBuiltIns: "entry",
216
+ // caller.target will be the same as the target option from webpack
217
+ targets: api.caller(caller => caller && caller.target === "node")
218
+ ? { node: "current" }
219
+ : { chrome: "58", ie: "11" }
220
+ }
221
+ ]
222
+ ]
223
+ }
224
+ }
225
+ ```
226
+
204
227
  ## Customized Loader
205
228
 
206
229
  `babel-loader` exposes a loader-builder utility that allows users to add custom handling
@@ -296,4 +319,5 @@ be passed to `babel.transform`.
296
319
  Given Babel's result object, allow loaders to make additional tweaks to it.
297
320
 
298
321
 
299
- ## [License](https://couto.mit-license.org/)
322
+ ## License
323
+ [MIT](https://couto.mit-license.org/)
package/lib/cache.js CHANGED
@@ -27,7 +27,7 @@ const mkdirpOrig = require("mkdirp");
27
27
 
28
28
  const findCacheDir = require("find-cache-dir");
29
29
 
30
- const promisify = require("util.promisify");
30
+ const promisify = require("pify");
31
31
 
32
32
  const transform = require("./transform"); // Lazily instantiated when needed
33
33
 
package/lib/index.js CHANGED
@@ -22,7 +22,9 @@ if (/^6\./.test(babel.version)) {
22
22
  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'.");
23
23
  }
24
24
 
25
- const pkg = require("../package.json");
25
+ const {
26
+ version
27
+ } = require("../package.json");
26
28
 
27
29
  const cache = require("./cache");
28
30
 
@@ -30,10 +32,16 @@ const transform = require("./transform");
30
32
 
31
33
  const injectCaller = require("./injectCaller");
32
34
 
33
- const path = require("path");
35
+ const schema = require("./schema");
36
+
37
+ const {
38
+ isAbsolute
39
+ } = require("path");
34
40
 
35
41
  const loaderUtils = require("loader-utils");
36
42
 
43
+ const validateOptions = require("schema-utils");
44
+
37
45
  function subscribe(subscriber, metadata, context) {
38
46
  if (context[subscriber]) {
39
47
  context[subscriber](metadata);
@@ -60,13 +68,16 @@ function _loader() {
60
68
  _loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
61
69
  const filename = this.resourcePath;
62
70
  let loaderOptions = loaderUtils.getOptions(this) || {};
71
+ validateOptions(schema, loaderOptions, {
72
+ name: "Babel loader"
73
+ });
63
74
 
64
75
  if (loaderOptions.customize != null) {
65
76
  if (typeof loaderOptions.customize !== "string") {
66
77
  throw new Error("Customized loaders must be implemented as standalone modules.");
67
78
  }
68
79
 
69
- if (!path.isAbsolute(loaderOptions.customize)) {
80
+ if (!isAbsolute(loaderOptions.customize)) {
70
81
  throw new Error("Customized loaders must be passed as absolute paths, since " + "babel-loader has no way to know what they would be relative to.");
71
82
  }
72
83
 
@@ -78,7 +89,7 @@ function _loader() {
78
89
 
79
90
  if (override.__esModule) override = override.default;
80
91
 
81
- if (typeof override === "function") {
92
+ if (typeof override !== "function") {
82
93
  throw new Error("Custom overrides must be functions.");
83
94
  }
84
95
 
@@ -137,7 +148,7 @@ function _loader() {
137
148
  throw new Error(`babel-loader ^8.0.0-beta.3 requires @babel/core@7.0.0-beta.41, but ` + `you appear to be using "${babel.version}". Either update your ` + `@babel/core version, or pin you babel-loader version to 8.0.0-beta.2`);
138
149
  }
139
150
 
140
- const config = babel.loadPartialConfig(injectCaller(programmaticOptions));
151
+ const config = babel.loadPartialConfig(injectCaller(programmaticOptions, this.target));
141
152
 
142
153
  if (config) {
143
154
  let options = config.options;
@@ -165,7 +176,7 @@ function _loader() {
165
176
  cacheIdentifier = JSON.stringify({
166
177
  options,
167
178
  "@babel/core": transform.version,
168
- "@babel/loader": pkg.version
179
+ "@babel/loader": version
169
180
  }),
170
181
  cacheCompression = true,
171
182
  metadataSubscribers = []
@@ -2,14 +2,21 @@
2
2
 
3
3
  const babel = require("@babel/core");
4
4
 
5
- module.exports = function injectCaller(opts) {
5
+ module.exports = function injectCaller(opts, target) {
6
6
  if (!supportsCallerOption()) return opts;
7
7
  return Object.assign({}, opts, {
8
8
  caller: Object.assign({
9
9
  name: "babel-loader",
10
+ // Provide plugins with insight into webpack target.
11
+ // https://github.com/babel/babel-loader/issues/787
12
+ target,
10
13
  // Webpack >= 2 supports ESM and dynamic import.
11
14
  supportsStaticESM: true,
12
- supportsDynamicImport: true
15
+ supportsDynamicImport: true,
16
+ // Webpack 5 supports TLA behind a flag. We enable it by default
17
+ // for Babel, and then webpack will throw an error if the experimental
18
+ // flag isn't enabled.
19
+ supportsTopLevelAwait: true
13
20
  }, opts.caller)
14
21
  });
15
22
  }; // TODO: We can remove this eventually, I'm just adding it so that people have
@@ -0,0 +1,28 @@
1
+ {
2
+ "type": "object",
3
+ "properties": {
4
+ "cacheDirectory": {
5
+ "oneOf": [
6
+ {
7
+ "type": "boolean"
8
+ },
9
+ {
10
+ "type": "string"
11
+ }
12
+ ],
13
+ "default": false
14
+ },
15
+ "cacheIdentifier": {
16
+ "type": "string"
17
+ },
18
+ "cacheCompression": {
19
+ "type": "boolean",
20
+ "default": true
21
+ },
22
+ "customize": {
23
+ "type": "string",
24
+ "default": null
25
+ }
26
+ },
27
+ "additionalProperties": true
28
+ }
package/lib/transform.js CHANGED
@@ -6,7 +6,7 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
6
6
 
7
7
  const babel = require("@babel/core");
8
8
 
9
- const promisify = require("util.promisify");
9
+ const promisify = require("pify");
10
10
 
11
11
  const LoaderError = require("./Error");
12
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-loader",
3
- "version": "8.0.3",
3
+ "version": "8.1.0",
4
4
  "description": "babel module loader for webpack",
5
5
  "files": [
6
6
  "lib"
@@ -10,41 +10,43 @@
10
10
  "node": ">= 6.9"
11
11
  },
12
12
  "dependencies": {
13
- "find-cache-dir": "^1.0.0",
14
- "loader-utils": "^1.0.2",
15
- "mkdirp": "^0.5.1",
16
- "util.promisify": "^1.0.0"
13
+ "find-cache-dir": "^2.1.0",
14
+ "loader-utils": "^1.4.0",
15
+ "mkdirp": "^0.5.3",
16
+ "pify": "^4.0.1",
17
+ "schema-utils": "^2.6.5"
17
18
  },
18
19
  "peerDependencies": {
19
20
  "@babel/core": "^7.0.0",
20
21
  "webpack": ">=2"
21
22
  },
22
23
  "devDependencies": {
23
- "@babel/cli": "^7.0.0",
24
- "@babel/core": "^7.0.0",
25
- "@babel/preset-env": "^7.0.0",
26
- "ava": "0.25.0",
27
- "babel-eslint": "^8.0.0",
28
- "babel-plugin-istanbul": "^4.0.0",
29
- "babel-plugin-react-intl": "^2.1.3",
30
- "cross-env": "^5.0.0",
31
- "eslint": "^4.1.0",
32
- "eslint-config-babel": "^8.0.0",
33
- "eslint-plugin-flowtype": "^2.25.0",
34
- "eslint-plugin-prettier": "^2.1.2",
35
- "husky": "^0.14.0",
36
- "lint-staged": "^7.1.0",
37
- "nyc": "^11.0.1",
38
- "prettier": "^1.2.2",
24
+ "@babel/cli": "^7.2.0",
25
+ "@babel/core": "^7.2.0",
26
+ "@babel/preset-env": "^7.2.0",
27
+ "ava": "^2.4.0",
28
+ "babel-eslint": "^10.0.1",
29
+ "babel-plugin-istanbul": "^5.1.0",
30
+ "babel-plugin-react-intl": "^4.1.19",
31
+ "cross-env": "^6.0.0",
32
+ "eslint": "^6.5.1",
33
+ "eslint-config-babel": "^9.0.0",
34
+ "eslint-config-prettier": "^6.3.0",
35
+ "eslint-plugin-flowtype": "^4.3.0",
36
+ "eslint-plugin-prettier": "^3.0.0",
37
+ "husky": "^3.0.7",
38
+ "lint-staged": "^9.4.1",
39
+ "nyc": "^14.1.1",
40
+ "prettier": "^1.15.3",
39
41
  "react": "^16.0.0",
40
- "react-intl": "^2.1.2",
41
- "react-intl-webpack-plugin": "^0.0.3",
42
- "rimraf": "^2.4.3",
42
+ "react-intl": "^3.3.2",
43
+ "react-intl-webpack-plugin": "^0.3.0",
44
+ "rimraf": "^3.0.0",
43
45
  "webpack": "^4.0.0"
44
46
  },
45
47
  "scripts": {
46
48
  "clean": "rimraf lib/",
47
- "build": "babel src/ --out-dir lib/",
49
+ "build": "babel src/ --out-dir lib/ --copy-files",
48
50
  "format": "prettier --write --trailing-comma all 'src/**/*.js' 'test/**/*.test.js' 'test/helpers/*.js' && prettier --write --trailing-comma es5 'scripts/*.js'",
49
51
  "lint": "eslint src test",
50
52
  "precommit": "lint-staged",
@@ -89,7 +91,10 @@
89
91
  "!test/fixtures/**/*",
90
92
  "!test/helpers/**/*"
91
93
  ],
92
- "source": [
94
+ "helpers": [
95
+ "**/helpers/**/*"
96
+ ],
97
+ "sources": [
93
98
  "src/**/*.js"
94
99
  ]
95
100
  },