Skip to content

Commit 556840d

Browse files
committed
feat: Add method for execution after service.
1 parent a09f42e commit 556840d

5 files changed

Lines changed: 43 additions & 12 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ import { LoaderConfOptions, DevServerConfigFunction, MockerAPIOptions } from 'kk
128128

129129
type KKTRC = {
130130
proxySetup?: (app: express.Application) => MockerAPIOptions;
131-
devServer?: (configFunction: DevServerConfigFunction, evn: string,) => DevServerConfigFunction;
132-
default?: (conf: Configuration, evn: string, options: LoaderConfOptions) => Configuration;
133-
}
131+
devServer?: (configFunction: DevServerConfigFunction, evn: string) => DevServerConfigFunction;
132+
default?: (conf: Configuration, evn: string, options: LoaderConfOptions)
133+
=> Configuration & { config: Configuration, after: () => void };
134+
};
135+
134136
type DevServerConfigFunction = (proxy: WebpackDevServer.ProxyConfigArrayItem[], allowedHost: string)
135137
=> WebpackDevServer.Configuration;
136138
```

core/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ You can download the following examples directly. [Download page](https://kktjs.
8181
- [**`typescript`**](https://github.com/kktjs/kkt/tree/master/example/typescript) - Use an example of `TypeScript`. [`Open in CodeSandbox`](https://codesandbox.io/s/github/kktjs/kkt/tree/master/example/typescript)
8282
- [**`uiw`**](https://github.com/kktjs/kkt/tree/master/example/uiw) - Use [`uiw`](https://uiwjs.github.io/) for the project. [`Open in CodeSandbox`](https://codesandbox.io/s/github/kktjs/kkt/tree/master/example/uiw)
8383

84+
8485
## How to rewire your create-react-app project
8586

8687
> Create your app using [create-react-app](https://github.com/facebook/create-react-app) and then rewire it.
@@ -127,9 +128,11 @@ import { LoaderConfOptions, DevServerConfigFunction, MockerAPIOptions } from 'kk
127128

128129
type KKTRC = {
129130
proxySetup?: (app: express.Application) => MockerAPIOptions;
130-
devServer?: (configFunction: DevServerConfigFunction, evn: string,) => DevServerConfigFunction;
131-
default?: (conf: Configuration, evn: string, options: LoaderConfOptions) => Configuration;
132-
}
131+
devServer?: (configFunction: DevServerConfigFunction, evn: string) => DevServerConfigFunction;
132+
default?: (conf: Configuration, evn: string, options: LoaderConfOptions)
133+
=> Configuration & { config: Configuration, after: () => void };
134+
};
135+
133136
type DevServerConfigFunction = (proxy: WebpackDevServer.ProxyConfigArrayItem[], allowedHost: string)
134137
=> WebpackDevServer.Configuration;
135138
```

core/src/scripts/build.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,28 @@ export default async function build(argvs: ParsedArgs) {
1515
const kktrc: KKTRC = await overrides();
1616
const overridesHandle = kktrc.default || kktrc;
1717

18+
let afterHandle: () => void = undefined;
1819
if (overridesHandle && typeof overridesHandle === 'function') {
1920
// Source maps are resource heavy and can cause out of memory issue for large source files.
2021
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
2122
const webpackConf = miniCssExtractPlugin(webpackConfig('production'));
2223
// override config in memory
23-
require.cache[require.resolve(webpackConfigPath)].exports = (env: string) =>
24-
overridesHandle(webpackConf, env, { ...argvs, shouldUseSourceMap, paths, kktrc });
24+
require.cache[require.resolve(webpackConfigPath)].exports = (env: string) => {
25+
const conf = overridesHandle(webpackConf, env, { ...argvs, shouldUseSourceMap, paths, kktrc });
26+
if (conf && conf.config) {
27+
afterHandle = conf.after;
28+
return conf.config;
29+
}
30+
return conf;
31+
};
2532
}
2633

2734
// run original script
28-
require(`${reactScripts}/scripts/build`);
35+
await require(`${reactScripts}/scripts/build`);
36+
// Can be used for server-side rendering development
37+
if (afterHandle && typeof afterHandle === 'function') {
38+
await afterHandle();
39+
}
2940
} catch (error) {
3041
console.log('KKT:BUILD:ERROR:', error);
3142
}

core/src/scripts/start.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default async function build(argvs: ParsedArgs) {
3535
// Source maps are resource heavy and can cause out of memory issue for large source files.
3636
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
3737
const overridesHandle = kktrc.default || kktrc;
38+
let afterHandle: () => void = undefined;
3839
if (overridesHandle && typeof overridesHandle === 'function') {
3940
const webpackConf = miniCssExtractPlugin(webpackConfig('development'));
4041
// override config in memory
@@ -43,12 +44,21 @@ export default async function build(argvs: ParsedArgs) {
4344
if (kktrc && kktrc.proxySetup && typeof kktrc.proxySetup === 'function') {
4445
cacheData({ proxySetup: kktrc.proxySetup });
4546
}
46-
return overridesHandle(webpackConf, env, { ...argvs, shouldUseSourceMap, paths, devServerConfig, kktrc });
47+
const conf = overridesHandle(webpackConf, env, { ...argvs, shouldUseSourceMap, paths, devServerConfig, kktrc });
48+
if (conf && conf.config) {
49+
afterHandle = conf.after;
50+
return conf.config;
51+
}
52+
return conf;
4753
};
4854
}
4955

5056
// run original script
51-
require(`${reactScripts}/scripts/start`);
57+
await require(`${reactScripts}/scripts/start`);
58+
// Can be used for server-side rendering development
59+
if (afterHandle && typeof afterHandle === 'function') {
60+
await afterHandle();
61+
}
5262
} catch (error) {
5363
console.log('KKT:START:ERROR:', error);
5464
}

core/src/utils/loaderConf.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ export type DevServerConfigFunction = (
3939
proxy: WebpackDevServer.ProxyConfigArrayItem[],
4040
allowedHost: string,
4141
) => WebpackDevServer.Configuration;
42+
4243
export type KKTRC = {
4344
proxySetup?: (app: express.Application) => MockerAPIOptions;
4445
devServer?: (configFunction: DevServerConfigFunction, evn: string) => DevServerConfigFunction;
45-
default?: (conf: Configuration, evn: string, options: LoaderConfOptions) => Configuration;
46+
default?: (
47+
conf: Configuration,
48+
evn: string,
49+
options: LoaderConfOptions,
50+
) => Configuration & { config: Configuration; after: () => void };
4651
};
4752

4853
export async function loaderConf(rcPath: string): Promise<KKTRC> {

0 commit comments

Comments
 (0)