feat(cli): support locales

This commit is contained in:
陈嘉涵
2019-12-06 15:51:20 +08:00
parent 6bd858fac5
commit dc6cc6c5af
16 changed files with 336 additions and 135 deletions

View File

@@ -67,12 +67,8 @@ type CompileSfcOptions = {
skipStyle?: boolean;
};
export async function compileSfc(
filePath: string,
options: CompileSfcOptions = {}
): Promise<any> {
export function parseSfc(filePath: string) {
const source = readFileSync(filePath, 'utf-8');
const jsFilePath = replaceExt(filePath, '.js');
const descriptor = compileUtils.parse({
source,
@@ -80,9 +76,17 @@ export async function compileSfc(
needMap: false
} as any);
const { template, styles } = descriptor;
return descriptor;
}
export async function compileSfc(
filePath: string,
options: CompileSfcOptions = {}
): Promise<any> {
const tasks = [remove(filePath)];
const jsFilePath = replaceExt(filePath, '.js');
const descriptor = parseSfc(filePath);
const { template, styles } = descriptor;
// compile js part
if (descriptor.script) {

View File

@@ -4,6 +4,7 @@ import { existsSync } from 'fs-extra';
import {
pascalize,
removeExt,
getVantConfig,
getComponents,
smartOutputFile
} from '../common';
@@ -19,23 +20,59 @@ type DocumentItem = {
path: string;
};
function formatName(component: string, lang?: string) {
component = pascalize(component);
if (lang) {
return `${component}_${lang.replace('-', '_')}`;
}
return component;
}
/**
* i18n mode:
* - action-sheet/README.md => ActionSheet_EnUS
* - action-sheet/README.zh-CN.md => ActionSheet_ZhCN
*
* default mode:
* - action-sheet/README.md => ActionSheet
*/
function resolveDocuments(components: string[]): DocumentItem[] {
const componentDocs = components
.filter(component => {
const absolutePath = join(SRC_DIR, component, 'README.md');
return existsSync(absolutePath);
})
.map(component => ({
name: pascalize(component),
path: join(SRC_DIR, component, 'README.md')
}));
const vantConfig = getVantConfig();
const { locales, defaultLang } = vantConfig.site;
const staticDocs = glob.sync(join(DOCS_DIR, '**/*.md')).map(path => ({
name: pascalize(parse(path).name),
path
}));
const docs: DocumentItem[] = [];
return [...componentDocs, ...staticDocs];
if (locales) {
const langs = Object.keys(locales);
langs.forEach(lang => {
const fileName = lang === defaultLang ? 'README.md' : `README.${lang}.md`;
components.forEach(component => {
docs.push({
name: formatName(component, lang),
path: join(SRC_DIR, component, fileName)
});
});
});
} else {
components.forEach(component => {
docs.push({
name: formatName(component),
path: join(SRC_DIR, component, 'README.md')
});
});
}
const staticDocs = glob.sync(join(DOCS_DIR, '**/*.md')).map(path => {
const pairs = parse(path).name.split('.');
return {
name: formatName(pairs[0], pairs[1] || defaultLang),
path
};
});
return [...staticDocs, ...docs.filter(item => existsSync(item.path))];
}
function genImportDocuments(items: DocumentItem[]) {

View File

@@ -40,12 +40,25 @@ function genConfig(demos: DemoItem[]) {
const vantConfig = getVantConfig();
const demoNames = demos.map(item => decamelize(item.name, '-'));
vantConfig.site.nav = vantConfig.site.nav.filter((group: any) => {
group.items = group.items.filter((item: any) =>
demoNames.includes(item.path)
);
return group.items.length;
});
function demoFilter(nav: any[]) {
return nav.filter(group => {
group.items = group.items.filter((item: any) =>
demoNames.includes(item.path)
);
return group.items.length;
});
}
const { nav, locales } = vantConfig.site;
if (locales) {
Object.keys(locales).forEach((lang: string) => {
if (locales[lang].nav) {
locales[lang].nav = demoFilter(locales[lang].nav);
}
});
} else if (nav) {
vantConfig.site.nav = demoFilter(nav);
}
return `export const config = ${JSON.stringify(vantConfig, null, 2)}`;
}