import { getCompiler, MiniProgramCompiler } from '@miniu/compiler'; import path from 'path'; import rimraf from 'rimraf'; import fs from 'fs-extra'; import { json, getProjectConfig } from '@miniu/utils'; export interface MiniBuildOptions { project?: string; output?: string; enableSubPack?: boolean; anymock?: { projectToken: string; sceneId?: string; }; appId?: string; } const DIST_DIR_NAME = 'miniu_dist'; let compiler: MiniProgramCompiler; // class BeforeBuildProxy { // async beforeBuild(api) { // const { options } = api; // const { input } = options; // const miniProjectJsonName = 'mini.project.json'; // const miniProjectJsonPath = path.join(input, miniProjectJsonName); // // 不存在mini.project.json // if (!fs.existsSync(miniProjectJsonPath)) { // console.warn( // `\n检测到项目有 .miniu,推荐在项目里添加 mini.project.json 文件 { "exclude": [".miniu/**", "miniu_dist/**"] } 提升速度,具体可见文档:https://opendocs.alipay.com/mini/framework/project` // ); // } else { // const miniProjectConfig = await json.getJSON(miniProjectJsonPath); // const { exclude = [] } = miniProjectConfig; // if ( // !(exclude.includes('.miniu/**') || exclude.includes('.miniu/*')) || // !(exclude.includes('miniu_dist/**') || exclude.includes('miniu_dist/*')) // ) { // console.warn( // `\n检测到项目有 .miniu,推荐在 mini.project.json 加上 { "exclude": [".miniu/**", "miniu_dist/**"] } 提升速度,具体可见文档:https://opendocs.alipay.com/mini/framework/project` // ); // } // } // } // } /** * @deprecated */ async function miniBuild( options: MiniBuildOptions, { silence, useLoaderCache, cacheDir, }: { silence: boolean; useLoaderCache: boolean; cacheDir?: string } = { silence: false, useLoaderCache: true, } ) { if (!compiler) { compiler = await getCompiler(); } const logger = compiler.getLoggerManager().getLogger(); const curLogLevel = logger.logLevel; if (silence) { compiler.getLoggerManager().getLogger().setLogLevel(Infinity); } // 增加对于['.miniu/**', 'miniu_dist/**']是否加入了excludes中 // compiler.addProxy(BeforeBuildProxy); const input = path.resolve(options.project || process.cwd()); const CACHE_DIR = cacheDir || path.join(input, '.miniu', 'cache'); await fs.ensureDir(CACHE_DIR); const output = path.resolve(options.output || path.join(process.cwd(), DIST_DIR_NAME)); const projectConfig = await getProjectConfig(input); const compileVersion = projectConfig.miniProjectConfig.enableAppxNg ? 'ng' : 'tiny'; const renderType = projectConfig.miniProjectConfig.enableCube ? 'cube' : 'web'; rimraf.sync(output); await compiler.clearCache(); const buildInfo = await compiler.runLocalBuild({ // for plugin appId: options.appId, input, output, compileVersion, renderType, ignoreDebugInjectCode: compileVersion === 'ng' && renderType === 'cube', parallel: true, sourceMap: false, // 构建缓存,提升之后的构建速度。cube 暂时禁用 cacheDir: useLoaderCache && renderType !== 'cube' && CACHE_DIR, }); if (silence) { compiler.getLoggerManager().getLogger().setLogLevel(curLogLevel); } return buildInfo; } export default miniBuild;