Skip to content

Commit

Permalink
Add overrides option
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer authored and loganfsmyth committed Sep 27, 2018
1 parent c8d7a72 commit 9b70a02
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -90,6 +90,8 @@ This loader also supports the following loader-specific option:

* `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.

* `customize`: Default `null`. See [Customized Loader](#customized-loader).

**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).

## Troubleshooting
Expand Down Expand Up @@ -208,6 +210,9 @@ of Babel's configuration for each file that it processes.
`babel` so that tooling can ensure that it using exactly the same `@babel/core`
instance as the loader itself.

This same callback can be provided under the `customize` key in the loader options.
You may also pass `customize` a file name which exports this callback.

### Example

```js
Expand All @@ -230,7 +235,7 @@ module.exports = require("babel-loader").custom(babel => {
};
},

// Passed Babel's 'PartialConfig' object.
// Passed Babel's 'PartialConfig' object.
config(cfg) {
if (cfg.hasFilesystemConfig()) {
// Use the normal config
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Expand Up @@ -52,6 +52,17 @@ async function loader(source, inputSourceMap, overrides) {

let loaderOptions = loaderUtils.getOptions(this) || {};

overrides = overrides || loaderOptions.customize;
// customize may have been passed as a file, so we should load it
if (typeof overrides === "string") {
overrides = require(overrides);
}
// customize may have been passed as a function and not an object (to access
// the `babel` variable), so let's build the overrides
if (typeof overrides === "function") {
overrides = overrides(babel);
}

let customOptions;
if (overrides && overrides.customOptions) {
const result = await overrides.customOptions.call(this, loaderOptions);
Expand Down Expand Up @@ -105,6 +116,7 @@ async function loader(source, inputSourceMap, overrides) {
sourceFileName: filename,
});
// Remove loader related options
delete programmaticOptions.customize;
delete programmaticOptions.cacheDirectory;
delete programmaticOptions.cacheIdentifier;
delete programmaticOptions.cacheCompression;
Expand Down

0 comments on commit 9b70a02

Please sign in to comment.