Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | import type { ExternalItem, ExternalItemFunctionData } from '@rspack/core';
import type { RspackChain } from 'rspack-chain';
import type { ParsedModuleLinkPluginOptions } from './types';
export function applyEntryConfig(
chain: RspackChain,
opts: ParsedModuleLinkPluginOptions
): void {
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: RspackChain) {
chain.output
.set('module', true)
.set('chunkFormat', 'module')
.set('chunkLoading', 'import')
.set('workerChunkLoading', 'import');
chain.output.library({
type: 'module'
});
}
export function applyExternalsConfig(
chain: RspackChain,
opts: ParsedModuleLinkPluginOptions
): void {
const existingExternals = chain.get('externals') || [];
const externals: ExternalItem[] = 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: ParsedModuleLinkPluginOptions,
compilerContext: string
) {
const importMap = new Map<string, string>();
let initPromise: Promise<void> | null = null;
type ResolvePath = (
request: string,
context?: string
) => Promise<string | null>;
const init = (resolvePath: ResolvePath): Promise<void> => {
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: string,
context: string,
resolvePath: ResolvePath
): Promise<string | null> => {
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: ExternalItemFunctionData) => {
if (
!data.request ||
!data.context ||
!data.contextInfo?.issuer ||
FILE_EXT_REGEX.test(data.contextInfo.issuer)
)
return;
const defaultContext = compilerContext;
const resolvePath: ResolvePath = async (
request: string,
context = defaultContext
): Promise<string | null> => {
if (!data.getResolve) {
return null;
}
const resolveFunc = data.getResolve();
return new Promise<string | null>((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}`;
}
};
}
|