Skip to content

Commit

Permalink
Support custom command.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 8, 2019
1 parent efce061 commit 2d254f4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"keywords": [
"kkt",
"react",
"react-native",
"redux",
"rematch",
"uiw",
Expand Down
23 changes: 15 additions & 8 deletions src/command/build/handlerBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import checkRequiredFiles from 'react-dev-utils/checkRequiredFiles';
import { measureFileSizesBeforeBuild, printFileSizesAfterBuild} from 'react-dev-utils/FileSizeReporter';
import printHostingInstructions from 'react-dev-utils/printHostingInstructions';
import { checkBrowsers } from 'react-dev-utils/browsersHelper';
import build from './build';
import configFactory from '../../config/webpack.config';
import * as paths from '../../config/paths';
import build from './build';
import { IMyYargsArgs } from '../../type/type';

const useYarn = fs.existsSync(paths.yarnLockFile);

Expand All @@ -29,24 +30,30 @@ function copyPublicFolder() {
}

const isInteractive = process.stdout.isTTY;

export default async () => {
export default async (args: IMyYargsArgs) => {
// Generate configuration
const config = await configFactory('production');
const config = await configFactory('production', args);
// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
process.exit(1);
}

let appBuild = paths.appBuild;
if (config.output && config.output.path) {
appBuild = config.output.path;
}

await checkBrowsers(paths.appPath, isInteractive);
// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
if (args.emptyDir) {
await fs.emptyDir(appBuild);
}

// First, read the current file sizes in build directory.
// This lets us display how much they changed later.
const previousFileSizes = await measureFileSizesBeforeBuild(paths.appBuild);
const previousFileSizes = await measureFileSizesBeforeBuild(appBuild);

// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
await fs.emptyDir(paths.appBuild);
// Merge with the public folder
copyPublicFolder();

Expand Down
16 changes: 8 additions & 8 deletions src/command/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export const describe = 'Builds the app for production to the dist folder.';

export function builder(yarg: IMyYargsArgs) {
return yarg.option({
emptyDir: {
alias: 'e',
describe: 'Empty the DIST directory before compiling.',
default: true,
},
})
.example('$ kkt build ', 'Build your project.')
.example('$ kkt build --no-emptyDir', 'Build your project.');
emptyDir: {
alias: 'e',
describe: 'Empty the DIST directory before compiling.',
default: true,
},
})
.example('$ kkt build ', 'Build your project.')
.example('$ kkt build --no-emptyDir', 'Build your project.');
}

export const handler = handlerBuild;
5 changes: 3 additions & 2 deletions src/command/start/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { choosePort, createCompiler, prepareProxy, prepareUrls } from 'react-dev
import configFactory from '../../config/webpack.config';
import createDevServerConfig from '../../config/webpack.config.server';
import * as paths from '../../config/paths';
import { IMyYargsArgs } from '../../type/type';


// We require that you explicitly set browsers and do not fall back to
Expand All @@ -36,7 +37,7 @@ process.on('unhandledRejection', err => {
throw err;
});

export default async function () {
export default async function (args: IMyYargsArgs) {
// Tools like Cloud9 rely on this.
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 19870;
const HOST = process.env.HOST || '0.0.0.0';
Expand All @@ -54,7 +55,7 @@ export default async function () {
try {
await checkBrowsers(paths.appPath, isInteractive);
const PORT = await choosePort(HOST, DEFAULT_PORT);
const config = await configFactory('development');
const config = await configFactory('development', args);
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
const appName = require(paths.appPackageJson as string).name;
const useTypeScript = fs.existsSync(paths.appTsConfig);
Expand Down
11 changes: 9 additions & 2 deletions src/config/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';
import * as paths from './paths';
import { ClientEnvironment } from '../type/type';
import loadConfHandle from '../utils/loadConf';
import { IMyYargsArgs } from '../type/type';

export interface OptionConf {
/**
Expand All @@ -17,6 +18,7 @@ export interface OptionConf {
publicPath: string;
publicUrl: string;
useTypeScript: boolean;
yargsArgs: IMyYargsArgs;
paths: {
moduleFileExtensions: string[];
};
Expand All @@ -28,7 +30,8 @@ const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
// Check if TypeScript is setup
const useTypeScript = fs.existsSync(paths.appTsConfig);

export default async (env: string = 'development', conf: Configuration = {}) => {
export default async (env: string = 'development', args?: IMyYargsArgs) => {
let conf: Configuration = {};
const isEnvDevelopment = env === 'development';
const isEnvProduction = env === 'production';

Expand Down Expand Up @@ -125,7 +128,11 @@ export default async (env: string = 'development', conf: Configuration = {}) =>
}
// =============================================
// Disable require.ensure as it's not a standard language feature.
const optionConf: OptionConf = { env, dotenv, paths, isEnvDevelopment, isEnvProduction, shouldUseSourceMap, publicPath, publicUrl, useTypeScript };
const optionConf: OptionConf = {
env, dotenv, paths, isEnvDevelopment, isEnvProduction,
shouldUseSourceMap, publicPath, publicUrl, useTypeScript,
yargsArgs: args,
};

conf = require('../plugs/optimization')(conf, optionConf);
conf = require('../plugs/resolve')(conf, optionConf);
Expand Down
2 changes: 1 addition & 1 deletion src/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface IMyYargsArgs extends Arguments {

export interface ClientEnvironment {
raw: {
NODE_ENV?: 'development' | 'production';
NODE_ENV?: 'development' | 'production' | string;
PUBLIC_URL?: string;
IMAGE_INLINE_SIZE_LIMIT?: string;
},
Expand Down

0 comments on commit 2d254f4

Please sign in to comment.