feat(cli): reload vant config

This commit is contained in:
陈嘉涵 2019-12-05 10:11:22 +08:00
parent b19b287276
commit 207089b2ca
8 changed files with 39 additions and 39 deletions

View File

@ -6,7 +6,7 @@ export const LIB_DIR = join(CWD, 'lib');
export const SRC_DIR = join(CWD, 'src');
export const DOCS_DIR = join(CWD, 'docs');
export const SITE_DIST_DIR = join(CWD, 'site');
export const CONFIG_FILE = join(CWD, 'vant.config.js');
export const VANT_CONFIG_FILE = join(CWD, 'vant.config.js');
export const PACKAGE_JSON_FILE = join(CWD, 'package.json');
export const WEBPACK_CONFIG_FILE = join(CWD, 'webpack.config.js');
@ -31,5 +31,4 @@ export const JEST_STYLE_MOCK_FILE = join(CONFIG_DIR, 'jest.style-mock.js');
export const SCRIPT_EXTS = ['.js', '.jsx', '.vue', '.ts', '.tsx'];
export const STYLE_EXTS = ['.css', '.less', '.scss'];
export const CONFIG = require(CONFIG_FILE);
export const PACKAGE_JSON = require(PACKAGE_JSON_FILE);

View File

@ -1,12 +1,14 @@
import { get } from 'lodash';
import { join, isAbsolute } from 'path';
import { CONFIG, STYLE_DIR, SRC_DIR } from './constant';
import { existsSync } from 'fs';
import { join, isAbsolute } from 'path';
import { getVantConfig } from '../common';
import { STYLE_DIR, SRC_DIR } from './constant';
type CSS_LANG = 'css' | 'less' | 'scss';
function getCssLang(): CSS_LANG {
const preprocessor = get(CONFIG, 'build.css.preprocessor', 'less');
const vantConfig = getVantConfig();
const preprocessor = get(vantConfig, 'build.css.preprocessor', 'less');
if (preprocessor === 'sass') {
return 'scss';
@ -18,9 +20,10 @@ function getCssLang(): CSS_LANG {
export const CSS_LANG = getCssLang();
export function getCssBaseFile() {
const vantConfig = getVantConfig();
let path = join(STYLE_DIR, `base.${CSS_LANG}`);
const baseFile = get(CONFIG, 'build.css.base', '');
const baseFile = get(vantConfig, 'build.css.base', '');
if (baseFile) {
path = isAbsolute(baseFile) ? baseFile : join(SRC_DIR, baseFile);
}

View File

@ -1,6 +1,5 @@
import decamelize from 'decamelize';
import { join } from 'path';
import { get } from 'lodash';
import {
lstatSync,
existsSync,
@ -8,7 +7,7 @@ import {
readFileSync,
outputFileSync
} from 'fs-extra';
import { CONFIG, SRC_DIR, WEBPACK_CONFIG_FILE } from './constant';
import { SRC_DIR, WEBPACK_CONFIG_FILE, VANT_CONFIG_FILE } from './constant';
export const EXT_REGEXP = /\.\w+$/;
export const SFC_REGEXP = /\.(vue)$/;
@ -81,6 +80,12 @@ export function pascalize(str: string): string {
);
}
export function getVantConfig() {
delete require.cache[VANT_CONFIG_FILE];
return require(VANT_CONFIG_FILE);
}
export function getWebpackConfig(): object {
if (existsSync(WEBPACK_CONFIG_FILE)) {
const config = require(WEBPACK_CONFIG_FILE);
@ -110,16 +115,6 @@ export function isDev() {
return process.env.NODE_ENV === 'development';
}
export function getCssLang(): string {
const preprocessor = get(CONFIG, 'build.css.preprocessor', 'less');
if (preprocessor === 'sass') {
return 'scss';
}
return preprocessor;
}
// Smarter outputFileSync
// Skip if content unchanged
export function smartOutputFile(filePath: string, content: string) {

View File

@ -10,7 +10,7 @@ import {
import {
SRC_DIR,
DOCS_DIR,
CONFIG_FILE,
VANT_CONFIG_FILE,
SITE_DESKTOP_SHARED_FILE
} from '../common/constant';
@ -51,7 +51,7 @@ function genExportDocuments(items: DocumentItem[]) {
}
function genImportConfig() {
return `import config from '${removeExt(CONFIG_FILE)}';`;
return `import config from '${removeExt(VANT_CONFIG_FILE)}';`;
}
function genExportConfig() {

View File

@ -1,11 +1,14 @@
import { join } from 'path';
import { existsSync } from 'fs-extra';
import { decamelize, pascalize, removeExt, getComponents, smartOutputFile } from '../common';
import { SRC_DIR, SITE_MODILE_SHARED_FILE } from '../common/constant';
import {
CONFIG,
SRC_DIR,
SITE_MODILE_SHARED_FILE
} from '../common/constant';
pascalize,
removeExt,
decamelize,
getVantConfig,
getComponents,
smartOutputFile
} from '../common';
type DemoItem = {
name: string;
@ -34,16 +37,17 @@ function genExports(demos: DemoItem[]) {
}
function genConfig(demos: DemoItem[]) {
const vantConfig = getVantConfig();
const demoNames = demos.map(item => decamelize(item.name, '-'));
CONFIG.site.nav = CONFIG.site.nav.filter((group: any) => {
vantConfig.site.nav = vantConfig.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(vantConfig, null, 2)}`;
}
function genCode(components: string[]) {

View File

@ -1,11 +1,11 @@
import merge from 'webpack-merge';
import { baseConfig } from './webpack.base';
import { getWebpackConfig } from '../common';
import { LIB_DIR, CONFIG, PACKAGE_ENTRY_FILE } from '../common/constant';
const { name } = CONFIG;
import { getVantConfig, getWebpackConfig } from '../common';
import { LIB_DIR, PACKAGE_ENTRY_FILE } from '../common/constant';
export function packageConfig(isMinify: boolean) {
const { name } = getVantConfig();
return merge(
baseConfig as any,
{

View File

@ -2,15 +2,14 @@ import merge from 'webpack-merge';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { join } from 'path';
import { baseConfig } from './webpack.base';
import { getWebpackConfig } from '../common';
import { getVantConfig, getWebpackConfig } from '../common';
import { VantCliSitePlugin } from '../compiler/vant-cli-site-plugin';
import {
CONFIG,
SITE_MODILE_SHARED_FILE,
SITE_DESKTOP_SHARED_FILE
} from '../common/constant';
const siteConfig = CONFIG.site;
const siteConfig = getVantConfig().site;
const title = `${siteConfig.title} - ${siteConfig.description}`;
export const siteDevBaseConfig = merge(baseConfig as any, {

View File

@ -1,12 +1,12 @@
import merge from 'webpack-merge';
import { get } from 'lodash';
import { getWebpackConfig } from '../common';
import { getVantConfig, getWebpackConfig } from '../common';
import { siteDevBaseConfig } from './webpack.site.dev';
import { CONFIG, SITE_DIST_DIR } from '../common/constant';
import { SITE_DIST_DIR } from '../common/constant';
const outputDir = get(CONFIG, 'build.site.outputDir', SITE_DIST_DIR);
const publicPath = get(CONFIG, 'build.site.publicPath', '/');
const vantConfig = getVantConfig();
const outputDir = get(vantConfig, 'build.site.outputDir', SITE_DIST_DIR);
const publicPath = get(vantConfig, 'build.site.publicPath', '/');
export const sitePrdConfig = merge(
siteDevBaseConfig,