From 9c1e3f166415435eb4a9095ac86474752ba77bbf Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Mon, 24 Sep 2018 14:28:04 -0400 Subject: [PATCH 1/4] Add overrides option --- README.md | 7 ++++++- src/index.js | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 921b2e32..0a8e8831 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. +* `overrides`: 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 `overrides` key in the loader options. +You may also pass `overrides` 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..257fcdaa 100644 --- a/src/index.js +++ b/src/index.js @@ -52,6 +52,14 @@ async function loader(source, inputSourceMap, overrides) { let loaderOptions = loaderUtils.getOptions(this) || {}; + overrides = overrides || loaderOptions.overrides; + if (typeof overrides === "string") { + overrides = require(overrides); + } + if (typeof overrides === "function") { + overrides = overrides(babel); + } + let customOptions; if (overrides && overrides.customOptions) { const result = await overrides.customOptions.call(this, loaderOptions); @@ -105,6 +113,7 @@ async function loader(source, inputSourceMap, overrides) { sourceFileName: filename, }); // Remove loader related options + delete programmaticOptions.overrides; delete programmaticOptions.cacheDirectory; delete programmaticOptions.cacheIdentifier; delete programmaticOptions.cacheCompression; From ea156de799eb81382a7dac951e0299ef0091a0e0 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Mon, 24 Sep 2018 15:30:27 -0400 Subject: [PATCH 2/4] Change key name --- README.md | 6 +++--- src/index.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0a8e8831..ed2ae9ef 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ 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. -* `overrides`: Default `null`. See [Customized Loader](#customized-loader). +* `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). @@ -210,8 +210,8 @@ 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 `overrides` key in the loader options. -You may also pass `overrides` a file name which exports this callback. +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 diff --git a/src/index.js b/src/index.js index 257fcdaa..ed4efd06 100644 --- a/src/index.js +++ b/src/index.js @@ -52,7 +52,7 @@ async function loader(source, inputSourceMap, overrides) { let loaderOptions = loaderUtils.getOptions(this) || {}; - overrides = overrides || loaderOptions.overrides; + overrides = overrides || loaderOptions.customize; if (typeof overrides === "string") { overrides = require(overrides); } From b2335a2ad89a31a9d9defded8adc2ce073d72528 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Mon, 24 Sep 2018 15:31:35 -0400 Subject: [PATCH 3/4] Oops --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index ed4efd06..30be99e3 100644 --- a/src/index.js +++ b/src/index.js @@ -113,7 +113,7 @@ async function loader(source, inputSourceMap, overrides) { sourceFileName: filename, }); // Remove loader related options - delete programmaticOptions.overrides; + delete programmaticOptions.customize; delete programmaticOptions.cacheDirectory; delete programmaticOptions.cacheIdentifier; delete programmaticOptions.cacheCompression; From e7dff83f8f5858c133fcdb08480c28492caf2a8b Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Mon, 24 Sep 2018 15:36:07 -0400 Subject: [PATCH 4/4] Add comments --- src/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.js b/src/index.js index 30be99e3..92f55754 100644 --- a/src/index.js +++ b/src/index.js @@ -53,9 +53,12 @@ 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); }