zephyr-rspack-plugin
Version:
Repack plugin for Zephyr
129 lines (128 loc) • 5.8 kB
JavaScript
;
var __webpack_require__ = {};
(()=>{
__webpack_require__.d = (exports1, getters, values)=>{
var define = (defs, kind)=>{
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
[kind]: defs[key]
});
};
define(getters, "get");
define(values, "value");
};
})();
(()=>{
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
__webpack_require__.r = (exports1)=>{
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
value: 'Module'
});
Object.defineProperty(exports1, '__esModule', {
value: true
});
};
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
ZeRspackPlugin: ()=>ZeRspackPlugin
});
const core_namespaceObject = require("@rspack/core");
const external_zephyr_agent_namespaceObject = require("zephyr-agent");
const external_zephyr_xpack_internal_namespaceObject = require("zephyr-xpack-internal");
const pluginName = 'ZeRspackPlugin';
class ZeRspackPlugin {
_options;
constructor(options){
this._options = Object.assign({
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, (0, external_zephyr_xpack_internal_namespaceObject.detectBaseHref)(compiler));
else (0, external_zephyr_xpack_internal_namespaceObject.detectAndStoreBaseHref)(this._options.zephyr_engine, compiler);
(0, external_zephyr_xpack_internal_namespaceObject.logBuildSteps)(this._options, compiler);
(0, external_zephyr_xpack_internal_namespaceObject.setupManifestEmission)(this._options, compiler);
(0, external_zephyr_xpack_internal_namespaceObject.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 = core_namespaceObject.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: (0, external_zephyr_agent_namespaceObject.buildEnvImportMap)(appUid, remotes)
}),
voidTag: false
});
} catch (e) {
external_zephyr_agent_namespaceObject.ze_log.misc('Failed to inject import map at build time:', e);
}
return data;
});
} catch {
external_zephyr_agent_namespaceObject.ze_log.misc('HtmlRspackPlugin not available, skipping import map injection');
}
});
}
}
exports.ZeRspackPlugin = __webpack_exports__.ZeRspackPlugin;
for(var __rspack_i in __webpack_exports__)if (-1 === [
"ZeRspackPlugin"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
value: true
});
//# sourceMappingURL=ze-rspack-plugin.js.map