feat(cli): support build types (#8264)

This commit is contained in:
neverland 2021-03-04 15:35:36 +08:00 committed by GitHub
parent 3fd2972b7c
commit e0eebbe982
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 38 deletions

View File

@ -1,6 +1,7 @@
import execa from 'execa';
import chokidar from 'chokidar'; import chokidar from 'chokidar';
import { join, relative } from 'path'; import { join, relative } from 'path';
import { remove, copy, readdirSync } from 'fs-extra'; import { remove, copy, readdirSync, existsSync } from 'fs-extra';
import { clean } from './clean'; import { clean } from './clean';
import { CSS_LANG } from '../common/css'; import { CSS_LANG } from '../common/css';
import { ora, consola, slimPath } from '../common/logger'; import { ora, consola, slimPath } from '../common/logger';
@ -63,50 +64,58 @@ async function compileDir(dir: string) {
); );
} }
async function buildEs() { async function copySourceCode() {
await copy(SRC_DIR, ES_DIR);
await copy(SRC_DIR, LIB_DIR);
}
async function buildESMOutputs() {
setModuleEnv('esmodule'); setModuleEnv('esmodule');
setBuildTarget('package'); setBuildTarget('package');
await copy(SRC_DIR, ES_DIR);
await compileDir(ES_DIR); await compileDir(ES_DIR);
} }
async function buildLib() { async function buildCJSOutputs() {
setModuleEnv('commonjs'); setModuleEnv('commonjs');
setBuildTarget('package'); setBuildTarget('package');
await copy(SRC_DIR, LIB_DIR);
await compileDir(LIB_DIR); await compileDir(LIB_DIR);
} }
async function buildTypeDeclarations() {
const tsConfig = join(process.cwd(), 'tsconfig.declaration.json');
if (existsSync(tsConfig)) {
await execa('tsc', ['-p', tsConfig]);
}
}
async function buildStyleEntry() { async function buildStyleEntry() {
await genStyleDepsMap(); await genStyleDepsMap();
genComponentStyle(); genComponentStyle();
} }
async function buildPackageEntry() { async function buildPackageScriptEntry() {
const esEntryFile = join(ES_DIR, 'index.js'); const esEntryFile = join(ES_DIR, 'index.js');
const libEntryFile = join(LIB_DIR, 'index.js'); const libEntryFile = join(LIB_DIR, 'index.js');
const styleEntryFile = join(LIB_DIR, `index.${CSS_LANG}`);
genPackageEntry({ genPackageEntry({
outputPath: esEntryFile, outputPath: esEntryFile,
pathResolver: (path: string) => `./${relative(SRC_DIR, path)}`, pathResolver: (path: string) => `./${relative(SRC_DIR, path)}`,
}); });
setModuleEnv('esmodule'); await copy(esEntryFile, libEntryFile);
await compileJs(esEntryFile); }
async function buildPackageStyleEntry() {
const styleEntryFile = join(LIB_DIR, `index.${CSS_LANG}`);
genPackageStyle({ genPackageStyle({
outputPath: styleEntryFile, outputPath: styleEntryFile,
pathResolver: (path: string) => path.replace(SRC_DIR, '.'), pathResolver: (path: string) => path.replace(SRC_DIR, '.'),
}); });
setModuleEnv('commonjs');
await copy(esEntryFile, libEntryFile);
await compileJs(libEntryFile);
await compileStyle(styleEntryFile);
} }
async function buildPackages() { async function buildBundledOutputs() {
setModuleEnv('esmodule'); setModuleEnv('esmodule');
await compilePackage(false); await compilePackage(false);
await compilePackage(true); await compilePackage(true);
@ -115,24 +124,36 @@ async function buildPackages() {
const tasks = [ const tasks = [
{ {
text: 'Build ESModule Outputs', text: 'Copy Source Code',
task: buildEs, task: copySourceCode,
}, },
{ {
text: 'Build Commonjs Outputs', text: 'Build Package Script Entry',
task: buildLib, task: buildPackageScriptEntry,
}, },
{ {
text: 'Build Style Entry', text: 'Build Component Style Entry',
task: buildStyleEntry, task: buildStyleEntry,
}, },
{ {
text: 'Build Package Entry', text: 'Build Package Style Entry',
task: buildPackageEntry, task: buildPackageStyleEntry,
}, },
{ {
text: 'Build Packed Outputs', text: 'Build Type Declarations',
task: buildPackages, task: buildTypeDeclarations,
},
{
text: 'Build ESModule Outputs',
task: buildESMOutputs,
},
{
text: 'Build CommonJS Outputs',
task: buildCJSOutputs,
},
{
text: 'Build Bundled Outputs',
task: buildBundledOutputs,
}, },
]; ];

View File

@ -4,8 +4,13 @@ import { replaceExt } from '../common';
import { replaceCssImportExt } from '../common/css'; import { replaceCssImportExt } from '../common/css';
import { replaceScriptImportExt } from './get-deps'; import { replaceScriptImportExt } from './get-deps';
export function compileJs(filePath: string): Promise<void> { export async function compileJs(filePath: string): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (filePath.includes('.d.ts')) {
resolve();
return;
}
let code = readFileSync(filePath, 'utf-8'); let code = readFileSync(filePath, 'utf-8');
code = replaceCssImportExt(code); code = replaceCssImportExt(code);

View File

@ -73,7 +73,7 @@ export default createComponent({
} }
}; };
const handleAction = (action: DialogAction) => { const getActionHandler = (action: DialogAction) => () => {
// should not trigger close event when hidden // should not trigger close event when hidden
if (!props.show) { if (!props.show) {
return; return;
@ -99,6 +99,9 @@ export default createComponent({
} }
}; };
const onCancel = getActionHandler('cancel');
const onConfirm = getActionHandler('confirm');
const renderTitle = () => { const renderTitle = () => {
const title = slots.title ? slots.title() : props.title; const title = slots.title ? slots.title() : props.title;
if (title) { if (title) {
@ -153,9 +156,7 @@ export default createComponent({
class={bem('cancel')} class={bem('cancel')}
style={{ color: props.cancelButtonColor }} style={{ color: props.cancelButtonColor }}
loading={loading.cancel} loading={loading.cancel}
onClick={() => { onClick={onCancel}
handleAction('cancel');
}}
/> />
)} )}
{props.showConfirmButton && ( {props.showConfirmButton && (
@ -165,9 +166,7 @@ export default createComponent({
class={[bem('confirm'), { [BORDER_LEFT]: props.showCancelButton }]} class={[bem('confirm'), { [BORDER_LEFT]: props.showCancelButton }]}
style={{ color: props.confirmButtonColor }} style={{ color: props.confirmButtonColor }}
loading={loading.confirm} loading={loading.confirm}
onClick={() => { onClick={onConfirm}
handleAction('confirm');
}}
/> />
)} )}
</div> </div>
@ -182,9 +181,7 @@ export default createComponent({
class={bem('cancel')} class={bem('cancel')}
color={props.cancelButtonColor} color={props.cancelButtonColor}
loading={loading.cancel} loading={loading.cancel}
onClick={() => { onClick={onCancel}
handleAction('cancel');
}}
/> />
)} )}
{props.showConfirmButton && ( {props.showConfirmButton && (
@ -194,9 +191,7 @@ export default createComponent({
class={bem('confirm')} class={bem('confirm')}
color={props.confirmButtonColor} color={props.confirmButtonColor}
loading={loading.confirm} loading={loading.confirm}
onClick={() => { onClick={onConfirm}
handleAction('confirm');
}}
/> />
)} )}
</ActionBar> </ActionBar>