Skip to content

Commit

Permalink
feat: Override CRA paths
Browse files Browse the repository at this point in the history
  • Loading branch information
attilapolakovics committed Jan 12, 2023
1 parent 47fb53b commit c90e944
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/craco-types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import type { Configuration as DevServerConfig } from 'webpack-dev-server';
import type {
BaseContext,
CraPaths,
DevServerContext,
JestContext,
WebpackContext,
Expand Down Expand Up @@ -101,4 +102,5 @@ export interface CracoConfig {
webpack?: CracoWebpackConfig;
devServer?: CracoDevServerConfig;
plugins?: CracoPluginDefinition<any>[];
paths?: Configure<CraPaths | undefined, BaseContext>;
}
28 changes: 21 additions & 7 deletions packages/craco/src/lib/cra.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
CracoConfig,
CraPaths,
DevServerConfigProvider,
JestConfigProvider,
} from '@craco/types';
Expand Down Expand Up @@ -62,7 +63,7 @@ function overrideModule(modulePath: string, newModule: any) {
throw new Error(`Module not found: ${modulePath}`);
}
require.cache[modulePath]!.exports = newModule;
log(`Overrided require cache for module: ${modulePath}`);
log(`Overrode require cache for module: ${modulePath}`);
}

function resolvePackageJson(cracoConfig: CracoConfig) {
Expand All @@ -89,14 +90,27 @@ export function getReactScriptVersion(cracoConfig: CracoConfig) {

let _resolvedCraPaths: any = null;

export function getCraPathsPath(cracoConfig: CracoConfig) {
return resolveConfigFilePath(cracoConfig, 'paths.js');
}

export function getCraPaths(cracoConfig: CracoConfig) {
if (!_resolvedCraPaths) {
_resolvedCraPaths = require(resolveConfigFilePath(cracoConfig, 'paths.js'));
_resolvedCraPaths = require(getCraPathsPath(cracoConfig));
}

return _resolvedCraPaths;
}

export function overrideCraPaths(
cracoConfig: CracoConfig,
newConfig?: CraPaths
) {
overrideModule(getCraPathsPath(cracoConfig), newConfig);

log('Overrode CRA paths config.');
}

/************ Webpack Dev Config ************/

function getWebpackDevConfigPath(cracoConfig: CracoConfig) {
Expand Down Expand Up @@ -137,7 +151,7 @@ export function overrideWebpackDevConfig(
overrideModule(result.filepath, () => newConfig);
}

log('Overrided Webpack dev config.');
log('Overrode Webpack dev config.');
}

/************ Webpack Prod Config ************/
Expand Down Expand Up @@ -180,7 +194,7 @@ export function overrideWebpackProdConfig(
overrideModule(result.filepath, () => newConfig);
}

log('Overrided Webpack prod config.');
log('Overrode Webpack prod config.');
}

/************ Dev Server Config ************/
Expand Down Expand Up @@ -211,7 +225,7 @@ export function overrideDevServerConfigProvider(

overrideModule(filepath, configProvider);

log('Overrided dev server config provider.');
log('Overrode dev server config provider.');
}

export function loadDevServerUtils() {
Expand All @@ -227,7 +241,7 @@ export function overrideDevServerUtils(newUtils: any) {

overrideModule(filepath, newUtils);

log('Overrided dev server utils.');
log('Overrode dev server utils.');
}

/************ Jest Config ************/
Expand Down Expand Up @@ -256,7 +270,7 @@ export function overrideJestConfigProvider(

overrideModule(filepath, configProvider);

log('Overrided Jest config provider.');
log('Overrode Jest config provider.');
}

/************ Scripts *******************/
Expand Down
22 changes: 22 additions & 0 deletions packages/craco/src/lib/features/paths/override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { BaseContext, CracoConfig, CraPaths } from '@craco/types';

import { overrideCraPaths } from '../../cra';
import { isFunction } from '../../utils';

export function overridePaths(cracoConfig: CracoConfig, context: BaseContext) {
let newConfig: CraPaths | undefined = context.paths;
if (cracoConfig.paths) {
if (isFunction(cracoConfig.paths)) {
newConfig = cracoConfig.paths(newConfig, context);
} else {
newConfig = {
...newConfig,
...cracoConfig.paths,
};
}

overrideCraPaths(cracoConfig, newConfig);
}

return newConfig;
}
2 changes: 2 additions & 0 deletions packages/craco/src/scripts/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { loadCracoConfigAsync } from '../lib/config';
import { getCraPaths, start } from '../lib/cra';
import { overrideDevServer } from '../lib/features/dev-server/override';
import { overrideWebpackDev } from '../lib/features/webpack/override';
import { overridePaths } from '../lib/features/paths/override';
import { log } from '../lib/logger';
import { validateCraVersion } from '../lib/validate-cra-version';

Expand All @@ -26,6 +27,7 @@ loadCracoConfigAsync(context).then((cracoConfig: CracoConfig) => {

context.paths = getCraPaths(cracoConfig);

context.paths = overridePaths(cracoConfig, context);
overrideWebpackDev(cracoConfig, context);
overrideDevServer(cracoConfig, context);

Expand Down

0 comments on commit c90e944

Please sign in to comment.