Skip to content

Commit

Permalink
feat: build/start add configName options.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Nov 15, 2022
1 parent 99752f1 commit 0d1a6ae
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions core/package.json
Expand Up @@ -6,6 +6,7 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"start": "npm run watch",
"build": "tsbb build --file-names src/bin/kkt.ts --file-names src/scripts/build.ts --file-names src/scripts/start.ts --file-names src/scripts/testk.ts --no-esm",
"watch": "tsbb watch --file-names src/bin/kkt.ts --file-names src/scripts/build.ts --file-names src/scripts/start.ts --file-names src/scripts/testk.ts --no-esm",
"test": "tsbb test",
Expand Down
4 changes: 4 additions & 0 deletions core/src/index.ts
Expand Up @@ -11,6 +11,10 @@ import { Paths } from './utils/path';
export interface BuildArgs extends ParsedArgs {
isNotCheckHTML?: boolean;
overridePaths?: Partial<Paths>;
/**
* Specify the configuration name. E.g: `.kktrc`
*/
configName?: string;
overridesWebpack?: (
conf: WebpackConfiguration,
env: 'development' | 'production',
Expand Down
4 changes: 2 additions & 2 deletions core/src/scripts/build.ts
Expand Up @@ -2,7 +2,7 @@ process.env.NODE_ENV = 'production';

import { Configuration } from 'webpack';
import { KKTRC, WebpackConfiguration, loaderConf } from '../utils/loaderConf';
import { reactScripts, isWebpackFactory, configOverrides } from '../utils/path';
import { reactScripts, isWebpackFactory, getConfPath } from '../utils/path';
import { overridePaths } from '../overrides/paths';
import { miniCssExtractPlugin } from '../plugins/miniCssExtractPlugin';
import { checkRequiredFiles } from '../overrides/checkRequired';
Expand All @@ -16,7 +16,7 @@ export default async function build(argvs: BuildArgs) {
await checkRequiredFiles(paths, isNotCheckHTML);
const webpackConfigPath = `${reactScripts}/config/webpack.config${!isWebpackFactory ? '.prod' : ''}`;
const createWebpackConfig: (env: string) => Configuration = require(webpackConfigPath);
const kktrc: KKTRC = await loaderConf(configOverrides);
const kktrc: KKTRC = await loaderConf(getConfPath(argvs.configName));
const overridesHandle = kktrc.default || argvs.overridesWebpack;

if (overridesHandle && typeof overridesHandle === 'function') {
Expand Down
4 changes: 2 additions & 2 deletions core/src/scripts/start.ts
Expand Up @@ -8,7 +8,7 @@ import redirectServedPath from 'react-dev-utils/redirectServedPathMiddleware';
import clearConsole from 'react-dev-utils/clearConsole';
import noopServiceWorkerMiddleware from 'react-dev-utils/noopServiceWorkerMiddleware';
import { KKTRC, DevServerConfigFunction, WebpackConfiguration, loaderConf } from '../utils/loaderConf';
import { reactScripts, isWebpackFactory, proxySetup, configOverrides } from '../utils/path';
import { reactScripts, isWebpackFactory, proxySetup, getConfPath } from '../utils/path';
import { overridePaths } from '../overrides/paths';
import { overridesOpenBrowser } from '../overrides/openBrowser';
import { overridesClearConsole } from '../overrides/clearConsole';
Expand All @@ -31,7 +31,7 @@ export default async function start(argvs: StartArgs) {
const createWebpackConfig: (env: string) => Configuration = require(webpackConfigPath);
const createDevServerConfig: DevServerConfigFunction = require(devServerConfigPath);
require('react-scripts/config/env');
const kktrc: KKTRC = await loaderConf(configOverrides);
const kktrc: KKTRC = await loaderConf(getConfPath(argvs.configName));
await overridesClearConsole(argvs);
await overridesOpenBrowser(argvs);

Expand Down
42 changes: 26 additions & 16 deletions core/src/utils/path.ts
Expand Up @@ -35,22 +35,26 @@ const argvs = minimist(args);
const projectDir = path.resolve(fs.realpathSync(process.cwd()));
const customOpts = require(path.resolve(projectDir, 'package.json'))['kkt'] || {};

/**
* 默认从 package.json 指定配置文件目录和当前目录根目录 `.kktrc` 配置文件
* `<Project Root Path>/.kktrc`
*
* ```js
* {
* "kkt": {
* "path": "./config/.kktrc"
* }
* }
* ```
*/
let configOverrides = customOpts.path ? `${projectDir}/${customOpts.path}` : `${projectDir}/.kktrc`;
function getConfPath(confName = '.kktrc') {
/**
* 默认从 package.json 指定配置文件目录和当前目录根目录 `.kktrc` 配置文件
* `<Project Root Path>/.kktrc`
*
* ```js
* {
* "kkt": {
* "path": "./config/.kktrc"
* }
* }
* ```
*/
let confPath = customOpts.path ? `${projectDir}/${customOpts.path}` : `${projectDir}/${confName}`;

if (argvs['config-overrides']) {
configOverrides = path.resolve(argvs['config-overrides']);
if (argvs['config-overrides']) {
confPath = path.resolve(argvs['config-overrides']);
}
confPath = confPath.replace(/\.(js|ts)$/gi, '');
return path.resolve(confPath);
}

/**
Expand Down Expand Up @@ -83,4 +87,10 @@ const scriptPkg = require(`${reactScripts}/package.json`);
*/
const isWebpackFactory = semver.gte(scriptPkg && scriptPkg.version, '2.1.2');

export { proxySetup, projectDir, reactScripts, reactDevUtils, configOverrides, isWebpackFactory, paths };
/**
* Compatible API
* @deprecated
*/
const configOverrides = getConfPath();

export { proxySetup, projectDir, reactScripts, reactDevUtils, configOverrides, getConfPath, isWebpackFactory, paths };

0 comments on commit 0d1a6ae

Please sign in to comment.