mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-27 01:22:49 +00:00
221 lines
6.9 KiB
JavaScript
221 lines
6.9 KiB
JavaScript
import path from "path";
|
|
import { normalizeOptions } from "@module-federation/sdk";
|
|
import { isTSProject, retrieveTypesAssetsInfo } from "@module-federation/dts-plugin/core";
|
|
import { HOT_UPDATE_SUFFIX, PLUGIN_IDENTIFIER } from "./constants.mjs";
|
|
import logger from "./logger.mjs";
|
|
|
|
;// CONCATENATED MODULE: external "path"
|
|
|
|
;// CONCATENATED MODULE: external "@module-federation/sdk"
|
|
|
|
;// CONCATENATED MODULE: external "@module-federation/dts-plugin/core"
|
|
|
|
;// CONCATENATED MODULE: external "./constants.mjs"
|
|
|
|
;// CONCATENATED MODULE: external "./logger.mjs"
|
|
|
|
;// CONCATENATED MODULE: ./src/utils.ts
|
|
|
|
|
|
|
|
|
|
|
|
function isHotFile(file) {
|
|
return file.includes(HOT_UPDATE_SUFFIX);
|
|
}
|
|
const collectAssets = (assets, jsTargetSet, cssTargetSet)=>{
|
|
assets.forEach((file)=>{
|
|
if (file.endsWith('.css')) {
|
|
cssTargetSet.add(file);
|
|
} else {
|
|
if (isDev()) {
|
|
if (!isHotFile(file)) {
|
|
jsTargetSet.add(file);
|
|
}
|
|
} else {
|
|
jsTargetSet.add(file);
|
|
}
|
|
}
|
|
});
|
|
};
|
|
function getSharedModuleName(name) {
|
|
const [_type, _shared, _module, _shareScope, sharedInfo] = name.split(' ');
|
|
return sharedInfo.split('@').slice(0, -1).join('@');
|
|
}
|
|
function getAssetsByChunkIDs(compilation, chunkIDMap) {
|
|
const arrayChunks = Array.from(compilation.chunks);
|
|
const assetMap = {};
|
|
Object.keys(chunkIDMap).forEach((key)=>{
|
|
const chunkIDs = Array.from(chunkIDMap[key]);
|
|
if (!assetMap[key]) {
|
|
assetMap[key] = {
|
|
css: new Set(),
|
|
js: new Set()
|
|
};
|
|
}
|
|
chunkIDs.forEach((chunkID)=>{
|
|
const chunk = arrayChunks.find((item)=>item.id === chunkID);
|
|
if (chunk) {
|
|
collectAssets([
|
|
...chunk.files
|
|
], assetMap[key].js, assetMap[key].css);
|
|
}
|
|
});
|
|
});
|
|
const assets = {};
|
|
Object.keys(assetMap).map((key)=>{
|
|
assets[key] = {
|
|
js: Array.from(assetMap[key].js),
|
|
css: Array.from(assetMap[key].css)
|
|
};
|
|
});
|
|
return assets;
|
|
}
|
|
function findChunk(id, chunks) {
|
|
for (const chunk of chunks){
|
|
if (id === chunk.id) {
|
|
return chunk;
|
|
}
|
|
}
|
|
}
|
|
function getSharedModules(stats, sharedModules) {
|
|
var _stats_modules;
|
|
// 获取入口文件就是实际内容的 module
|
|
const entryContentModuleNames = [];
|
|
let effectiveSharedModules = ((_stats_modules = stats.modules) === null || _stats_modules === void 0 ? void 0 : _stats_modules.reduce((sum, module)=>{
|
|
for (const sharedModule of sharedModules){
|
|
if (sharedModule.name === module.issuerName) {
|
|
entryContentModuleNames.push(sharedModule.name);
|
|
sum.push([
|
|
getSharedModuleName(module.issuerName),
|
|
module
|
|
]);
|
|
return sum;
|
|
}
|
|
}
|
|
return sum;
|
|
}, [])) || [];
|
|
// 获取入口文件仅作为 Re Export 的 module
|
|
const entryReExportModules = sharedModules.filter((sharedModule)=>!entryContentModuleNames.includes(sharedModule.name));
|
|
if (entryReExportModules.length) {
|
|
effectiveSharedModules = effectiveSharedModules.concat(stats.modules.reduce((sum, module)=>{
|
|
let flag = false;
|
|
for (const entryReExportModule of entryReExportModules){
|
|
if (flag) {
|
|
break;
|
|
}
|
|
if (module.reasons) {
|
|
for (const issueModule of module.reasons){
|
|
if (issueModule.moduleName === entryReExportModule.name) {
|
|
sum.push([
|
|
getSharedModuleName(entryReExportModule.name),
|
|
module
|
|
]);
|
|
flag = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return sum;
|
|
}, []));
|
|
}
|
|
return effectiveSharedModules;
|
|
}
|
|
function getAssetsByChunk(chunk, entryPointNames) {
|
|
const assesSet = {
|
|
js: {
|
|
sync: new Set(),
|
|
async: new Set()
|
|
},
|
|
css: {
|
|
sync: new Set(),
|
|
async: new Set()
|
|
}
|
|
};
|
|
const collectChunkFiles = (targetChunk, type)=>{
|
|
[
|
|
...targetChunk.groupsIterable
|
|
].forEach((chunkGroup)=>{
|
|
if (chunkGroup.name && !entryPointNames.includes(chunkGroup.name)) {
|
|
collectAssets(chunkGroup.getFiles(), assesSet.js[type], assesSet.css[type]);
|
|
}
|
|
});
|
|
};
|
|
collectChunkFiles(chunk, 'sync');
|
|
[
|
|
...chunk.getAllAsyncChunks()
|
|
].forEach((asyncChunk)=>{
|
|
collectAssets([
|
|
...asyncChunk.files
|
|
], assesSet.js['async'], assesSet.css['async']);
|
|
collectChunkFiles(asyncChunk, 'async');
|
|
});
|
|
const assets = {
|
|
js: {
|
|
sync: Array.from(assesSet.js.sync),
|
|
async: Array.from(assesSet.js.async)
|
|
},
|
|
css: {
|
|
sync: Array.from(assesSet.css.sync),
|
|
async: Array.from(assesSet.css.async)
|
|
}
|
|
};
|
|
return assets;
|
|
}
|
|
function assert(condition, msg) {
|
|
if (!condition) {
|
|
error(msg);
|
|
}
|
|
}
|
|
function error(msg) {
|
|
throw new Error(`[ ${PLUGIN_IDENTIFIER} ]: ${msg}`);
|
|
}
|
|
function isDev() {
|
|
return process.env['NODE_ENV'] === 'development';
|
|
}
|
|
function getFileNameWithOutExt(str) {
|
|
return str.replace(path.extname(str), '');
|
|
}
|
|
function getTypesMetaInfo(pluginOptions, context) {
|
|
const defaultRemoteOptions = {
|
|
generateAPITypes: true,
|
|
compileInChildProcess: true
|
|
};
|
|
const defaultTypesMetaInfo = {
|
|
path: '',
|
|
name: '',
|
|
zip: '',
|
|
api: ''
|
|
};
|
|
try {
|
|
const normalizedDtsOptions = normalizeOptions(isTSProject(pluginOptions.dts, context), {
|
|
generateTypes: defaultRemoteOptions,
|
|
consumeTypes: {}
|
|
}, 'mfOptions.dts')(pluginOptions.dts);
|
|
if (normalizedDtsOptions === false) {
|
|
return defaultTypesMetaInfo;
|
|
}
|
|
const normalizedRemote = normalizeOptions(true, defaultRemoteOptions, 'mfOptions.dts.generateTypes')(normalizedDtsOptions.generateTypes);
|
|
if (normalizedRemote === false) {
|
|
return defaultTypesMetaInfo;
|
|
}
|
|
const { apiFileName, zipName } = retrieveTypesAssetsInfo({
|
|
...normalizedRemote,
|
|
context,
|
|
moduleFederationConfig: pluginOptions
|
|
});
|
|
return {
|
|
path: '',
|
|
name: '',
|
|
zip: zipName,
|
|
api: apiFileName
|
|
};
|
|
} catch (err) {
|
|
logger.warn(`getTypesMetaInfo failed, it will use the default types meta info, and the errors as belows: ${err}`);
|
|
return defaultTypesMetaInfo;
|
|
}
|
|
}
|
|
|
|
export { assert, error, findChunk, getAssetsByChunk, getAssetsByChunkIDs, getFileNameWithOutExt, getSharedModules, getTypesMetaInfo, isDev };
|