feat(cli): build package style entry

This commit is contained in:
陈嘉涵
2019-11-29 11:37:48 +08:00
parent 964b2cf3bf
commit 19a1ab37ef
11 changed files with 66 additions and 27 deletions
@@ -2,10 +2,10 @@ import { join, relative } from 'path';
import { existsSync, ensureDirSync, writeFileSync } from 'fs-extra';
import { decamelize, pascalize, removeExt, getComponents } from '../common';
import {
CONFIG,
SRC_DIR,
DIST_DIR,
MOBILE_ENTRY_FILE,
CONFIG_FILE
MOBILE_ENTRY_FILE
} from '../common/constant';
type DemoItem = {
@@ -38,15 +38,14 @@ function genExports(demos: DemoItem[]) {
function genConfig(demos: DemoItem[]) {
// eslint-disable-next-line
const config = require(CONFIG_FILE);
const demoNames = demos.map(item => decamelize(item.name, '-'));
config.nav = config.nav.filter((group: any) => {
CONFIG.site.nav = CONFIG.site.nav.filter((group: any) => {
group.items = group.items.filter((item: any) => (demoNames.includes(item.path)));
return group.items.length;
});
return `export const config = ${JSON.stringify(config, null, 2)}`;
return `export const config = ${JSON.stringify(CONFIG, null, 2)}`;
}
function genCode(components: string[]) {
@@ -1,15 +1,21 @@
import { get } from 'lodash';
import { join, relative } from 'path';
import { writeFileSync } from 'fs-extra';
import { pascalize, getComponents } from '../common';
import { pascalize, getComponents, replaceExt } from '../common';
import {
CONFIG,
SRC_DIR,
DIST_DIR,
PACKAGE_JSON_FILE,
PACKAGE_ENTRY_FILE
PACKAGE_ENTRY_FILE,
PACKAGE_STYLE_FILE,
STYPE_DEPS_JSON_FILE
} from '../common/constant';
// eslint-disable-next-line
const packageJson = require(PACKAGE_JSON_FILE);
// eslint-disable-next-line
const styleDepsJson = require(STYPE_DEPS_JSON_FILE);
const version = process.env.PACKAGE_VERSION || packageJson.version;
function genImports(components: string[]): string {
@@ -25,7 +31,26 @@ function genExports(names: string[]): string {
return names.map(name => `${name}`).join(',\n ');
}
export function genPackageEntry() {
function getStyleExt(): string {
const preprocessor = get(CONFIG, 'build.css.preprocessor', 'less');
if (preprocessor === 'sass') {
return '.scss';
}
return `.${preprocessor}`;
}
function genStyleEntry() {
const ext = getStyleExt();
const content = styleDepsJson.sequence
.map((item: string) => `@import "./${item}/index${ext}";`)
.join('\n');
writeFileSync(replaceExt(PACKAGE_STYLE_FILE, ext), content);
}
function genScriptEntry() {
const components = getComponents();
const names = components.map(item => pascalize(item));
@@ -64,3 +89,8 @@ export default {
writeFileSync(PACKAGE_ENTRY_FILE, content);
}
export function genPackageEntry() {
genStyleEntry();
genScriptEntry();
}