Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customize option #687

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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