Skip to content

Commit

Permalink
feat(kkt): Override webpack config support promise.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jan 22, 2021
1 parent b9f35e5 commit deba4bc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
5 changes: 5 additions & 0 deletions core/src/overrides/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export type OverridePaths = Paths & {
_oldPaths: OverridePaths;
};

/**
* Override Paths
* @param {ParsedArgs | undefined} argvs
* @param {Record<string, string>} opts
*/
export const overridePaths = (
argvs = undefined as ParsedArgs | undefined,
opts: Record<string, string> = {},
Expand Down
9 changes: 7 additions & 2 deletions core/src/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ export default async function build(argvs: ParsedArgs) {
// Source maps are resource heavy and can cause out of memory issue for large source files.
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
const webpackConf = miniCssExtractPlugin(webpackConfig('production'));
const overrideWebpackConf = await overridesHandle(webpackConf, 'production', {
...argvs,
shouldUseSourceMap,
paths,
kktrc,
});
// override config in memory
require.cache[require.resolve(webpackConfigPath)].exports = (env: string) =>
overridesHandle(webpackConf, env, { ...argvs, shouldUseSourceMap, paths, kktrc });
require.cache[require.resolve(webpackConfigPath)].exports = (env: string) => overrideWebpackConf;
}

// run original script
Expand Down
19 changes: 12 additions & 7 deletions core/src/scripts/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ export default async function build(argvs: ParsedArgs) {
const overridesHandle = (kktrc.default || kktrc) as KKTRC['default'];
if (overridesHandle && typeof overridesHandle === 'function') {
const webpackConf = miniCssExtractPlugin(webpackConfig('development'));
await overridePaths(undefined, { proxySetup });
if (kktrc && kktrc.proxySetup && typeof kktrc.proxySetup === 'function') {
cacheData({ proxySetup: kktrc.proxySetup });
}
const overrideWebpackConf = await overridesHandle(webpackConf, 'development', {
...argvs,
shouldUseSourceMap,
paths,
devServerConfigHandle,
kktrc,
});
// override config in memory
require.cache[require.resolve(webpackConfigPath)].exports = (env: string) => {
overridePaths(undefined, { proxySetup });
if (kktrc && kktrc.proxySetup && typeof kktrc.proxySetup === 'function') {
cacheData({ proxySetup: kktrc.proxySetup });
}
return overridesHandle(webpackConf, env, { ...argvs, shouldUseSourceMap, paths, devServerConfigHandle, kktrc });
};
require.cache[require.resolve(webpackConfigPath)].exports = (env: string) => overrideWebpackConf;
}

// run original script
Expand Down
2 changes: 1 addition & 1 deletion core/src/utils/loaderConf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type DevServerConfigFunction = (
export type KKTRC = {
proxySetup?: (app: express.Application) => MockerAPIOptions;
devServer?: (config: WebpackDevServer.Configuration) => WebpackDevServer.Configuration;
default?: (conf: Configuration, evn: string, options: LoaderConfOptions) => Configuration;
default?: (conf: Configuration, evn: string, options: LoaderConfOptions) => Configuration | Promise<Configuration>;
};

export async function loaderConf(rcPath: string): Promise<KKTRC> {
Expand Down
2 changes: 1 addition & 1 deletion core/src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const reactDevUtils = path.join(require.resolve('react-dev-utils/package.json'),
const paths: Paths = require(`${reactScripts}/config/paths`);

/**
* overrides proxySetup path
* Overrides proxySetup path
* `<root path>/node_modules/kkt/lib/utils/proxySetup.js`
*/
const proxySetup = path.resolve(__dirname, './proxySetup.js');
Expand Down

2 comments on commit deba4bc

@vercel
Copy link

@vercel vercel bot commented on deba4bc Jan 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kkt-basic-example – ./

kkt-basic-example.398188662.vercel.app
kkt-basic-example-git-master.398188662.vercel.app
kkt.vercel.app

@vercel
Copy link

@vercel vercel bot commented on deba4bc Jan 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kkt-bundle-example – ./

kkt.vercel.app
kkt-bundle-example.398188662.vercel.app
kkt-bundle-example-git-master.398188662.vercel.app

Please sign in to comment.