You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
4.3 KiB

# Advanced Neutrino Customization
No two JavaScript projects are ever the same, and as such there may be times when you will need to make modifications
to the way your Neutrino preset is building your project. If you need more customization than can be afforded by
augmenting your project's package.json, consider using this advanced configuration guide to modify your build as
needed.
8 years ago
## Creating a project-specific override
8 years ago
Neutrino configurations are backed by [webpack-chain](https://github.com/mozilla-neutrino/webpack-chain), a library for
making modifications to a Webpack configuration using a fluent or chained API. When your project needs more advanced
build overrides, you will be interacting with this API in order to perform modifications.
8 years ago
First, we need to create a project-specific override to make these changes. This can either be a JS file or a directory
with an `index.js` file. Since Neutrino uses Node.js and Webpack for interacting with presets, it is helpful to
8 years ago
understand that this is a Node.js module. By exporting a middleware function from your module, you will be provided with
a Neutrino instance for modifying the build. Let's create a file called `override.js` in the root of our example
project:
```js
8 years ago
// override.js
module.exports = neutrino => {
// ...
};
```
8 years ago
The signature of this function is what we call "Neutrino middleware". If you're familiar with middleware from the
Express/connect world, this works similarly. When using Express middleware, you provide a function to Express which
receives arguments to modify a request or response along its lifecycle. There can be a number of middleware functions
that Express can load, each one potentially modifying a request or response in succession.
When you customize Neutrino with an override, you export a Neutrino middleware function, except this is typically used
to override Neutrino's configuration. Every preset or middleware the Neutrino has loaded follows this same middleware
pipeline.
At the moment our custom override isn't doing anything, but it does get us far enough to be able to tell Neutrino
to use it for additional configuration. Modify your package.json and add `override.js` as an additional preset.
_Note: Neutrino will attempt to load this module relative to the current working directory, which should be the root of
your project._
```json
{
"config": {
"presets": [
"neutrino-preset-react",
"neutrino-preset-karma",
8 years ago
"override.js"
]
},
"scripts": {
"build": "neutrino build"
}
}
```
Other than actually changing the config, that is all the setup necessary for Neutrino to pick up your custom changes.
## Configuring
The Neutrino instance provided to your custom configurator has a `config` property that is an instance of
8 years ago
[webpack-chain](https://github.com/mozilla-neutrino/webpack-chain). We won't go in-depth of all the configuration
possibilities here, but encourage you to check out the documentation for webpack-chain for instruction on your
8 years ago
particular use case. Just know that you can use webpack-chain to modify any part of the underlying Webpack configuration
using its API.
8 years ago
This `neutrino.config` is an accumulation of all configuration up to this moment. All Neutrino middleware and presets
interact with and make changes through this config, which is all available to you. For example, if you are using the
presets `neutrino-preset-react` and `neutrino-preset-karma`, any config set can be extended, manipulated, or removed.
_Example: Neutrino's React preset adds `.jsx` as a module extension. Let's remove it._
```js
module.exports = neutrino => {
neutrino.config.resolve.extensions.delete('.jsx');
};
```
_Example: Neutrino's Node.js preset uses `babel-preset-env` to support Node.js v6.9. Let's change it to support back to
v4.2. This preset has a rule named "compile" and a loader named "babel"._
```js
module.exports = neutrino => {
neutrino.config.module
.rule('compile')
8 years ago
.loader('babel', options => {
options.presets[0][1].targets.node = 4.2;
return options;
});
};
```
Presets can also have their own custom data in addition to the Neutrino config. See your respective preset for details.
8 years ago
Again, rather than reiterate the documentation for [webpack-chain](https://github.com/mozilla-neutrino/webpack-chain)
here, please refer to its documentation for all ways you can modify a config instance to solve your use cases.