Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 44 additions & 69 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,63 +182,59 @@ export function sentryCliBinaryExists(): boolean {
return fs.existsSync(SentryCli.getPath());
}

// We need to be careful not to inject the snippet before any `"use strict";`s.
// As an additional complication `"use strict";`s may come after any number of comments.
const COMMENT_USE_STRICT_REGEX =
// Note: CodeQL complains that this regex potentially has n^2 runtime. This likely won't affect realistic files.
/^(?:\s*|\/\*(?:.|\r|\n)*?\*\/|\/\/.*[\n\r])*(?:"[^"]*";|'[^']*';)?/;

/**
* Simplified `renderChunk` hook type from Rollup.
* We can't reference the type directly because the Vite plugin complains
* about type mismatches
*/
type RenderChunkHook = (
code: string,
chunk: {
fileName: string;
}
) => {
code: string;
map: SourceMap;
} | null;

export function createRollupReleaseInjectionHooks(injectionCode: string): {
resolveId: UnpluginOptions["resolveId"];
load: UnpluginOptions["load"];
transform: UnpluginOptions["transform"];
renderChunk: RenderChunkHook;
} {
const virtualReleaseInjectionFileId = "\0sentry-release-injection-file";
return {
resolveId(id: string) {
if (id === virtualReleaseInjectionFileId) {
return {
id: virtualReleaseInjectionFileId,
external: false,
moduleSideEffects: true,
};
} else {
return null;
}
},

load(id: string) {
if (id === virtualReleaseInjectionFileId) {
return injectionCode;
} else {
return null;
}
},

transform(code: string, id: string) {
if (id === virtualReleaseInjectionFileId) {
return null;
}

// id may contain query and hash which will trip up our file extension logic below
const idWithoutQueryAndHash = stripQueryAndHashFromPath(id);

if (idWithoutQueryAndHash.match(/\\node_modules\\|\/node_modules\//)) {
return null;
}

renderChunk(code: string, chunk: { fileName: string }) {
if (
![".js", ".ts", ".jsx", ".tsx", ".mjs"].some((ending) =>
idWithoutQueryAndHash.endsWith(ending)
// chunks could be any file (html, md, ...)
[".js", ".mjs", ".cjs"].some((ending) =>
stripQueryAndHashFromPath(chunk.fileName).endsWith(ending)
)
) {
return null;
}
const ms = new MagicString(code, { filename: chunk.fileName });

const ms = new MagicString(code);
const match = code.match(COMMENT_USE_STRICT_REGEX)?.[0];

// Appending instead of prepending has less probability of mucking with user's source maps.
// Luckily import statements get hoisted to the top anyways.
ms.append(`\n\n;import "${virtualReleaseInjectionFileId}";`);
if (match) {
// Add injected code after any comments or "use strict" at the beginning of the bundle.
ms.appendLeft(match.length, injectionCode);
} else {
// ms.replace() doesn't work when there is an empty string match (which happens if
// there is neither, a comment, nor a "use strict" at the top of the chunk) so we
// need this special case here.
ms.prepend(injectionCode);
}

return {
code: ms.toString(),
map: ms.generateMap({ hires: "boundary" }),
};
return {
code: ms.toString(),
map: ms.generateMap({ file: chunk.fileName, hires: "boundary" }),
};
} else {
return null; // returning null means not modifying the chunk at all
}
},
};
}
Expand All @@ -253,27 +249,6 @@ export function createRollupBundleSizeOptimizationHooks(replacementValues: Sentr
};
}

// We need to be careful not to inject the snippet before any `"use strict";`s.
// As an additional complication `"use strict";`s may come after any number of comments.
const COMMENT_USE_STRICT_REGEX =
// Note: CodeQL complains that this regex potentially has n^2 runtime. This likely won't affect realistic files.
/^(?:\s*|\/\*(?:.|\r|\n)*?\*\/|\/\/.*[\n\r])*(?:"[^"]*";|'[^']*';)?/;

/**
* Simplified `renderChunk` hook type from Rollup.
* We can't reference the type directly because the Vite plugin complains
* about type mismatches
*/
type RenderChunkHook = (
code: string,
chunk: {
fileName: string;
}
) => {
code: string;
map: SourceMap;
} | null;

export function createRollupDebugIdInjectionHooks(): {
renderChunk: RenderChunkHook;
} {
Expand Down
Loading