|
1 | 1 | import fs from 'fs'; |
2 | | -import type { WebpackPluginInstance } from 'webpack'; |
| 2 | +import type { ResolveOptions, WebpackPluginInstance } from 'webpack'; |
3 | 3 | import { merge } from 'webpack-merge'; |
4 | 4 | import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin'; |
5 | 5 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
@@ -31,10 +31,31 @@ import { sharedExternals } from './externals'; |
31 | 31 | import { WebpackPluginMulticompilerProgress } from './webpack-plugin-multicompiler-progress'; |
32 | 32 | import MiniCssExtractPlugin from 'mini-css-extract-plugin'; |
33 | 33 |
|
34 | | -const sharedResolveOptions = { |
35 | | - mainFields: ['compass:module', 'compass:main', 'module', 'main'], |
36 | | - exportsFields: ['compass:exports', 'exports'], |
37 | | - extensions: ['.jsx', '.tsx', '.ts', '...'], |
| 34 | +const sharedResolveOptions = ( |
| 35 | + target: ConfigArgs['target'] |
| 36 | +): Pick<ResolveOptions, 'mainFields' | 'exportsFields' | 'extensions'> => { |
| 37 | + if (typeof target === 'string') { |
| 38 | + target = [target]; |
| 39 | + } |
| 40 | + return { |
| 41 | + // This replicates webpack behavior with additional special `compass:` keys |
| 42 | + // taking priority over the default ones that webpack uses |
| 43 | + // |
| 44 | + // See https://webpack.js.org/configuration/resolve/#resolvemainfields |
| 45 | + mainFields: |
| 46 | + target?.includes('web') || target?.includes('webworker') |
| 47 | + ? [ |
| 48 | + 'compass:browser', |
| 49 | + 'compass:module', |
| 50 | + 'compass:main', |
| 51 | + 'browser', |
| 52 | + 'module', |
| 53 | + 'main', |
| 54 | + ] |
| 55 | + : ['compass:module', 'compass:main', 'module', 'main'], |
| 56 | + exportsFields: ['compass:exports', 'exports'], |
| 57 | + extensions: ['.jsx', '.tsx', '.ts', '...'], |
| 58 | + }; |
38 | 59 | }; |
39 | 60 |
|
40 | 61 | export function createElectronMainConfig( |
@@ -69,7 +90,7 @@ export function createElectronMainConfig( |
69 | 90 | resolve: { |
70 | 91 | // To avoid resolving the `browser` field |
71 | 92 | aliasFields: [], |
72 | | - ...sharedResolveOptions, |
| 93 | + ...sharedResolveOptions(opts.target), |
73 | 94 | }, |
74 | 95 | plugins: [new WebpackPluginMulticompilerProgress()], |
75 | 96 | }; |
@@ -141,7 +162,7 @@ export function createElectronRendererConfig( |
141 | 162 | resolve: { |
142 | 163 | // To avoid resolving the `browser` field |
143 | 164 | aliasFields: [], |
144 | | - ...sharedResolveOptions, |
| 165 | + ...sharedResolveOptions(opts.target), |
145 | 166 | }, |
146 | 167 | }; |
147 | 168 |
|
@@ -254,7 +275,7 @@ export function createWebConfig(args: Partial<ConfigArgs>): WebpackConfig { |
254 | 275 | ...toCommonJsExternal(builtinModules), |
255 | 276 | }, |
256 | 277 | resolve: { |
257 | | - ...sharedResolveOptions, |
| 278 | + ...sharedResolveOptions(opts.target), |
258 | 279 | }, |
259 | 280 | }; |
260 | 281 | } |
|
0 commit comments