diff --git a/README.md b/README.md index 921b2e32..ed2ae9ef 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/src/index.js b/src/index.js index e54bb602..92f55754 100644 --- a/src/index.js +++ b/src/index.js @@ -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); @@ -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;