Skip to content

Commit

Permalink
Tweak the customize implementation to be a bit more strict.
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Sep 27, 2018
1 parent 9b70a02 commit ac0c869
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

* `customize`: Default `null`. See [Customized Loader](#customized-loader).
* `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.

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

Expand Down Expand Up @@ -210,12 +210,14 @@ 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.
In cases where you want to customize without actually having a file to call `.custom`, you
may also pass the `customize` option with a string pointing at a file that exports
your `custom` callback function.

### Example

```js
// Export from "./my-custom-loader.js" or whatever you want.
module.exports = require("babel-loader").custom(babel => {
function myPlugin() {
return {
Expand Down Expand Up @@ -263,6 +265,20 @@ module.exports = require("babel-loader").custom(babel => {
});
```

```js
// And in your Webpack config
module.exports = {
// ..
module: {
rules: [{
// ...
loader: path.join(__dirname, 'my-custom-loader.js'),
// ...
}]
}
};
```

### `customOptions(options: Object): { custom: Object, loader: Object }`

Given the loader's options, split custom options out of `babel-loader`'s
Expand Down
36 changes: 27 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const pkg = require("../package.json");
const cache = require("./cache");
const transform = require("./transform");

const path = require("path");
const loaderUtils = require("loader-utils");

function subscribe(subscriber, metadata, context) {
Expand Down Expand Up @@ -52,15 +53,32 @@ 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);
if (loaderOptions.customize != null) {
if (typeof loaderOptions.customize !== "string") {
throw new Error(
"Customized loaders must be implemented as standalone modules.",
);
}
if (!path.isAbsolute(loaderOptions.customize)) {
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.",
);
}
if (overrides) {
throw new Error(
"babel-loader's 'customize' option is not available when already " +
"using a customized babel-loader wrapper.",
);
}

let override = require(loaderOptions.customize);
if (override.__esModule) override = override.default;

if (typeof override === "function") {

This comment has been minimized.

Copy link
@iansu

iansu Sep 27, 2018

Contributor

Is this check correct? If override is a function throw an error saying it needs to be a function?

This comment has been minimized.

Copy link
@loganfsmyth

loganfsmyth Sep 27, 2018

Author Member

Nope! That's what I get to tweaking late at night.

This comment has been minimized.

Copy link
@iansu

iansu Sep 27, 2018

Contributor

Fixed here: #692

throw new Error("Custom overrides must be functions.");
}
overrides = override(babel);
}

let customOptions;
Expand Down

0 comments on commit ac0c869

Please sign in to comment.