1 | # find-webpack [![semantic-release][semantic-image] ][semantic-url] [![Build status][ci-image] ][ci-url]
|
2 |
|
3 | > Utility to find webpack settings in react-scripts and other situations
|
4 |
|
5 | ## Use
|
6 |
|
7 | ```shell
|
8 | npm i -S find-webpack
|
9 | ```
|
10 |
|
11 | ```js
|
12 | const fw = require('find-webpack')
|
13 | const config = fw.getWebpackOptions()
|
14 | // if config is an object, we found it!
|
15 | ```
|
16 |
|
17 | Works with `react-scripts` and ejected `react-scripts`. Uses `development` environment.
|
18 |
|
19 | **Note:** `react-scripts` assumes there is `package.json` file in the current working directory, otherwise it won't load.
|
20 |
|
21 | ### tryLoadingWebpackConfig
|
22 |
|
23 | Loading Webpack config from `webpack.config.js` might need `NODE_ENV` set, and other tricks.
|
24 |
|
25 | ```js
|
26 | const fw = require('find-webpack')
|
27 | const config = fw.tryLoadingWebpackConfig('path/to/webpack.config.js')
|
28 | ```
|
29 |
|
30 | Returns `undefined` if cannot load the config object.
|
31 |
|
32 | **Note:** when loading the config object, this module sets `process.env.BABEL_ENV` and `process.env.NODE_ENV` to `development` and keeps it.
|
33 |
|
34 | ### Cypress
|
35 |
|
36 | There is a utility method for cleaning up the found Webpack config for using with Cypress webpack preprocessor: removing optimization plugins, etc.
|
37 |
|
38 | ```js
|
39 | const fw = require('find-webpack')
|
40 | const config = fw.getWebpackOptions()
|
41 | if (config) {
|
42 | // config is modified in place
|
43 | const opts = {
|
44 | reactScripts: true, // cleaning for react-scripts?
|
45 | coverage: true, // adds babel-plugin-istanbul
|
46 | }
|
47 | fw.cleanForCypress(opts, config)
|
48 | }
|
49 | ```
|
50 |
|
51 | If you are using `opts.reactScripts = true`, you can also add the component test folder to be transpiled using the same options as `src` folder.
|
52 |
|
53 | ```js
|
54 | const componentTestFolder = ... // from Cypress config
|
55 | // config is modified in place
|
56 | const opts = {
|
57 | reactScripts: true, // cleaning for react-scripts?
|
58 | addFolderToTranspile: componentTestFolder,
|
59 | coverage: true // adds babel-plugin-istanbul
|
60 | }
|
61 | fw.cleanForCypress(opts, config)
|
62 | ```
|
63 |
|
64 | and you can add option `looseModules: true` to insert Babel plugin [@babel/plugin-transform-modules-commonjs](https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs) to allow mocking named imports.
|
65 |
|
66 | ## Debugging
|
67 |
|
68 | Run with environment variable `DEBUG=find-webpack` to see verbose logs
|
69 |
|
70 | ## About
|
71 |
|
72 | ### Author
|
73 |
|
74 | Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017
|
75 |
|
76 | - [@bahmutov](https://twitter.com/bahmutov)
|
77 | - [glebbahmutov.com](https://glebbahmutov.com)
|
78 | - [blog](https://glebbahmutov.com/blog)
|
79 |
|
80 | ### License
|
81 |
|
82 | [MIT](LICENSE) - do anything with the code, but don't blame me if it does not work.
|
83 |
|
84 | [ci-image]: https://github.com/bahmutov/find-webpack/workflows/ci/badge.svg?branch=master
|
85 | [ci-url]: https://github.com/bahmutov/find-webpack/actions
|
86 | [semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
87 | [semantic-url]: https://github.com/semantic-release/semantic-release
|