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

Commit 97ac95b

Browse files
committed
Merge branch 'custom-builder'
2 parents ba2559a + f250dea commit 97ac95b

File tree

8 files changed

+472
-490
lines changed

8 files changed

+472
-490
lines changed

.eslintrc

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
{
2-
"rules": {
3-
"indent": [ 2, "tab", { "SwitchCase": 1 } ],
4-
"quotes": [ 2, "single" , { "avoidEscape": true }],
5-
"linebreak-style": [ 2, "unix" ],
6-
"semi": [ 2, "always" ],
7-
"keyword-spacing": [ 2, { "before": true, "after": true } ],
8-
"space-before-blocks": [ 2, "always" ],
9-
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
10-
"no-cond-assign": [ 0 ],
11-
},
12-
"env": {
13-
"es6": true,
14-
"browser": true,
15-
"mocha": true,
16-
"node": true
17-
},
18-
"extends": "eslint:recommended",
19-
"parserOptions": {
20-
"sourceType": "module",
21-
"ecmaVersion": 2018,
22-
"ecmaFeatures": {
23-
"spread": true
24-
}
25-
}
2+
"extends": ["eslint:recommended", "prettier", "plugin:prettier/recommended"],
3+
"rules": {
4+
"no-cond-assign": [0]
5+
},
6+
"env": {
7+
"es6": true,
8+
"browser": true,
9+
"mocha": true,
10+
"node": true
11+
},
12+
"parserOptions": {
13+
"sourceType": "module",
14+
"ecmaVersion": 2018,
15+
"ecmaFeatures": {
16+
"spread": true
17+
}
18+
}
2619
}

README.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ However, setting `modules: false` in your `.babelrc` may conflict if you are usi
105105

106106
```js
107107
plugins: [
108-
babel({
109-
babelrc: false,
110-
presets: [['env', { modules: false }]],
111-
}),
108+
babel({
109+
babelrc: false,
110+
presets: [['env', { modules: false }]],
111+
}),
112112
];
113113
```
114114

@@ -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)