Skip to content

Commit 841bd5d

Browse files
committed
refactor: extract file type check
1 parent 2347b6d commit 841bd5d

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

  • packages/bundler-plugin-core/src

packages/bundler-plugin-core/src/index.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ type RenderChunkHook = (
213213
map: SourceMap;
214214
} | null;
215215

216+
/**
217+
* Checks if a file is a JavaScript file based on its extension.
218+
* Handles query strings and hashes in the filename.
219+
*/
220+
function isJsFile(fileName: string): boolean {
221+
const cleanFileName = stripQueryAndHashFromPath(fileName);
222+
return [".js", ".mjs", ".cjs"].some((ext) => cleanFileName.endsWith(ext));
223+
}
224+
216225
/**
217226
* Checks if a chunk should be skipped for code injection
218227
*
@@ -244,12 +253,7 @@ export function createRollupReleaseInjectionHooks(injectionCode: string): {
244253
} {
245254
return {
246255
renderChunk(code: string, chunk: { fileName: string; facadeModuleId?: string | null }) {
247-
if (
248-
// chunks could be any file (html, md, ...)
249-
![".js", ".mjs", ".cjs"].some((ending) =>
250-
stripQueryAndHashFromPath(chunk.fileName).endsWith(ending)
251-
)
252-
) {
256+
if (!isJsFile(chunk.fileName)) {
253257
return null; // returning null means not modifying the chunk at all
254258
}
255259

@@ -295,12 +299,7 @@ export function createRollupDebugIdInjectionHooks(): {
295299
} {
296300
return {
297301
renderChunk(code: string, chunk: { fileName: string; facadeModuleId?: string | null }) {
298-
if (
299-
// chunks could be any file (html, md, ...)
300-
![".js", ".mjs", ".cjs"].some((ending) =>
301-
stripQueryAndHashFromPath(chunk.fileName).endsWith(ending)
302-
)
303-
) {
302+
if (!isJsFile(chunk.fileName)) {
304303
return null; // returning null means not modifying the chunk at all
305304
}
306305

@@ -350,12 +349,7 @@ export function createRollupModuleMetadataInjectionHooks(injectionCode: string):
350349
} {
351350
return {
352351
renderChunk(code: string, chunk: { fileName: string; facadeModuleId?: string | null }) {
353-
if (
354-
// chunks could be any file (html, md, ...)
355-
![".js", ".mjs", ".cjs"].some((ending) =>
356-
stripQueryAndHashFromPath(chunk.fileName).endsWith(ending)
357-
)
358-
) {
352+
if (!isJsFile(chunk.fileName)) {
359353
return null; // returning null means not modifying the chunk at all
360354
}
361355

0 commit comments

Comments
 (0)