UNPKG

@hyext/builder-neorn

Version:

neo react-native builder for hyext

162 lines (161 loc) 8.34 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ramda_1 = __importDefault(require("ramda")); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); // @ts-ignore const config_1 = require("@hyext/config"); const matrix_compiler_1 = require("@hyext/matrix-compiler"); function buildBundle({ projectRoot, imageBaseURL, buildConfig, metroConfig, outputDir, onBeforeEach, infoProvider, // isUseHyCssLoader, RNVersion, logger = config_1.logger, }) { return __awaiter(this, void 0, void 0, function* () { let bundleTaskNo = 0; if (RNVersion !== '0.62.2') { let bundleInfoList = []; for (const configItem of buildConfig) { const { extType, entry, registerComponent, baseUrlMap, baseMd5Map } = configItem; const extTypeOutputDir = path_1.default.resolve(outputDir, extType); yield fs_extra_1.default.ensureDir(extTypeOutputDir); const inputAbsPath = path_1.default.resolve(projectRoot, entry); const seperator = imageBaseURL.endsWith('/') ? '' : '/'; const imageBaseURLWithExtType = `${imageBaseURL}${seperator}${path_1.default.basename(extTypeOutputDir)}`; logger.info(`compiler imageBaseURL: ${imageBaseURLWithExtType}`); const compileOutputs = yield matrix_compiler_1.deprecatedCompiler.compile({ projectRoot, metroConfigAbsPath: matrix_compiler_1.deprecatedCompiler.generateMetroCfg({ projectRoot, imageBaseURL: imageBaseURLWithExtType, filterModulesPath: infoProvider.getBaseModulesPath(extType), mode: 'production', cfgOutputDir: projectRoot, useHyCSS: true, }), bundleConfigs: [ { entryAbsPath: inputAbsPath, outputDir: extTypeOutputDir, } ] }); // FIXME 转成异步 const entryFileContent = fs_extra_1.default.readFileSync(inputAbsPath).toString(); const registerComponentList = matrix_compiler_1.utils.getRegisterComponents({ entryFileContent, configComponents: registerComponent, }); bundleInfoList = [ ...bundleInfoList, ...compileOutputs.map(({ entryAbsPath, bundleAbsPath, bundleMd5, platform }) => { return { extType, // bundleRelativePath: bundleAbsPath.replace(publicPath, ''), bundleRelativePath: bundleAbsPath.replace(outputDir, ''), registerComponents: registerComponentList.map(x => ({ name: x })), platform, SDKVersion: infoProvider.getSDKVerByExtType(extType), bundleMd5, baseUrl: baseUrlMap && baseUrlMap[platform], baseMd5: baseMd5Map && baseMd5Map[platform], }; }) ]; } return bundleInfoList; } /* * * * *************************************************** * 0.62.2 以上版本可以对 buildConfig 进行分组, 加速构建 * 一个分组内部, 必须保证metroconfig相等, 也就是保证以下值相等 * 1. imageBaseURI * 2. filterModules * *************************************************** * * */ const groups = ramda_1.default.groupBy(infoProvider.groupBuildConfigFn, buildConfig); let bundleInfoList = []; for (const group of Object.values(groups)) { bundleTaskNo += 1; logger.info(`NEO: 打包任务组[${bundleTaskNo}]: ${JSON.stringify(group)}`); const firstExtType = group[0].extType; const filterModules = infoProvider.getBaseModules(firstExtType); const SDKVersion = infoProvider.getSDKVerByExtType(firstExtType); if (typeof onBeforeEach === 'function') { // FIXME yield onBeforeEach({ // SDKName: getSDKName(SDKRole), SDKNames: Object.keys(infoProvider.shouldInstallSDKVer), extType: group[0].extType, }); } const bundleConfigsPromises = group.map(({ extType, entry }) => __awaiter(this, void 0, void 0, function* () { const extTypeOutputDir = path_1.default.resolve(outputDir, extType); yield fs_extra_1.default.ensureDir(extTypeOutputDir); const inputAbsPath = path_1.default.resolve(projectRoot, entry); return { entryAbsPath: inputAbsPath, outputDir: extTypeOutputDir, // 将assets 放到 最外层目录, 所有 extType 公用一个 assetsDir: outputDir, }; })); const bundleConfigs = yield Promise.all(bundleConfigsPromises); logger.info(`compiler imageBaseURL: ${imageBaseURL}`); const compileOutputs = yield matrix_compiler_1.compiler.compile({ projectRoot, imageBaseURL, SDKAlias: infoProvider.SDKAlias, filterModules, metroConfig, bundleConfigs, }); const getTargetConfigItem = (entryAbsPath) => { const entry = path_1.default.basename(entryAbsPath); const target = group.find(item => item.entry === entry); return target; }; bundleInfoList = [ ...bundleInfoList, ...compileOutputs.map(({ entryAbsPath, bundleAbsPath, bundleMd5, platform }) => { // const targetConfigItem = getTargetConfigItem(entryAbsPath) const { extType, registerComponent, baseUrlMap, baseMd5Map } = getTargetConfigItem(entryAbsPath); // FIXME 转成异步 const entryFileContent = fs_extra_1.default.readFileSync(entryAbsPath).toString(); const registerComponentList = matrix_compiler_1.utils.getRegisterComponents({ entryFileContent, configComponents: registerComponent, }); return { extType, bundleRelativePath: bundleAbsPath.replace(outputDir, ''), registerComponents: registerComponentList.map(x => ({ name: x })), platform, SDKVersion, bundleMd5, baseUrl: baseUrlMap && baseUrlMap[platform], baseMd5: baseMd5Map && baseMd5Map[platform], }; }) ]; } logger.success(`NEO: bundleInfo: ${JSON.stringify(bundleInfoList, null, ' ')}`); return bundleInfoList; }); } exports.default = buildBundle; module.exports = buildBundle;