UNPKG

@esmx/rspack

Version:

A high-performance Rspack integration for Esmx microfrontend framework, providing Module Linking and SSR capabilities.

101 lines (100 loc) 3.34 kB
export function applyEntryConfig(chain, opts) { if (chain.entryPoints.has("main")) { const mainEntry = chain.entry("main"); if (mainEntry.values().length === 0) { chain.entryPoints.clear(); } } for (const value of Object.values(opts.exports)) { if (value.file) { const entry = chain.entry(value.name); for (const preEntry of opts.preEntries) { entry.add(preEntry); } entry.add(value.file); } } } export function applyModuleConfig(chain) { chain.output.set("module", true).set("chunkFormat", "module").set("chunkLoading", "import").set("workerChunkLoading", "import"); chain.output.library({ type: "module" }); } export function applyExternalsConfig(chain, opts) { const existingExternals = chain.get("externals") || []; const externals = Array.isArray(existingExternals) ? [...existingExternals] : [existingExternals]; const compilerContext = chain.get("context") ?? process.cwd(); const externalFunc = createExternalsFunction(opts, compilerContext); externals.push(externalFunc); chain.externals(externals); } function createExternalsFunction(opts, compilerContext) { const importMap = /* @__PURE__ */ new Map(); let initPromise = null; const init = (resolvePath) => { if (initPromise) return initPromise; initPromise = (async () => { await Promise.all( Object.values(opts.exports).map(async (value) => { const identifier = value.pkg ? value.name : value.identifier; importMap.set(identifier, identifier); importMap.set(value.name, identifier); const resolvedPath = await resolvePath(value.file); if (resolvedPath) { importMap.set(resolvedPath, identifier); } }) ); for (const key of Object.keys(opts.imports)) { importMap.set(key, key); } })(); return initPromise; }; const match = async (request, context, resolvePath) => { if (!request) return null; if (opts.deps.length > 0) { const matchedDep = opts.deps.find( (dep) => request === dep || request.startsWith(`${dep}/`) ); if (matchedDep) { return request; } } let importName = importMap.get(request); if (!importName) { const resolvedPath = await resolvePath(request, context); if (resolvedPath) { importName = importMap.get(resolvedPath); } } return importName || null; }; const FILE_EXT_REGEX = /\.worker\.(js|mjs|cjs|jsx|mjsx|cjsx|ts|mts|cts|tsx|mtsx|ctsx)$/i; return async (data) => { if (!data.request || !data.context || !data.contextInfo?.issuer || FILE_EXT_REGEX.test(data.contextInfo.issuer)) return; const defaultContext = compilerContext; const resolvePath = async (request, context = defaultContext) => { if (!data.getResolve) { return null; } const resolveFunc = data.getResolve(); return new Promise((resolve) => { resolveFunc(context, request, (err, res) => { resolve(typeof res === "string" ? res : null); }); }); }; await init(resolvePath); const matchedIdentifier = await match( data.request, data.context, resolvePath ); if (matchedIdentifier) { return `module-import ${matchedIdentifier}`; } }; }