1 |
|
2 | const debug = require('debug')('find-webpack')
|
3 | const path = require('path')
|
4 | const fs = require('fs')
|
5 | const findYarnWorkspaceRoot = require('find-yarn-workspace-root')
|
6 | const mockEnv = require('mocked-env')
|
7 |
|
8 | const tryLoadingWebpackConfig = (webpackConfigPath) => {
|
9 | debug('trying to load webpack config from %s', webpackConfigPath)
|
10 |
|
11 | const envName = 'development'
|
12 |
|
13 | const restoreEnv = mockEnv({
|
14 | BABEL_ENV: envName,
|
15 | NODE_ENV: envName,
|
16 | })
|
17 | try {
|
18 | let webpackOptions = require(webpackConfigPath)
|
19 | if (typeof webpackOptions === 'function') {
|
20 | webpackOptions = webpackOptions(envName)
|
21 | }
|
22 | debug('webpack options: %o', webpackOptions)
|
23 | return webpackOptions
|
24 | } catch (err) {
|
25 | debug('could not load react-scripts webpack')
|
26 | debug('error %s', err.message)
|
27 | debug(err)
|
28 | restoreEnv()
|
29 | }
|
30 | }
|
31 |
|
32 | const tryVueCLIScripts = () => {
|
33 | const webpackConfigPath = path.resolve(
|
34 | findYarnWorkspaceRoot() || process.cwd(),
|
35 | 'node_modules',
|
36 | '@vue',
|
37 | 'cli-service',
|
38 | 'webpack.config.js',
|
39 | )
|
40 |
|
41 | debug('path to Vue CLI resolved webpack.config.js: %s', webpackConfigPath)
|
42 | return tryLoadingWebpackConfig(webpackConfigPath)
|
43 | }
|
44 |
|
45 | const tryRootProjectWebpack = () => {
|
46 | const webpackConfigPath = path.resolve(
|
47 | findYarnWorkspaceRoot() || process.cwd(),
|
48 | 'webpack.config.js',
|
49 | )
|
50 |
|
51 | debug('path to root webpack.config.js: %s', webpackConfigPath)
|
52 | return tryLoadingWebpackConfig(webpackConfigPath)
|
53 | }
|
54 |
|
55 | const tryReactScripts = () => {
|
56 |
|
57 |
|
58 | const root = findYarnWorkspaceRoot() || process.cwd()
|
59 | debug('trying filename for react scripts from root %s', root)
|
60 | const filename = path.resolve(
|
61 | root,
|
62 | 'node_modules',
|
63 | 'react-scripts',
|
64 | 'config',
|
65 | 'webpack.config.js',
|
66 | )
|
67 | return tryLoadingWebpackConfig(filename)
|
68 | }
|
69 |
|
70 | const tryEjectedReactScripts = () => {
|
71 | const webpackConfigPath = path.resolve(
|
72 | process.cwd(),
|
73 | 'config',
|
74 | 'webpack.config.js',
|
75 | )
|
76 | return tryLoadingWebpackConfig(webpackConfigPath)
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | const tryWebpackInReactScriptsModule = () => {
|
84 | const webpackConfigModuleName = 'react-scripts/config/webpack.config.js'
|
85 | debug(
|
86 | 'trying to require webpack config via path: %s',
|
87 | webpackConfigModuleName,
|
88 | )
|
89 |
|
90 | const found = tryLoadingWebpackConfig(webpackConfigModuleName)
|
91 | if (!found) {
|
92 | debug('Could not find react-scripts webpack config')
|
93 | const packageJsonExists = fs.existsSync(
|
94 | path.join(process.cwd(), 'package.json'),
|
95 | )
|
96 | if (!packageJsonExists) {
|
97 | debug('⚠️ react-scripts requires package.json file')
|
98 | debug('We could not find it in % s', process.cwd())
|
99 | }
|
100 | }
|
101 | return found
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | const getWebpackOptions = () => {
|
109 | debug('get webpack starting from cwd: %s', process.cwd())
|
110 | const webpackOptions =
|
111 | tryReactScripts() ||
|
112 | tryEjectedReactScripts() ||
|
113 | tryVueCLIScripts() ||
|
114 | tryRootProjectWebpack() ||
|
115 | tryWebpackInReactScriptsModule()
|
116 |
|
117 | if (!webpackOptions) {
|
118 |
|
119 |
|
120 | debug('could not find webpack options')
|
121 | }
|
122 | return webpackOptions
|
123 | }
|
124 |
|
125 | module.exports = getWebpackOptions
|