UNPKG

zephyr-rspack-plugin

Version:

Repack plugin for Zephyr

93 lines (92 loc) • 4.3 kB
import __rslib_shim_module__ from "node:module"; const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)()); import { HtmlRspackPlugin } from "@rspack/core"; import { buildEnvImportMap, ze_log } from "zephyr-agent"; import { detectAndStoreBaseHref, detectBaseHref, logBuildSteps, setupManifestEmission, setupZeDeploy } from "zephyr-xpack-internal"; const pluginName = 'ZeRspackPlugin'; class ZeRspackPlugin { _options; constructor(options){ this._options = Object.assign({ pluginName: pluginName }, options); } apply(compiler) { this._options.zephyr_engine.buildProperties.output = compiler.outputPath; if (this._options.coordinator && this._options.participant) this._options.coordinator.registerParticipantBaseHref(this._options.participant, detectBaseHref(compiler)); else detectAndStoreBaseHref(this._options.zephyr_engine, compiler); logBuildSteps(this._options, compiler); setupManifestEmission(this._options, compiler); setupZeDeploy(this._options, compiler); if (this._options.zephyr_engine.env?.target === 'tap-app') return; this.#injectImportMapAtBuildTime(compiler); const rules = compiler.options?.module?.rules || []; rules.unshift({ test: /\.[jt]sx?$/, exclude: /node_modules/, use: [ { loader: require.resolve('./env-virtual-loader.js'), options: { specifier: `env:vars:${this._options.zephyr_engine.application_uid}` } } ] }); compiler.options.module = compiler.options.module || {}; compiler.options.module.rules = rules; const existingExternals = compiler.options?.externals; const PER_APP_SPECIFIER = `env:vars:${this._options.zephyr_engine.application_uid}`; const virtualExternal = { [PER_APP_SPECIFIER]: `module ${PER_APP_SPECIFIER}` }; if (existingExternals) { if (Array.isArray(existingExternals)) compiler.options.externals = [ ...existingExternals, virtualExternal ]; else if ('object' == typeof existingExternals) compiler.options.externals = { ...existingExternals, ...virtualExternal }; } else compiler.options.externals = virtualExternal; } #convertFederatedDepsToRemotes() { return this._options.zephyr_engine.federated_dependencies?.map((dep)=>({ name: dep.name, application_uid: dep.application_uid, remote_entry_url: dep.default_url })) || []; } #injectImportMapAtBuildTime(compiler) { compiler.hooks.compilation.tap(pluginName, (compilation)=>{ try { const hooks = HtmlRspackPlugin.getCompilationHooks(compilation); hooks.afterTemplateExecution.tapPromise(pluginName, async (data)=>{ try { const appUid = this._options.zephyr_engine.application_uid; const remotes = this.#convertFederatedDepsToRemotes(); const hasImportMap = data.headTags.some((tag)=>"script" === tag.tagName && tag.attributes?.type === 'importmap'); if (!hasImportMap) data.headTags.unshift({ tagName: "script", attributes: { type: 'importmap' }, innerHTML: JSON.stringify({ imports: buildEnvImportMap(appUid, remotes) }), voidTag: false }); } catch (e) { ze_log.misc('Failed to inject import map at build time:', e); } return data; }); } catch { ze_log.misc('HtmlRspackPlugin not available, skipping import map injection'); } }); } } export { ZeRspackPlugin }; //# sourceMappingURL=ze-rspack-plugin.mjs.map