diff --git a/core/package.json b/core/package.json index ca9385ae..f528b307 100644 --- a/core/package.json +++ b/core/package.json @@ -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", diff --git a/core/src/index.ts b/core/src/index.ts index 5debc2a3..a04ba808 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -11,6 +11,10 @@ import { Paths } from './utils/path'; export interface BuildArgs extends ParsedArgs { isNotCheckHTML?: boolean; overridePaths?: Partial; + /** + * Specify the configuration name. E.g: `.kktrc` + */ + configName?: string; overridesWebpack?: ( conf: WebpackConfiguration, env: 'development' | 'production', diff --git a/core/src/scripts/build.ts b/core/src/scripts/build.ts index 14d513bb..96975ff8 100644 --- a/core/src/scripts/build.ts +++ b/core/src/scripts/build.ts @@ -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'; @@ -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') { diff --git a/core/src/scripts/start.ts b/core/src/scripts/start.ts index 6a5fb8a8..81c5bdf9 100644 --- a/core/src/scripts/start.ts +++ b/core/src/scripts/start.ts @@ -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'; @@ -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); diff --git a/core/src/utils/path.ts b/core/src/utils/path.ts index 9ea256f1..9da44fb2 100644 --- a/core/src/utils/path.ts +++ b/core/src/utils/path.ts @@ -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` 配置文件 - * `/.kktrc` - * - * ```js - * { - * "kkt": { - * "path": "./config/.kktrc" - * } - * } - * ``` - */ -let configOverrides = customOpts.path ? `${projectDir}/${customOpts.path}` : `${projectDir}/.kktrc`; +function getConfPath(confName = '.kktrc') { + /** + * 默认从 package.json 指定配置文件目录和当前目录根目录 `.kktrc` 配置文件 + * `/.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); } /** @@ -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 };