Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit f250dea

Browse files
committedJan 10, 2019
Add docs about .custom builder
1 parent 542c143 commit f250dea

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
 

‎README.md

+59
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,65 @@ npm install --save-dev rollup-plugin-babel@3 babel-preset-env babel-plugin-exter
137137
}
138138
```
139139

140+
## Custom plugin builder
141+
142+
`rollup-plugin-babel` exposes a plugin-builder utility that allows users to add custom handling of Babel's configuration for each file that it processes.
143+
144+
`.custom` accepts a callback that will be called with the loader's instance of `babel` so that tooling can ensure that it using exactly the same `@babel/core` instance as the loader itself.
145+
146+
It's main purpose is to allow other tools for configuration of transpilation without forcing people to add extra configuration but still allow for using their own babelrc / babel config files.
147+
148+
### Example
149+
150+
```js
151+
import babel from 'rollup-plugin-babel';
152+
153+
export default babel.custom(babelCore => {
154+
function myPlugin() {
155+
return {
156+
visitor: {},
157+
};
158+
}
159+
160+
return {
161+
// Passed the plugin options.
162+
options({ opt1, opt2, ...pluginOptions }) {
163+
return {
164+
// Pull out any custom options that the plugin might have.
165+
customOptions: { opt1, opt2 },
166+
167+
// Pass the options back with the two custom options removed.
168+
pluginOptions,
169+
};
170+
},
171+
172+
config(cfg /* Passed Babel's 'PartialConfig' object. */, { code, customOptions }) {
173+
if (cfg.hasFilesystemConfig()) {
174+
// Use the normal config
175+
return cfg.options;
176+
}
177+
178+
return {
179+
...cfg.options,
180+
plugins: [
181+
...(cfg.options.plugins || []),
182+
183+
// Include a custom plugin in the options.
184+
myPlugin,
185+
],
186+
};
187+
},
188+
189+
result(result, { code, customOptions, config, transformOptions }) {
190+
return {
191+
...result,
192+
code: result.code + '\n// Generated by some custom loader',
193+
};
194+
},
195+
};
196+
});
197+
```
198+
140199
## License
141200

142201
MIT

0 commit comments

Comments
 (0)
This repository has been archived.