|
2 | 2 |
|
3 | 3 | Cypress preprocessor for bundling JavaScript via webpack |
4 | 4 |
|
5 | | -## Install |
| 5 | +## Installation |
6 | 6 |
|
7 | 7 | Requires [Node](https://nodejs.org/en/) version 6.5.0 or above. |
8 | 8 |
|
9 | 9 | ```sh |
10 | 10 | npm install --save-dev cypress-webpack-preprocessor |
11 | 11 | ``` |
12 | 12 |
|
| 13 | +## Usage |
| 14 | + |
| 15 | +In your project's [plugins file](https://on.cypress.io/guides/plugins): |
| 16 | + |
| 17 | +```javascript |
| 18 | +const webpack = require('@cypress/webpack-preprocessor') |
| 19 | + |
| 20 | +module.exports = (register, config) => { |
| 21 | + register('on:spec:file:preprocessor', webpack(config)) |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +## Options |
| 26 | + |
| 27 | +Pass in options as the second argument to `webpack`: |
| 28 | + |
| 29 | +```javascript |
| 30 | +module.exports = (register, config) => { |
| 31 | + const options = { |
| 32 | + // send in the options from your webpack.config.js, so it works the same |
| 33 | + // as your app's code |
| 34 | + webpackOptions: require('../../webpack.config'), |
| 35 | + watchOptions: {}, |
| 36 | + } |
| 37 | + |
| 38 | + register('on:spec:file:preprocessor', webpack(config, options)) |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### webpackOptions |
| 43 | + |
| 44 | +Object of webpack options. Just `require` in the options from your `webpack.config.js` to use the same options as your app. |
| 45 | + |
| 46 | +**Default**: |
| 47 | + |
| 48 | +```javascript |
| 49 | +{ |
| 50 | + module: { |
| 51 | + rules: [ |
| 52 | + { |
| 53 | + test: /\.jsx?$/, |
| 54 | + exclude: [/node_modules/], |
| 55 | + use: [{ |
| 56 | + loader: 'babel-loader', |
| 57 | + options: { |
| 58 | + presets: [ |
| 59 | + 'babel-preset-env', |
| 60 | + 'babel-preset-react', |
| 61 | + ], |
| 62 | + }, |
| 63 | + }], |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### watchOptions |
| 71 | + |
| 72 | +Object of options for watching. See [webpack's docs](https://webpack.github.io/docs/node.js-api.html#compiler). |
| 73 | + |
| 74 | +**Default**: `{}` |
| 75 | + |
| 76 | +## Modifying default options |
| 77 | + |
| 78 | +The default options are provided as `webpack.defaultOptions` so they can be more easily modified. |
| 79 | + |
| 80 | +If, for example, you want to update the options for the `babel-loader` to add the [stage-3 preset](https://babeljs.io/docs/plugins/preset-stage-3/), you could do the following: |
| 81 | + |
| 82 | +```javascript |
| 83 | +const webpack = require('@cypress/webpack-preprocessor') |
| 84 | + |
| 85 | +module.exports = (register, config) => { |
| 86 | + const options = webpack.defaultOptions |
| 87 | + options.webpackOptions.module.rules[0].use.options.presets.push('babel-preset-stage-3') |
| 88 | + |
| 89 | + register('on:spec:file:preprocessor', webpack(config, options)) |
| 90 | +} |
| 91 | +``` |
| 92 | + |
13 | 93 | ## License |
14 | 94 |
|
15 | 95 | This project is licensed under the terms of the [MIT license](/LICENSE.md). |
0 commit comments