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

Commit

Permalink
Merge branch 'custom-builder'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Jan 10, 2019
2 parents ba2559a + f250dea commit 97ac95b
Show file tree
Hide file tree
Showing 8 changed files with 472 additions and 490 deletions.
41 changes: 17 additions & 24 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
{
"rules": {
"indent": [ 2, "tab", { "SwitchCase": 1 } ],
"quotes": [ 2, "single" , { "avoidEscape": true }],
"linebreak-style": [ 2, "unix" ],
"semi": [ 2, "always" ],
"keyword-spacing": [ 2, { "before": true, "after": true } ],
"space-before-blocks": [ 2, "always" ],
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
"no-cond-assign": [ 0 ],
},
"env": {
"es6": true,
"browser": true,
"mocha": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018,
"ecmaFeatures": {
"spread": true
}
}
"extends": ["eslint:recommended", "prettier", "plugin:prettier/recommended"],
"rules": {
"no-cond-assign": [0]
},
"env": {
"es6": true,
"browser": true,
"mocha": true,
"node": true
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018,
"ecmaFeatures": {
"spread": true
}
}
}
67 changes: 63 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ However, setting `modules: false` in your `.babelrc` may conflict if you are usi

```js
plugins: [
babel({
babelrc: false,
presets: [['env', { modules: false }]],
}),
babel({
babelrc: false,
presets: [['env', { modules: false }]],
}),
];
```

Expand Down Expand Up @@ -137,6 +137,65 @@ npm install --save-dev rollup-plugin-babel@3 babel-preset-env babel-plugin-exter
}
```

## Custom plugin builder

`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.

`.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.

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.

### Example

```js
import babel from 'rollup-plugin-babel';

export default babel.custom(babelCore => {
function myPlugin() {
return {
visitor: {},
};
}

return {
// Passed the plugin options.
options({ opt1, opt2, ...pluginOptions }) {
return {
// Pull out any custom options that the plugin might have.
customOptions: { opt1, opt2 },

// Pass the options back with the two custom options removed.
pluginOptions,
};
},

config(cfg /* Passed Babel's 'PartialConfig' object. */, { code, customOptions }) {
if (cfg.hasFilesystemConfig()) {
// Use the normal config
return cfg.options;
}

return {
...cfg.options,
plugins: [
...(cfg.options.plugins || []),

// Include a custom plugin in the options.
myPlugin,
],
};
},

result(result, { code, customOptions, config, transformOptions }) {
return {
...result,
code: result.code + '\n// Generated by some custom loader',
};
},
};
});
```

## License

MIT
Loading

0 comments on commit 97ac95b

Please sign in to comment.