mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
chore(cli): split compilers
This commit is contained in:
parent
e2c9628316
commit
51d2512311
@ -5,6 +5,7 @@ import { clean } from '../commands/clean';
|
|||||||
import { buildESModuleOutputs } from './build';
|
import { buildESModuleOutputs } from './build';
|
||||||
import { siteDevConfig } from '../config/webpack.site.dev';
|
import { siteDevConfig } from '../config/webpack.site.dev';
|
||||||
import { genPackageEntry } from '../compiler/gen-package-entry';
|
import { genPackageEntry } from '../compiler/gen-package-entry';
|
||||||
|
import { genPacakgeStyle } from '../compiler/gen-package-style';
|
||||||
import { genMobileEntry } from '../compiler/gen-mobile-entry';
|
import { genMobileEntry } from '../compiler/gen-mobile-entry';
|
||||||
import { genDesktopEntry } from '../compiler/gen-desktop-entry';
|
import { genDesktopEntry } from '../compiler/gen-desktop-entry';
|
||||||
import { genDepsMap } from '../compiler/gen-style-deps-map';
|
import { genDepsMap } from '../compiler/gen-style-deps-map';
|
||||||
@ -39,6 +40,7 @@ export async function dev() {
|
|||||||
await buildESModuleOutputs();
|
await buildESModuleOutputs();
|
||||||
genDepsMap();
|
genDepsMap();
|
||||||
genPackageEntry();
|
genPackageEntry();
|
||||||
|
genPacakgeStyle();
|
||||||
genMobileEntry();
|
genMobileEntry();
|
||||||
genDesktopEntry();
|
genDesktopEntry();
|
||||||
runWebpack();
|
runWebpack();
|
||||||
|
@ -27,3 +27,5 @@ export const JEST_STYLE_MOCK_FILE = join(CONFIG_DIR, 'jest.style-mock.js');
|
|||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
export const CONFIG = require(CONFIG_FILE);
|
export const CONFIG = require(CONFIG_FILE);
|
||||||
|
// eslint-disable-next-line
|
||||||
|
export const PACKAGE_JSON = require(PACKAGE_JSON_FILE);
|
||||||
|
12
packages/vant-cli/src/compiler/compile-css.ts
Normal file
12
packages/vant-cli/src/compiler/compile-css.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import postcss from 'postcss';
|
||||||
|
import postcssrc from 'postcss-load-config';
|
||||||
|
import { POSTCSS_CONFIG_FILE } from '../common/constant';
|
||||||
|
|
||||||
|
export async function compileCss(source: string | Buffer) {
|
||||||
|
const config = await postcssrc({}, POSTCSS_CONFIG_FILE);
|
||||||
|
const output = await postcss(config.plugins as any).process(source, {
|
||||||
|
from: undefined
|
||||||
|
});
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
11
packages/vant-cli/src/compiler/compile-less.ts
Normal file
11
packages/vant-cli/src/compiler/compile-less.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { readFileSync } from 'fs-extra';
|
||||||
|
import { render as renderLess } from 'less';
|
||||||
|
|
||||||
|
export async function compileLess(filePath: string) {
|
||||||
|
const source = readFileSync(filePath, 'utf-8');
|
||||||
|
const { css } = await renderLess(source, {
|
||||||
|
filename: filePath
|
||||||
|
});
|
||||||
|
|
||||||
|
return css;
|
||||||
|
}
|
6
packages/vant-cli/src/compiler/compile-sass.ts
Normal file
6
packages/vant-cli/src/compiler/compile-sass.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { renderSync as renderSass } from 'sass';
|
||||||
|
|
||||||
|
export async function compileSass(filePath: string) {
|
||||||
|
const { css } = renderSass({ file: filePath });
|
||||||
|
return css;
|
||||||
|
}
|
@ -84,7 +84,7 @@ export async function compileSfc(filePath: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
writeFileSync(jsFilePath, script);
|
writeFileSync(jsFilePath, script);
|
||||||
await compileJs(jsFilePath);
|
compileJs(jsFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// compile style part
|
// compile style part
|
||||||
|
@ -1,46 +1,29 @@
|
|||||||
import postcss from 'postcss';
|
|
||||||
import postcssrc from 'postcss-load-config';
|
|
||||||
import { parse } from 'path';
|
import { parse } from 'path';
|
||||||
import { render as renderLess } from 'less';
|
|
||||||
import { renderSync as renderSass } from 'sass';
|
|
||||||
import { readFileSync, writeFileSync } from 'fs';
|
import { readFileSync, writeFileSync } from 'fs';
|
||||||
import { replaceExt } from '../common';
|
import { replaceExt } from '../common';
|
||||||
import { POSTCSS_CONFIG_FILE } from '../common/constant';
|
import { compileCss } from './compile-css';
|
||||||
|
import { compileLess } from './compile-less';
|
||||||
|
import { compileSass } from './compile-sass';
|
||||||
|
|
||||||
async function compilePostcss(filePath: string, source: string | Buffer) {
|
async function compileFile(filePath: string) {
|
||||||
const config = await postcssrc({}, POSTCSS_CONFIG_FILE);
|
|
||||||
const output = await postcss(config.plugins as any).process(source, {
|
|
||||||
from: undefined
|
|
||||||
});
|
|
||||||
|
|
||||||
writeFileSync(filePath, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function compileLess(filePath: string) {
|
|
||||||
const source = readFileSync(filePath, 'utf-8');
|
|
||||||
const { css } = await renderLess(source, {
|
|
||||||
filename: filePath
|
|
||||||
});
|
|
||||||
|
|
||||||
return css;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function compileSass(filePath: string) {
|
|
||||||
const { css } = renderSass({ file: filePath });
|
|
||||||
return css;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function compileStyle(filePath: string) {
|
|
||||||
const parsedPath = parse(filePath);
|
const parsedPath = parse(filePath);
|
||||||
|
|
||||||
if (parsedPath.ext === '.less') {
|
if (parsedPath.ext === '.less') {
|
||||||
const source = await compileLess(filePath);
|
const source = await compileLess(filePath);
|
||||||
await compilePostcss(replaceExt(filePath, '.css'), source);
|
return compileCss(source);
|
||||||
} else if (parsedPath.ext === '.scss') {
|
|
||||||
const source = await compileSass(filePath);
|
|
||||||
await compilePostcss(replaceExt(filePath, '.css'), source);
|
|
||||||
} else {
|
|
||||||
const source = readFileSync(filePath, 'utf-8');
|
|
||||||
await compilePostcss(filePath, source);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (parsedPath.ext === '.scss') {
|
||||||
|
const source = await compileSass(filePath);
|
||||||
|
return compileCss(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
const source = readFileSync(filePath, 'utf-8');
|
||||||
|
return compileCss(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function compileStyle(filePath: string) {
|
||||||
|
const content = await compileFile(filePath);
|
||||||
|
|
||||||
|
writeFileSync(replaceExt(filePath, '.css'), content);
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,13 @@
|
|||||||
import { get } from 'lodash';
|
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { writeFileSync } from 'fs-extra';
|
import { writeFileSync } from 'fs-extra';
|
||||||
import { pascalize, getComponents, replaceExt } from '../common';
|
import { pascalize, getComponents } from '../common';
|
||||||
import {
|
import {
|
||||||
CONFIG,
|
|
||||||
SRC_DIR,
|
SRC_DIR,
|
||||||
PACKAGE_JSON_FILE,
|
PACKAGE_JSON,
|
||||||
PACKAGE_ENTRY_FILE,
|
PACKAGE_ENTRY_FILE
|
||||||
PACKAGE_STYLE_FILE,
|
|
||||||
STYPE_DEPS_JSON_FILE
|
|
||||||
} from '../common/constant';
|
} from '../common/constant';
|
||||||
|
|
||||||
// eslint-disable-next-line
|
const version = process.env.PACKAGE_VERSION || PACKAGE_JSON.version;
|
||||||
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 {
|
function genImports(components: string[]): string {
|
||||||
return components
|
return components
|
||||||
@ -27,26 +19,7 @@ function genExports(names: string[]): string {
|
|||||||
return names.map(name => `${name}`).join(',\n ');
|
return names.map(name => `${name}`).join(',\n ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStyleExt(): string {
|
export function genPackageEntry() {
|
||||||
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((name: string) => `@import "${join(SRC_DIR, `${name}/index${ext}`)}";`)
|
|
||||||
.join('\n');
|
|
||||||
|
|
||||||
writeFileSync(replaceExt(PACKAGE_STYLE_FILE, ext), content);
|
|
||||||
}
|
|
||||||
|
|
||||||
function genScriptEntry() {
|
|
||||||
const components = getComponents();
|
const components = getComponents();
|
||||||
const names = components.map(item => pascalize(item));
|
const names = components.map(item => pascalize(item));
|
||||||
|
|
||||||
@ -85,8 +58,3 @@ export default {
|
|||||||
|
|
||||||
writeFileSync(PACKAGE_ENTRY_FILE, content);
|
writeFileSync(PACKAGE_ENTRY_FILE, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function genPackageEntry() {
|
|
||||||
genStyleEntry();
|
|
||||||
genScriptEntry();
|
|
||||||
}
|
|
||||||
|
32
packages/vant-cli/src/compiler/gen-package-style.ts
Normal file
32
packages/vant-cli/src/compiler/gen-package-style.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { get } from 'lodash';
|
||||||
|
import { join } from 'path';
|
||||||
|
import { writeFileSync } from 'fs-extra';
|
||||||
|
import { replaceExt } from '../common';
|
||||||
|
import {
|
||||||
|
CONFIG,
|
||||||
|
SRC_DIR,
|
||||||
|
PACKAGE_STYLE_FILE,
|
||||||
|
STYPE_DEPS_JSON_FILE
|
||||||
|
} from '../common/constant';
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
|
const styleDepsJson = require(STYPE_DEPS_JSON_FILE);
|
||||||
|
|
||||||
|
function getStyleExt(): string {
|
||||||
|
const preprocessor = get(CONFIG, 'build.css.preprocessor', 'less');
|
||||||
|
|
||||||
|
if (preprocessor === 'sass') {
|
||||||
|
return '.scss';
|
||||||
|
}
|
||||||
|
|
||||||
|
return `.${preprocessor}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function genPacakgeStyle() {
|
||||||
|
const ext = getStyleExt();
|
||||||
|
const content = styleDepsJson.sequence
|
||||||
|
.map((name: string) => `@import "${join(SRC_DIR, `${name}/index${ext}`)}";`)
|
||||||
|
.join('\n');
|
||||||
|
|
||||||
|
writeFileSync(replaceExt(PACKAGE_STYLE_FILE, ext), content);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user