"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uniEntryPlugin = exports.parseComponentStyleIsolation = exports.isUniComponentUrl = exports.isUniPageUrl = exports.parseVirtualComponentPath = exports.parseVirtualPagePath = exports.virtualComponentPath = exports.virtualPagePath = void 0; const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared"); const uniPagePrefix = 'uniPage://'; const uniComponentPrefix = 'uniComponent://'; function virtualPagePath(filepath) { return uniPagePrefix + (0, uni_cli_shared_1.encodeBase64Url)(filepath); } exports.virtualPagePath = virtualPagePath; function virtualComponentPath(filepath) { return uniComponentPrefix + (0, uni_cli_shared_1.encodeBase64Url)(filepath); } exports.virtualComponentPath = virtualComponentPath; function parseVirtualPagePath(uniPageUrl) { return (0, uni_cli_shared_1.decodeBase64Url)(uniPageUrl.replace(uniPagePrefix, '')); } exports.parseVirtualPagePath = parseVirtualPagePath; function parseVirtualComponentPath(uniComponentUrl) { return (0, uni_cli_shared_1.decodeBase64Url)(uniComponentUrl.replace(uniComponentPrefix, '')); } exports.parseVirtualComponentPath = parseVirtualComponentPath; function isUniPageUrl(id) { return id.startsWith(uniPagePrefix); } exports.isUniPageUrl = isUniPageUrl; function isUniComponentUrl(id) { return id.startsWith(uniComponentPrefix); } exports.isUniComponentUrl = isUniComponentUrl; const styleIsolationRE = [ /defineOptions\s*[\s\S]*?styleIsolation\s*:\s*['"](isolated|apply-shared|shared)['"]/, /export\s+default\s+[\s\S]*?styleIsolation\s*:\s*['|"](isolated|apply-shared|shared)['|"]/, ]; function parseComponentStyleIsolation(content) { for (const regex of styleIsolationRE) { const matches = content.match(regex); if (matches) { return matches[1]; } } } exports.parseComponentStyleIsolation = parseComponentStyleIsolation; function uniEntryPlugin({ global, template, style, }) { const inputDir = process.env.UNI_INPUT_DIR; const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir); const platformOptions = manifestJson[process.env.UNI_PLATFORM] || {}; const easycomEncryptComponentPaths = new Set(); return { name: 'uni:virtual', enforce: 'pre', resolveId(id) { if (isUniPageUrl(id) || isUniComponentUrl(id)) { return id; } }, buildStart() { easycomEncryptComponentPaths.clear(); }, load(id) { if (isUniPageUrl(id)) { const filepath = (0, uni_cli_shared_1.normalizePath)(path_1.default.resolve(inputDir, parseVirtualPagePath(id))); this.addWatchFile(filepath); return { code: `import MiniProgramPage from '${filepath}' ${global}.createPage(MiniProgramPage)`, }; } else if (isUniComponentUrl(id)) { const filepath = (0, uni_cli_shared_1.normalizePath)(path_1.default.resolve(inputDir, parseVirtualComponentPath(id))); this.addWatchFile(filepath); const relativePath = (0, uni_cli_shared_1.normalizePath)(path_1.default.relative(inputDir, filepath)); // 判断当前插件是否是easycom加密插件 if (relativePath.startsWith('uni_modules')) { const pluginId = relativePath.split('/')[1]; const encryptType = (0, uni_cli_shared_1.getUniModulesEncryptType)(pluginId); if (encryptType === 'easycom') { const componentName = (0, uni_cli_shared_1.capitalize)((0, uni_cli_shared_1.camelize)((0, uni_cli_shared_1.removeExt)(path_1.default.basename(relativePath)))); easycomEncryptComponentPaths.add((0, uni_cli_shared_1.removeExt)(relativePath)); return { code: `import { defineComponent${componentName} } from '@/uni_modules/${pluginId}?uni_helpers' defineComponent${componentName}()`, }; } } const json = { component: true, styleIsolation: undefined, }; if (process.env.UNI_PLATFORM === 'mp-alipay') { json.styleIsolation = parseComponentStyleIsolation(fs_1.default.readFileSync(filepath, 'utf-8')) || platformOptions.styleIsolation || 'apply-shared'; } // 微信小程序json文件中的styleIsolation优先级比options中的高,为了兼容旧版本,不能设置默认值,并且只有在manifest.json中配置styleIsolation才会静态分析组件的styleIsolation if (process.env.UNI_PLATFORM === 'mp-weixin') { if (platformOptions.styleIsolation) { json.styleIsolation = parseComponentStyleIsolation(fs_1.default.readFileSync(filepath, 'utf-8')) || platformOptions.styleIsolation; } } (0, uni_cli_shared_1.addMiniProgramComponentJson)((0, uni_cli_shared_1.removeExt)((0, uni_cli_shared_1.normalizeMiniProgramFilename)(filepath, inputDir)), json); if (process.env.UNI_COMPILE_TARGET === 'uni_modules') { // 云编译时,组件的代码会直接内联到入口文件中,以方法对外导出,不能立刻执行createComponent return { code: `import Component from '${filepath} export default Component`, }; } return { code: `import Component from '${filepath}' ${global}.createComponent(Component)`, }; } }, generateBundle() { const cacheDir = process.env.UNI_MODULES_ENCRYPT_CACHE_DIR; if (cacheDir) { for (const componentPath of easycomEncryptComponentPaths) { const componentCachePath = path_1.default.resolve(cacheDir, componentPath); ['.json', template.extname, style.extname].forEach((extname) => { const filename = componentCachePath + extname; if (fs_1.default.existsSync(filename)) { this.emitFile({ fileName: componentPath + extname, type: 'asset', source: fs_1.default.readFileSync(filename, 'utf-8'), }); } }); } } }, }; } exports.uniEntryPlugin = uniEntryPlugin;