-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
getUserWebpackConfig.ts
79 lines (67 loc) · 2.62 KB
/
getUserWebpackConfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import path from 'node:path';
import {
CosmosConfig,
getCliArgs,
importModule,
moduleExists,
} from 'react-cosmos';
import webpack from 'webpack';
import { createWebpackCosmosConfig } from '../cosmosConfig/createWebpackCosmosConfig.js';
import { getDefaultWebpackConfig } from './getDefaultWebpackConfig.js';
import { getWebpackNodeEnv } from './getWebpackNodeEnv.js';
type WebpackConfig =
| webpack.Configuration
// Mirror webpack API for config functions
// https://webpack.js.org/configuration/configuration-types/#exporting-a-function
| ((
env: unknown,
_argv: {}
) => webpack.Configuration | Promise<webpack.Configuration>);
// Override arguments are inspired by react-app-rewired
// https://github.com/timarney/react-app-rewired/blob/b673379d32fe7b57c71667f4827f3b16e3717363/scripts/start.js#L22
type WebpackOverride = (
baseConfig: webpack.Configuration,
env: string
) => webpack.Configuration;
export async function getUserWebpackConfig(
cosmosConfig: CosmosConfig,
userWebpack: typeof webpack
) {
const baseWebpackConfig = await getBaseWebpackConfig(
cosmosConfig,
userWebpack
);
const { overridePath } = createWebpackCosmosConfig(cosmosConfig);
if (!overridePath || !moduleExists(overridePath)) {
console.log(
`[Cosmos] Learn how to override webpack config for cosmos: https://reactcosmos.org/docs/getting-started/webpack#webpack-config-override`
);
return baseWebpackConfig;
}
const relPath = path.relative(process.cwd(), overridePath);
console.log(`[Cosmos] Overriding webpack config at ${relPath}`);
const module = await importModule<{ default: WebpackOverride }>(overridePath);
const webpackOverride = module.default;
return webpackOverride(baseWebpackConfig, getWebpackNodeEnv());
}
async function getBaseWebpackConfig(
cosmosConfig: CosmosConfig,
userWebpack: typeof webpack
) {
const { rootDir } = cosmosConfig;
const { configPath } = createWebpackCosmosConfig(cosmosConfig);
if (!configPath || !moduleExists(configPath)) {
console.log('[Cosmos] Using default webpack config');
return getDefaultWebpackConfig(userWebpack, rootDir);
}
const relPath = path.relative(process.cwd(), configPath);
console.log(`[Cosmos] Using webpack config found at ${relPath}`);
const module = await importModule<{ default: WebpackConfig }>(configPath);
const webpackConfig = module.default;
// The --env flag matches the webpack CLI convention
// https://webpack.js.org/api/cli/#env
const cliArgs = getCliArgs();
return typeof webpackConfig === 'function'
? await webpackConfig(cliArgs.env || getWebpackNodeEnv(), cliArgs)
: webpackConfig;
}