mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
chore(cli): improve build tasks
This commit is contained in:
parent
6104ecb352
commit
ed51b08011
@ -1,11 +1,10 @@
|
|||||||
// @ts-ignore
|
|
||||||
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 } 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';
|
||||||
|
import { installDependencies } from '../common/manager';
|
||||||
import { compileJs } from '../compiler/compile-js';
|
import { compileJs } from '../compiler/compile-js';
|
||||||
import { compileSfc } from '../compiler/compile-sfc';
|
import { compileSfc } from '../compiler/compile-sfc';
|
||||||
import { compileStyle } from '../compiler/compile-style';
|
import { compileStyle } from '../compiler/compile-style';
|
||||||
@ -23,7 +22,6 @@ import {
|
|||||||
isScript,
|
isScript,
|
||||||
isDemoDir,
|
isDemoDir,
|
||||||
isTestDir,
|
isTestDir,
|
||||||
hasYarn,
|
|
||||||
setNodeEnv,
|
setNodeEnv,
|
||||||
setModuleEnv
|
setModuleEnv
|
||||||
} from '../common';
|
} from '../common';
|
||||||
@ -64,116 +62,95 @@ async function compileDir(dir: string) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function installDependencies() {
|
|
||||||
consola.info('Install Dependencies');
|
|
||||||
console.log('');
|
|
||||||
|
|
||||||
try {
|
async function buildEs() {
|
||||||
const manager = hasYarn() ? 'yarn' : 'npm';
|
setModuleEnv('esmodule');
|
||||||
|
await copy(SRC_DIR, ES_DIR);
|
||||||
await execa(manager, ['install', '--prod=false'], {
|
await compileDir(ES_DIR);
|
||||||
stdio: 'inherit'
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('');
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildESModuleOutputs() {
|
async function buildLib() {
|
||||||
const spinner = ora('Build ESModule Outputs').start();
|
setModuleEnv('commonjs');
|
||||||
|
await copy(SRC_DIR, LIB_DIR);
|
||||||
try {
|
await compileDir(LIB_DIR);
|
||||||
setModuleEnv('esmodule');
|
|
||||||
await copy(SRC_DIR, ES_DIR);
|
|
||||||
await compileDir(ES_DIR);
|
|
||||||
spinner.succeed('Build ESModule Outputs');
|
|
||||||
} catch (err) {
|
|
||||||
spinner.fail('Build ESModule Outputs');
|
|
||||||
console.log(err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function buildCommonjsOutputs() {
|
|
||||||
const spinner = ora('Build Commonjs Outputs').start();
|
|
||||||
|
|
||||||
try {
|
|
||||||
setModuleEnv('commonjs');
|
|
||||||
await copy(SRC_DIR, LIB_DIR);
|
|
||||||
await compileDir(LIB_DIR);
|
|
||||||
spinner.succeed('Build Commonjs Outputs');
|
|
||||||
} catch (err) {
|
|
||||||
spinner.fail('Build Commonjs Outputs');
|
|
||||||
console.log(err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildStyleEntry() {
|
async function buildStyleEntry() {
|
||||||
const spinner = ora('Build Style Entry').start();
|
await genStyleDepsMap();
|
||||||
|
genComponentStyle();
|
||||||
try {
|
|
||||||
await genStyleDepsMap();
|
|
||||||
genComponentStyle();
|
|
||||||
spinner.succeed('Build Style Entry');
|
|
||||||
} catch (err) {
|
|
||||||
spinner.fail('Build Style Entry');
|
|
||||||
console.log(err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildPackedOutputs() {
|
async function buildPacakgeEntry() {
|
||||||
const spinner = ora('Build Packed Outputs').start();
|
const esEntryFile = join(ES_DIR, 'index.js');
|
||||||
|
const libEntryFile = join(LIB_DIR, 'index.js');
|
||||||
|
const styleEntryFile = join(LIB_DIR, `index.${CSS_LANG}`);
|
||||||
|
|
||||||
try {
|
genPackageEntry({
|
||||||
setModuleEnv('esmodule');
|
outputPath: esEntryFile,
|
||||||
await compilePackage(false);
|
pathResolver: (path: string) => `./${relative(SRC_DIR, path)}`
|
||||||
await compilePackage(true);
|
});
|
||||||
genVeturConfig();
|
|
||||||
spinner.succeed('Build Packed Outputs');
|
setModuleEnv('esmodule');
|
||||||
} catch (err) {
|
await compileJs(esEntryFile);
|
||||||
spinner.fail('Build Packed Outputs');
|
|
||||||
console.log(err);
|
genPacakgeStyle({
|
||||||
throw err;
|
outputPath: styleEntryFile,
|
||||||
}
|
pathResolver: (path: string) => path.replace(SRC_DIR, '.')
|
||||||
|
});
|
||||||
|
|
||||||
|
setModuleEnv('commonjs');
|
||||||
|
await copy(esEntryFile, libEntryFile);
|
||||||
|
await compileJs(libEntryFile);
|
||||||
|
await compileStyle(styleEntryFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildPackageEntry() {
|
async function buildPackages() {
|
||||||
const spinner = ora('Build Package Entry').start();
|
setModuleEnv('esmodule');
|
||||||
|
await compilePackage(false);
|
||||||
|
await compilePackage(true);
|
||||||
|
genVeturConfig();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
const tasks = [
|
||||||
const esEntryFile = join(ES_DIR, 'index.js');
|
{
|
||||||
const libEntryFile = join(LIB_DIR, 'index.js');
|
text: 'Build ESModule Outputs',
|
||||||
const styleEntryFile = join(LIB_DIR, `index.${CSS_LANG}`);
|
task: buildEs
|
||||||
|
},
|
||||||
genPackageEntry({
|
{
|
||||||
outputPath: esEntryFile,
|
text: 'Build Commonjs Outputs',
|
||||||
pathResolver: (path: string) => `./${relative(SRC_DIR, path)}`
|
task: buildLib
|
||||||
});
|
},
|
||||||
|
{
|
||||||
setModuleEnv('esmodule');
|
text: 'Build Style Entry',
|
||||||
await compileJs(esEntryFile);
|
task: buildStyleEntry
|
||||||
|
},
|
||||||
genPacakgeStyle({
|
{
|
||||||
outputPath: styleEntryFile,
|
text: 'Build Package Entry',
|
||||||
pathResolver: (path: string) => path.replace(SRC_DIR, '.')
|
task: buildPacakgeEntry
|
||||||
});
|
},
|
||||||
|
{
|
||||||
setModuleEnv('commonjs');
|
text: 'Build Packed Outputs',
|
||||||
await copy(esEntryFile, libEntryFile);
|
task: buildPackages
|
||||||
await compileJs(libEntryFile);
|
|
||||||
await compileStyle(styleEntryFile);
|
|
||||||
|
|
||||||
spinner.succeed('Build Package Entry');
|
|
||||||
} catch (err) {
|
|
||||||
spinner.fail('Build Package Entry');
|
|
||||||
console.log(err);
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
async function runBuildTasks() {
|
||||||
|
for (let i = 0; i < tasks.length; i++) {
|
||||||
|
const { task, text } = tasks[i];
|
||||||
|
const spinner = ora(text).start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
await task();
|
||||||
|
spinner.succeed(text);
|
||||||
|
} catch (err) {
|
||||||
|
spinner.fail(text);
|
||||||
|
console.log(err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
consola.success('Compile successfully');
|
||||||
}
|
}
|
||||||
|
|
||||||
function watchFileChange() {
|
function watchFileChange() {
|
||||||
@ -209,13 +186,7 @@ export async function build(cmd: { watch?: boolean } = {}) {
|
|||||||
try {
|
try {
|
||||||
await clean();
|
await clean();
|
||||||
await installDependencies();
|
await installDependencies();
|
||||||
await buildESModuleOutputs();
|
await runBuildTasks();
|
||||||
await buildCommonjsOutputs();
|
|
||||||
await buildStyleEntry();
|
|
||||||
await buildPackageEntry();
|
|
||||||
await buildPackedOutputs();
|
|
||||||
|
|
||||||
consola.success('Compile successfully');
|
|
||||||
|
|
||||||
if (cmd.watch) {
|
if (cmd.watch) {
|
||||||
watchFileChange();
|
watchFileChange();
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { execSync } from 'child_process';
|
|
||||||
import {
|
import {
|
||||||
lstatSync,
|
lstatSync,
|
||||||
existsSync,
|
existsSync,
|
||||||
@ -156,19 +155,4 @@ export function smartOutputFile(filePath: string, content: string) {
|
|||||||
outputFileSync(filePath, content);
|
outputFileSync(filePath, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
let hasYarnCache: boolean;
|
|
||||||
|
|
||||||
export function hasYarn() {
|
|
||||||
if (hasYarnCache === undefined) {
|
|
||||||
try {
|
|
||||||
execSync('yarn --version', { stdio: 'ignore' });
|
|
||||||
hasYarnCache = true;
|
|
||||||
} catch (e) {
|
|
||||||
hasYarnCache = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return hasYarnCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
export { getVantConfig };
|
export { getVantConfig };
|
||||||
|
36
packages/vant-cli/src/common/manager.ts
Normal file
36
packages/vant-cli/src/common/manager.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// @ts-ignore
|
||||||
|
import execa from 'execa';
|
||||||
|
import { consola } from './logger';
|
||||||
|
import { execSync } from 'child_process';
|
||||||
|
|
||||||
|
let hasYarnCache: boolean;
|
||||||
|
|
||||||
|
export function hasYarn() {
|
||||||
|
if (hasYarnCache === undefined) {
|
||||||
|
try {
|
||||||
|
execSync('yarn --version', { stdio: 'ignore' });
|
||||||
|
hasYarnCache = true;
|
||||||
|
} catch (e) {
|
||||||
|
hasYarnCache = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasYarnCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function installDependencies() {
|
||||||
|
consola.info('Install Dependencies\n');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const manager = hasYarn() ? 'yarn' : 'npm';
|
||||||
|
|
||||||
|
await execa(manager, ['install', '--prod=false'], {
|
||||||
|
stdio: 'inherit'
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('');
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user