feat(cli): optimize dist file path

This commit is contained in:
陈嘉涵 2019-11-29 11:56:55 +08:00
parent 19a1ab37ef
commit e2c9628316
3 changed files with 15 additions and 22 deletions

View File

@ -1,11 +1,10 @@
import glob from 'fast-glob';
import { join, parse, relative } from 'path';
import { join, parse } from 'path';
import { existsSync, writeFileSync } from 'fs-extra';
import { pascalize, removeExt, getComponents } from '../common';
import {
SRC_DIR,
DOCS_DIR,
DIST_DIR,
CONFIG_FILE,
DESKTOP_ENTRY_FILE
} from '../common/constant';
@ -36,10 +35,7 @@ function resolveDocuments(components: string[]): DocumentItem[] {
function genImportDocuments(items: DocumentItem[]) {
return items
.map(item => {
const relativePath = relative(DIST_DIR, item.path);
return `import ${item.name} from '${relativePath}';`;
})
.map(item => `import ${item.name} from '${item.path}';`)
.join('\n');
}
@ -50,8 +46,7 @@ function genExportDocuments(items: DocumentItem[]) {
}
function genImportConfig() {
const configRelative = relative(DIST_DIR, CONFIG_FILE);
return `import config from '${removeExt(configRelative)}';`;
return `import config from '${removeExt(CONFIG_FILE)}';`;
}
function genExportConfig() {

View File

@ -1,4 +1,4 @@
import { join, relative } from 'path';
import { join } from 'path';
import { existsSync, ensureDirSync, writeFileSync } from 'fs-extra';
import { decamelize, pascalize, removeExt, getComponents } from '../common';
import {
@ -16,6 +16,7 @@ type DemoItem = {
function genInstall() {
return `import Vue from 'vue';
import PackageEntry from './package-entry';
import './package-style';
Vue.use(PackageEntry);
`;
@ -23,10 +24,7 @@ Vue.use(PackageEntry);
function genImports(demos: DemoItem[]) {
return demos
.map(item => {
const relativePath = relative(DIST_DIR, item.path);
return `import ${item.name} from '${removeExt(relativePath)}';`;
})
.map(item => `import ${item.name} from '${removeExt(item.path)}';`)
.join('\n');
}
@ -41,7 +39,9 @@ function genConfig(demos: DemoItem[]) {
const demoNames = demos.map(item => decamelize(item.name, '-'));
CONFIG.site.nav = CONFIG.site.nav.filter((group: any) => {
group.items = group.items.filter((item: any) => (demoNames.includes(item.path)));
group.items = group.items.filter((item: any) =>
demoNames.includes(item.path)
);
return group.items.length;
});
@ -56,7 +56,9 @@ function genCode(components: string[]) {
}))
.filter(item => existsSync(item.path));
return `${genInstall()}\n${genImports(demos)}\n\n${genExports(demos)}\n${genConfig(demos)}\n`;
return `${genInstall()}\n${genImports(demos)}\n\n${genExports(
demos
)}\n${genConfig(demos)}\n`;
}
export function genMobileEntry() {

View File

@ -1,11 +1,10 @@
import { get } from 'lodash';
import { join, relative } from 'path';
import { join } from 'path';
import { writeFileSync } from 'fs-extra';
import { pascalize, getComponents, replaceExt } from '../common';
import {
CONFIG,
SRC_DIR,
DIST_DIR,
PACKAGE_JSON_FILE,
PACKAGE_ENTRY_FILE,
PACKAGE_STYLE_FILE,
@ -20,10 +19,7 @@ const version = process.env.PACKAGE_VERSION || packageJson.version;
function genImports(components: string[]): string {
return components
.map(name => {
const relativePath = relative(DIST_DIR, join(SRC_DIR, name));
return `import ${pascalize(name)} from '${relativePath}';`;
})
.map(name => `import ${pascalize(name)} from '${join(SRC_DIR, name)}';`)
.join('\n');
}
@ -44,7 +40,7 @@ function getStyleExt(): string {
function genStyleEntry() {
const ext = getStyleExt();
const content = styleDepsJson.sequence
.map((item: string) => `@import "./${item}/index${ext}";`)
.map((name: string) => `@import "${join(SRC_DIR, `${name}/index${ext}`)}";`)
.join('\n');
writeFileSync(replaceExt(PACKAGE_STYLE_FILE, ext), content);