feat(cli): build lib packed entry files

This commit is contained in:
陈嘉涵
2019-12-12 09:46:39 +08:00
parent 29e9e1c476
commit 5318f3bd3f
7 changed files with 43 additions and 42 deletions

View File

@@ -20,6 +20,8 @@ import {
setNodeEnv,
setModuleEnv
} from '../common';
import { genPacakgeStyle } from '../compiler/gen-package-style';
import { CSS_LANG } from '../common/css';
const stepper = getStepper(10);
@@ -112,15 +114,23 @@ async function buildPackageEntry() {
try {
const esEntryFile = join(ES_DIR, 'index.js');
const libEntryFile = join(LIB_DIR, 'index.js');
const styleEntryFile = join(LIB_DIR, `index.${CSS_LANG}`);
genPackageEntry({
outputPath: esEntryFile,
pathResolver: (path: string) => `./${relative(SRC_DIR, path)}`
});
genPacakgeStyle({
outputPath: styleEntryFile,
pathResolver: (path: string) => path.replace(SRC_DIR, '.')
});
setModuleEnv('commonjs');
await copy(esEntryFile, libEntryFile);
await compileJs(libEntryFile);
await compileStyle(styleEntryFile);
stepper.success('Build Package Entry');
} catch (err) {

View File

@@ -1,26 +1,39 @@
import { join } from 'path';
import { replaceExt, smartOutputFile } from '../common';
import { smartOutputFile } from '../common';
import { CSS_LANG, getCssBaseFile } from '../common/css';
import {
SRC_DIR,
PACKAGE_STYLE_FILE,
STYPE_DEPS_JSON_FILE
} from '../common/constant';
import { SRC_DIR, STYPE_DEPS_JSON_FILE } from '../common/constant';
export function genPacakgeStyle() {
type Options = {
outputPath: string;
pathResolver?: Function;
};
export function genPacakgeStyle(options: Options) {
const styleDepsJson = require(STYPE_DEPS_JSON_FILE);
const ext = '.' + CSS_LANG;
let content = '';
const baseFile = getCssBaseFile();
let baseFile = getCssBaseFile();
if (baseFile) {
if (options.pathResolver) {
baseFile = options.pathResolver(baseFile);
}
content += `@import "${baseFile}";\n`;
}
content += styleDepsJson.sequence
.map((name: string) => `@import "${join(SRC_DIR, `${name}/index${ext}`)}";`)
.map((name: string) => {
let path = join(SRC_DIR, `${name}/index${ext}`);
if (options.pathResolver) {
path = options.pathResolver(path);
}
return `@import "${path}";`;
})
.join('\n');
smartOutputFile(replaceExt(PACKAGE_STYLE_FILE, ext), content);
smartOutputFile(options.outputPath, content);
}

View File

@@ -1,10 +1,12 @@
import { Compiler } from 'webpack';
import { replaceExt } from '../common';
import { CSS_LANG } from '../common/css';
import { genPackageEntry } from './gen-package-entry';
import { genPacakgeStyle } from './gen-package-style';
import { genSiteMobileShared } from './gen-site-mobile-shared';
import { genSiteDesktopShared } from './gen-site-desktop-shared';
import { genStyleDepsMap } from './gen-style-deps-map';
import { PACKAGE_ENTRY_FILE } from '../common/constant';
import { PACKAGE_ENTRY_FILE, PACKAGE_STYLE_FILE } from '../common/constant';
const PLUGIN_NAME = 'VantCliSitePlugin';
@@ -20,7 +22,9 @@ export class VantCliSitePlugin {
genPackageEntry({
outputPath: PACKAGE_ENTRY_FILE
});
genPacakgeStyle();
genPacakgeStyle({
outputPath: replaceExt(PACKAGE_STYLE_FILE, `.${CSS_LANG}`)
});
genSiteMobileShared();
genSiteDesktopShared();
resolve();

View File

@@ -1,14 +1,8 @@
module.exports = function(api: any) {
module.exports = function() {
const { BABEL_MODULE, NODE_ENV } = process.env;
const isTest = NODE_ENV === 'test';
const useESModules = BABEL_MODULE !== 'commonjs' && !isTest;
console.log('cache ??', useESModules, api);
if (api) {
console.log('never ed');
api.cache.never();
}
return {
presets: [
[