Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
44 changes: 24 additions & 20 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Options, SentrySDKBuildFlags } from "./types";
import {
CodeInjection,
containsOnlyImports,
generateGlobalInjectorCode,
generateReleaseInjectorCode,
generateModuleMetadataInjectorCode,
replaceBooleanFlagsInCode,
stringToUUID,
Expand Down Expand Up @@ -118,7 +118,7 @@ export function sentryUnpluginFactory({
"No release name provided. Will not inject release. Please set the `release.name` option to identify your release."
);
} else {
const code = generateGlobalInjectorCode({
const code = generateReleaseInjectorCode({
release: options.release.name,
injectBuildInformation: options._experiments.injectBuildInformation || false,
});
Expand Down Expand Up @@ -227,7 +227,7 @@ export function sentryCliBinaryExists(): boolean {

// 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 =
export 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])*(?:"[^"]*";|'[^']*';)?/;

Expand All @@ -253,7 +253,7 @@ type RenderChunkHook = (
* Checks if a file is a JavaScript file based on its extension.
* Handles query strings and hashes in the filename.
*/
function isJsFile(fileName: string): boolean {
export function isJsFile(fileName: string): boolean {
const cleanFileName = stripQueryAndHashFromPath(fileName);
return [".js", ".mjs", ".cjs"].some((ext) => cleanFileName.endsWith(ext));
}
Expand All @@ -272,7 +272,10 @@ function isJsFile(fileName: string): boolean {
* @param facadeModuleId - The facade module ID (if any) - HTML files create facade chunks
* @returns true if the chunk should be skipped
*/
function shouldSkipCodeInjection(code: string, facadeModuleId: string | null | undefined): boolean {
export function shouldSkipCodeInjection(
code: string,
facadeModuleId: string | null | undefined
): boolean {
// Skip empty chunks - these are placeholder chunks that should be optimized away
if (code.trim().length === 0) {
return true;
Expand Down Expand Up @@ -369,6 +372,19 @@ export function createRollupInjectionHooks(
};
}

export function globFiles(outputDir: string): Promise<string[]> {
return glob(
["/**/*.js", "/**/*.mjs", "/**/*.cjs", "/**/*.js.map", "/**/*.mjs.map", "/**/*.cjs.map"].map(
(q) => `${q}?(\\?*)?(#*)`
), // We want to allow query and hashes strings at the end of files
{
root: outputDir,
absolute: true,
nodir: true,
}
);
}

export function createRollupDebugIdUploadHooks(
upload: (buildArtifacts: string[]) => Promise<void>,
_logger: Logger,
Expand All @@ -388,21 +404,7 @@ export function createRollupDebugIdUploadHooks(
try {
if (outputOptions.dir) {
const outputDir = outputOptions.dir;
const buildArtifacts = await glob(
[
"/**/*.js",
"/**/*.mjs",
"/**/*.cjs",
"/**/*.js.map",
"/**/*.mjs.map",
"/**/*.cjs.map",
].map((q) => `${q}?(\\?*)?(#*)`), // We want to allow query and hashes strings at the end of files
{
root: outputDir,
absolute: true,
nodir: true,
}
);
const buildArtifacts = await globFiles(outputDir);
await upload(buildArtifacts);
} else if (outputOptions.file) {
await upload([outputOptions.file]);
Expand Down Expand Up @@ -492,3 +494,5 @@ export type { Logger } from "./logger";
export type { Options, SentrySDKBuildFlags } from "./types";
export { CodeInjection, replaceBooleanFlagsInCode, stringToUUID } from "./utils";
export { createSentryBuildPluginManager } from "./build-plugin-manager";
export { generateReleaseInjectorCode, generateModuleMetadataInjectorCode } from "./utils";
export { createDebugIdUploadFunction } from "./debug-id-upload";
2 changes: 1 addition & 1 deletion packages/bundler-plugin-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export function determineReleaseName(): string | undefined {
* Generates code for the global injector which is responsible for setting the global
* `SENTRY_RELEASE` & `SENTRY_BUILD_INFO` variables.
*/
export function generateGlobalInjectorCode({
export function generateReleaseInjectorCode({
release,
injectBuildInformation,
}: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generateGlobalInjectorCode generates code with release 1`] = `"!function(){try{var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{};e.SENTRY_RELEASE={id:\\"1.2.3\\"};}catch(e){}}();"`;

exports[`generateGlobalInjectorCode generates code with release and build information 1`] = `"!function(){try{var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{};e.SENTRY_RELEASE={id:\\"1.2.3\\"};e.SENTRY_BUILD_INFO={\\"deps\\":[\\"myDep\\",\\"rollup\\"],\\"depsVersions\\":{\\"rollup\\":3},\\"nodeVersion\\":18};}catch(e){}}();"`;

exports[`generateModuleMetadataInjectorCode generates code with empty metadata object 1`] = `"!function(){try{var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},e._sentryModuleMetadata[(new e.Error).stack]=function(e){for(var n=1;n<arguments.length;n++){var a=arguments[n];if(null!=a)for(var t in a)a.hasOwnProperty(t)&&(e[t]=a[t])}return e}({},e._sentryModuleMetadata[(new e.Error).stack],{});}catch(e){}}();"`;

exports[`generateModuleMetadataInjectorCode generates code with metadata object 1`] = `"!function(){try{var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},e._sentryModuleMetadata[(new e.Error).stack]=function(e){for(var n=1;n<arguments.length;n++){var a=arguments[n];if(null!=a)for(var t in a)a.hasOwnProperty(t)&&(e[t]=a[t])}return e}({},e._sentryModuleMetadata[(new e.Error).stack],{\\"file1.js\\":{\\"foo\\":\\"bar\\"},\\"file2.js\\":{\\"bar\\":\\"baz\\"}});}catch(e){}}();"`;

exports[`generateReleaseInjectorCode generates code with release 1`] = `"!function(){try{var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{};e.SENTRY_RELEASE={id:\\"1.2.3\\"};}catch(e){}}();"`;

exports[`generateReleaseInjectorCode generates code with release and build information 1`] = `"!function(){try{var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{};e.SENTRY_RELEASE={id:\\"1.2.3\\"};e.SENTRY_BUILD_INFO={\\"deps\\":[\\"myDep\\",\\"rollup\\"],\\"depsVersions\\":{\\"rollup\\":3},\\"nodeVersion\\":18};}catch(e){}}();"`;
8 changes: 4 additions & 4 deletions packages/bundler-plugin-core/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
generateGlobalInjectorCode,
generateReleaseInjectorCode,
generateModuleMetadataInjectorCode,
getDependencies,
getPackageJson,
Expand Down Expand Up @@ -221,9 +221,9 @@ if (false && true) {
});
});

describe("generateGlobalInjectorCode", () => {
describe("generateReleaseInjectorCode", () => {
it("generates code with release", () => {
const generatedCode = generateGlobalInjectorCode({
const generatedCode = generateReleaseInjectorCode({
release: "1.2.3",
injectBuildInformation: false,
});
Expand All @@ -244,7 +244,7 @@ describe("generateGlobalInjectorCode", () => {
})
);

const generatedCode = generateGlobalInjectorCode({
const generatedCode = generateReleaseInjectorCode({
release: "1.2.3",
injectBuildInformation: true,
});
Expand Down
Loading
Loading