MCPcopy Create free account
hub / github.com/getsentry/sentry-javascript / renderChunk

Function renderChunk

packages/bundler-plugins/src/rollup/index.ts:140–193  ·  view source on GitHub ↗
(
    code: string,
    chunk: { fileName: string; facadeModuleId?: string | null },
    _?: unknown,
    meta?: { magicString?: MagicString },
  )

Source from the content-addressed store, hash-verified

138 }
139
140 function renderChunk(
141 code: string,
142 chunk: { fileName: string; facadeModuleId?: string | null },
143 _?: unknown,
144 meta?: { magicString?: MagicString },
145 ): {
146 code: string;
147 map?: SourceMap;
148 } | null {
149 if (!isJsFile(chunk.fileName)) {
150 return null; // returning null means not modifying the chunk at all
151 }
152
153 // Skip empty chunks and HTML facade chunks (Vite MPA)
154 if (shouldSkipCodeInjection(code, chunk.facadeModuleId)) {
155 return null;
156 }
157
158 const injectCode = staticInjectionCode.clone();
159
160 if (sourcemapsEnabled && !hasExistingDebugID(code)) {
161 const debugId = stringToUUID(code); // generate a deterministic debug ID
162 injectCode.append(getDebugIdSnippet(debugId));
163 }
164
165 if (injectCode.isEmpty()) {
166 return null;
167 }
168
169 const ms = meta?.magicString || new MagicString(code, { filename: chunk.fileName });
170 const match = code.match(COMMENT_USE_STRICT_REGEX)?.[0];
171
172 if (match) {
173 // Add injected code after any comments or "use strict" at the beginning of the bundle.
174 ms.appendLeft(match.length, injectCode.code());
175 } else {
176 // ms.replace() doesn't work when there is an empty string match (which happens if
177 // there is neither, a comment, nor a "use strict" at the top of the chunk) so we
178 // need this special case here.
179 ms.prepend(injectCode.code());
180 }
181
182 // Rolldown can pass a native MagicString instance in meta.magicString
183 // https://rolldown.rs/in-depth/native-magic-string#usage-examples
184 if (ms?.constructor?.name === 'BindingMagicString') {
185 // Rolldown docs say to return the magic string instance directly in this case
186 return { code: ms as unknown as string };
187 }
188
189 return {
190 code: ms.toString(),
191 map: ms.generateMap({ file: chunk.fileName, hires: 'boundary' as unknown as undefined }),
192 };
193 }
194
195 async function writeBundle(
196 outputOptions: { dir?: string; file?: string },

Callers 1

public-api.test.tsFile · 0.50

Calls 11

isJsFileFunction · 0.90
shouldSkipCodeInjectionFunction · 0.90
stringToUUIDFunction · 0.90
getDebugIdSnippetFunction · 0.90
hasExistingDebugIDFunction · 0.85
isEmptyMethod · 0.80
matchMethod · 0.80
codeMethod · 0.80
cloneMethod · 0.65
appendMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected