fix(cli): compile commonjs module

This commit is contained in:
陈嘉涵
2019-12-12 16:20:15 +08:00
parent 19ceb9dddb
commit 71415e467d
5 changed files with 55 additions and 7 deletions

View File

@@ -45,7 +45,7 @@ async function compileDir(dir: string) {
}
if (isScript(filePath)) {
return compileJs(filePath);
return compileJs(filePath, { reloadConfig: true });
}
if (isStyle(filePath)) {
@@ -129,7 +129,7 @@ async function buildPackageEntry() {
setModuleEnv('commonjs');
await copy(esEntryFile, libEntryFile);
await compileJs(libEntryFile);
await compileJs(libEntryFile, { reloadConfig: true });
await compileStyle(styleEntryFile);
stepper.success('Build Package Entry');

View File

@@ -1,10 +1,49 @@
// @ts-ignore
import findBabelConfig from 'find-babel-config';
import { join } from 'path';
import { transformFileAsync } from '@babel/core';
import { removeSync, outputFileSync } from 'fs-extra';
import { removeSync, outputFileSync, existsSync } from 'fs-extra';
import { replaceExt } from '../common';
import { CWD, DIST_DIR } from '../common/constant';
export function compileJs(filePath: string): Promise<undefined> {
type Options = {
// whether to fouce reload babel config
reloadConfig?: boolean;
};
const TEMP_BABEL_CONFIG = join(DIST_DIR, 'babel.config.js');
function genTempBabelConfig() {
const { config } = findBabelConfig.sync(CWD);
const content = `module.exports = function (api) {
api.cache.never();
return ${JSON.stringify(config)}
};`;
outputFileSync(TEMP_BABEL_CONFIG, content);
}
function getBabelOptions(options: Options) {
if (options.reloadConfig) {
if (!existsSync(TEMP_BABEL_CONFIG)) {
genTempBabelConfig();
}
return {
configFile: TEMP_BABEL_CONFIG
};
}
return {};
}
export function compileJs(
filePath: string,
options: Options = {}
): Promise<undefined> {
return new Promise((resolve, reject) => {
transformFileAsync(filePath)
transformFileAsync(filePath, getBabelOptions(options))
.then(result => {
if (result) {
const jsFilePath = replaceExt(filePath, '.js');

View File

@@ -39,7 +39,7 @@ const version = '${version}';
function install(Vue) {
const components = [
${components.filter(item => !skipInstall.includes(item)).join(',\n ')}
${components.filter(item => !skipInstall.includes(item)).join(',\n ')}
];
components.forEach(item => {