| 3 | import { DefaultConfiguration } from './webpack/common'; |
| 4 | |
| 5 | export async function extendConfig( |
| 6 | [webPackConfig, enhancer]: DefaultConfiguration, |
| 7 | otherConfigPath: string, |
| 8 | overrides: Configuration = {}, |
| 9 | ): Promise<Configuration> { |
| 10 | const original = webPackConfig; |
| 11 | |
| 12 | if (existsSync(otherConfigPath)) { |
| 13 | try { |
| 14 | let otherConfig = require(otherConfigPath); |
| 15 | if (otherConfig.default) { |
| 16 | // The webpack config file appears to be an ESM module; |
| 17 | // this interop should give us the actual exported config |
| 18 | otherConfig = otherConfig.default; |
| 19 | } |
| 20 | |
| 21 | if (typeof otherConfig === 'function') { |
| 22 | // support Promise for returned config: |
| 23 | // https://webpack.js.org/configuration/configuration-types/#exporting-a-promise |
| 24 | webPackConfig = await otherConfig(webPackConfig); |
| 25 | } else if (typeof otherConfig === 'object') { |
| 26 | webPackConfig = { |
| 27 | ...webPackConfig, |
| 28 | ...otherConfig, |
| 29 | }; |
| 30 | } else { |
| 31 | console.warn(`Did not recognize the export from "${otherConfigPath}". Skipping.`); |
| 32 | } |
| 33 | } catch (ex) { |
| 34 | console.error(`Error while using the config from "${otherConfigPath}": ${ex}`); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | ['entry', 'output', 'optimization'].forEach((s) => { |
| 39 | if (original[s] !== webPackConfig[s]) { |
| 40 | console.warn( |
| 41 | `You've overwritten the "${s}" section of the Webpack config. Make sure you know what you are doing.`, |
| 42 | ); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | return enhancer({ |
| 47 | ...webPackConfig, |
| 48 | ...overrides, |
| 49 | }); |
| 50 | } |