UNPKG

@remax/build-store

Version:

使用真正的 React 构建跨平台小程序

114 lines (113 loc) 3.98 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto = __importStar(require("crypto")); const lodash_1 = require("lodash"); const shared_1 = require("@remax/shared"); function unique(array) { return Array.from(new Set(array)); } function generatePathHash(path) { const hash = crypto.createHash('sha256'); hash.update(path); return hash.digest('hex').slice(0, 7); } function generateComponentId(path) { const [basename, dirname] = path.replace(/\.js$/, '').split('/').reverse(); const hash = generatePathHash(path); return [lodash_1.kebabCase(dirname), lodash_1.kebabCase(basename), hash].filter(Boolean).join('-'); } const Store = { collectedComponents: new Map(), registeredHostComponents: new Map(), compositionComponents: new Map(), nativeComponents: new Map(), pluginComponents: new Map(), skipHostComponents: [], appEvents: new Map(), pageEvents: new Map(), slotView: { id: 'slot-view', props: [], }, extractedTemplates: new Map(), templateId: 1, registerNativeComponent(componentPath, output) { let component = this.nativeComponents.get(componentPath); if (!component) { const id = generateComponentId(output); component = { id, }; } this.nativeComponents.set(componentPath, component); return component.id; }, registerPluginComponent(componentPath, importer) { let component = this.pluginComponents.get(componentPath); if (!component) { const id = generateComponentId(componentPath); component = { id, componentPath, importers: new Set(), props: new Set(), }; } component.importers.add(importer); this.pluginComponents.set(componentPath, component); return component.id; }, getCollectedComponents() { this.registeredHostComponents.forEach((component, key) => { if (!this.collectedComponents.has(key)) { this.collectedComponents.set(key, { id: key, props: unique(component.props).sort(), additional: component.additional, }); } }); return this.collectedComponents; }, generateTemplateId(filename) { const id = this.templateId; const hash = generatePathHash(shared_1.slash(filename)); this.templateId += 1; return [hash, id.toString()].join('-'); }, resetTemplateId() { this.templateId = 1; }, reset() { this.collectedComponents.clear(); this.registeredHostComponents.clear(); this.compositionComponents.clear(); this.nativeComponents.clear(); this.pluginComponents.clear(); this.slotView.props = []; this.extractedTemplates.clear(); this.appEvents.clear(); this.pageEvents.clear(); this.templateId = 1; }, }; exports.default = Store;