mirror of
https://github.com/youzan/vant.git
synced 2025-10-20 02:31:21 +00:00
chore(cli): extract create-vant-cli-app package
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import webpack from 'webpack';
|
||||
import { packageConfig } from '../config/webpack.package';
|
||||
import { getPackageConfig } from '../config/webpack.package';
|
||||
|
||||
export async function compilePackage(isMinify: boolean) {
|
||||
return new Promise((resolve, reject) => {
|
||||
webpack(packageConfig(isMinify), (err, stats) => {
|
||||
const config = getPackageConfig(isMinify);
|
||||
|
||||
webpack(config, (err, stats) => {
|
||||
if (err || stats.hasErrors()) {
|
||||
reject();
|
||||
} else {
|
||||
|
@@ -5,8 +5,8 @@ import WebpackDevServer from 'webpack-dev-server';
|
||||
import { get } from 'lodash';
|
||||
import { getPort } from 'portfinder';
|
||||
import { GREEN } from '../common/constant';
|
||||
import { siteDevConfig } from '../config/webpack.site.dev';
|
||||
import { sitePrdConfig } from '../config/webpack.site.prd';
|
||||
import { getSiteDevConfig } from '../config/webpack.site.dev';
|
||||
import { getSitePrdConfig } from '../config/webpack.site.prd';
|
||||
|
||||
function logServerInfo(port: number) {
|
||||
const local = `http://localhost:${port}/`;
|
||||
@@ -17,16 +17,16 @@ function logServerInfo(port: number) {
|
||||
console.log(` ${chalk.bold('Network')}: ${chalk.hex(GREEN)(network)}`);
|
||||
}
|
||||
|
||||
function runDevServer(port: number) {
|
||||
const server = new WebpackDevServer(
|
||||
webpack(siteDevConfig),
|
||||
siteDevConfig.devServer
|
||||
);
|
||||
function runDevServer(
|
||||
port: number,
|
||||
config: ReturnType<typeof getSiteDevConfig>
|
||||
) {
|
||||
const server = new WebpackDevServer(webpack(config), config.devServer);
|
||||
|
||||
// this is a hack to disable wds status log
|
||||
(server as any).showStatus = function() {};
|
||||
|
||||
const host = get(siteDevConfig.devServer, 'host', 'localhost');
|
||||
const host = get(config.devServer, 'host', 'localhost');
|
||||
server.listen(port, host, (err?: Error) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
@@ -35,9 +35,11 @@ function runDevServer(port: number) {
|
||||
}
|
||||
|
||||
function watch() {
|
||||
const config = getSiteDevConfig();
|
||||
|
||||
getPort(
|
||||
{
|
||||
port: siteDevConfig.devServer!.port
|
||||
port: config.devServer!.port
|
||||
},
|
||||
(err, port) => {
|
||||
if (err) {
|
||||
@@ -46,14 +48,16 @@ function watch() {
|
||||
}
|
||||
|
||||
logServerInfo(port);
|
||||
runDevServer(port);
|
||||
runDevServer(port, config);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function build() {
|
||||
return new Promise((resolve, reject) => {
|
||||
webpack(sitePrdConfig, (err, stats) => {
|
||||
const config = getSitePrdConfig();
|
||||
|
||||
webpack(config, (err, stats) => {
|
||||
if (err || stats.hasErrors()) {
|
||||
reject();
|
||||
} else {
|
||||
|
@@ -5,8 +5,6 @@ import { getDeps, clearDepsCache, fillExt } from './get-deps';
|
||||
import { getComponents, smartOutputFile } from '../common';
|
||||
import { SRC_DIR, STYPE_DEPS_JSON_FILE } from '../common/constant';
|
||||
|
||||
const components = getComponents();
|
||||
|
||||
function matchPath(path: string, component: string): boolean {
|
||||
return path
|
||||
.replace(SRC_DIR, '')
|
||||
@@ -23,7 +21,7 @@ export function checkStyleExists(component: string) {
|
||||
}
|
||||
|
||||
// analyze component dependencies
|
||||
function analyzeComponentDeps(component: string) {
|
||||
function analyzeComponentDeps(components: string[], component: string) {
|
||||
const checkList: string[] = [];
|
||||
const componentEntry = fillExt(join(SRC_DIR, component, 'index'));
|
||||
const record = new Set();
|
||||
@@ -54,7 +52,7 @@ function analyzeComponentDeps(component: string) {
|
||||
|
||||
type DepsMap = Record<string, string[]>;
|
||||
|
||||
function getSequence(depsMap: DepsMap) {
|
||||
function getSequence(components: string[], depsMap: DepsMap) {
|
||||
const sequence: string[] = [];
|
||||
const record = new Set();
|
||||
|
||||
@@ -94,16 +92,18 @@ function getSequence(depsMap: DepsMap) {
|
||||
}
|
||||
|
||||
export async function genStyleDepsMap() {
|
||||
const components = getComponents();
|
||||
|
||||
return new Promise(resolve => {
|
||||
clearDepsCache();
|
||||
|
||||
const map = {} as DepsMap;
|
||||
|
||||
components.forEach(component => {
|
||||
map[component] = analyzeComponentDeps(component);
|
||||
map[component] = analyzeComponentDeps(components, component);
|
||||
});
|
||||
|
||||
const sequence = getSequence(map);
|
||||
const sequence = getSequence(components, map);
|
||||
|
||||
Object.keys(map).forEach(key => {
|
||||
map[key] = map[key].sort(
|
||||
|
@@ -1,72 +0,0 @@
|
||||
import chalk from 'chalk';
|
||||
import { join } from 'path';
|
||||
import { consola, slimPath } from '../common/logger';
|
||||
import { CWD, GENERATOR_DIR } from '../common/constant';
|
||||
import Generator from 'yeoman-generator';
|
||||
|
||||
const TEMPLATES = join(GENERATOR_DIR, 'templates');
|
||||
const PROMPTS = [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'name',
|
||||
message: 'Your package name'
|
||||
}
|
||||
];
|
||||
|
||||
export class VantCliGenerator extends Generator {
|
||||
inputs = {
|
||||
name: ''
|
||||
};
|
||||
|
||||
async prompting() {
|
||||
return this.prompt(PROMPTS).then(inputs => {
|
||||
this.inputs = inputs as any;
|
||||
});
|
||||
}
|
||||
|
||||
writing() {
|
||||
consola.info(`Creating project in ${slimPath(CWD)}\n`);
|
||||
|
||||
const copy = (from: string, to?: string) => {
|
||||
this.fs.copy(join(TEMPLATES, from), this.destinationPath(to || from));
|
||||
};
|
||||
|
||||
const copyTpl = (name: string) => {
|
||||
this.fs.copyTpl(
|
||||
join(TEMPLATES, name),
|
||||
this.destinationPath(name),
|
||||
this.inputs
|
||||
);
|
||||
};
|
||||
|
||||
copy('.gitignore');
|
||||
copy('.eslintignore');
|
||||
copy('babel.config.js');
|
||||
copy('src/**/*', 'src');
|
||||
copy('docs/**/*', 'docs');
|
||||
copyTpl('package.json');
|
||||
copyTpl('vant.config.js');
|
||||
}
|
||||
|
||||
install() {
|
||||
console.log();
|
||||
consola.info('Install dependencies...\n');
|
||||
|
||||
this.installDependencies({
|
||||
npm: false,
|
||||
bower: false,
|
||||
yarn: true,
|
||||
skipMessage: true
|
||||
});
|
||||
}
|
||||
|
||||
end() {
|
||||
console.log();
|
||||
consola.success(
|
||||
`Successfully created project ${chalk.yellow(this.inputs.name)}.`
|
||||
);
|
||||
consola.success(
|
||||
`Now you can run ${chalk.yellow('yarn dev')} to start development!`
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user