mirror of
https://github.com/youzan/vant.git
synced 2025-10-19 01:54:48 +00:00
perf(cli): simplify babel cache
This commit is contained in:
@@ -36,7 +36,7 @@ async function compileFile(filePath: string) {
|
||||
}
|
||||
|
||||
if (isScript(filePath)) {
|
||||
return compileJs(filePath, { reloadConfig: true });
|
||||
return compileJs(filePath);
|
||||
}
|
||||
|
||||
if (isStyle(filePath)) {
|
||||
@@ -153,7 +153,7 @@ async function buildPackageEntry() {
|
||||
});
|
||||
|
||||
setModuleEnv('esmodule');
|
||||
await compileJs(esEntryFile, { reloadConfig: true });
|
||||
await compileJs(esEntryFile);
|
||||
|
||||
genPacakgeStyle({
|
||||
outputPath: styleEntryFile,
|
||||
@@ -162,7 +162,7 @@ async function buildPackageEntry() {
|
||||
|
||||
setModuleEnv('commonjs');
|
||||
await copy(esEntryFile, libEntryFile);
|
||||
await compileJs(libEntryFile, { reloadConfig: true });
|
||||
await compileJs(libEntryFile);
|
||||
await compileStyle(styleEntryFile);
|
||||
|
||||
stepper.success('Build Package Entry');
|
||||
|
@@ -1,49 +1,11 @@
|
||||
// @ts-ignore
|
||||
import findBabelConfig from 'find-babel-config';
|
||||
import { join } from 'path';
|
||||
import { transformFileAsync } from '@babel/core';
|
||||
import { removeSync, outputFileSync, existsSync } from 'fs-extra';
|
||||
import { removeSync, outputFileSync } from 'fs-extra';
|
||||
import { replaceExt } from '../common';
|
||||
import { ROOT, DIST_DIR } from '../common/constant';
|
||||
|
||||
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(ROOT);
|
||||
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> {
|
||||
export function compileJs(filePath: string): Promise<undefined> {
|
||||
return new Promise((resolve, reject) => {
|
||||
transformFileAsync(filePath, getBabelOptions(options))
|
||||
transformFileAsync(filePath)
|
||||
.then(result => {
|
||||
if (result) {
|
||||
const jsFilePath = replaceExt(filePath, '.js');
|
||||
|
@@ -1,4 +1,10 @@
|
||||
module.exports = function() {
|
||||
import { ConfigAPI } from '@babel/core';
|
||||
|
||||
module.exports = function(api?: ConfigAPI) {
|
||||
if (api) {
|
||||
api.cache.never();
|
||||
}
|
||||
|
||||
const { BABEL_MODULE, NODE_ENV } = process.env;
|
||||
const isTest = NODE_ENV === 'test';
|
||||
const useESModules = BABEL_MODULE !== 'commonjs' && !isTest;
|
||||
|
Reference in New Issue
Block a user