Skip to content

Commit 2a47339

Browse files
committed
PoC: Regular Rollup Plugin
1 parent 0131e86 commit 2a47339

File tree

6 files changed

+241
-92
lines changed

6 files changed

+241
-92
lines changed

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Options, SentrySDKBuildFlags } from "./types";
1616
import {
1717
CodeInjection,
1818
containsOnlyImports,
19-
generateGlobalInjectorCode,
19+
generateReleaseInjectorCode,
2020
generateModuleMetadataInjectorCode,
2121
replaceBooleanFlagsInCode,
2222
stringToUUID,
@@ -118,7 +118,7 @@ export function sentryUnpluginFactory({
118118
"No release name provided. Will not inject release. Please set the `release.name` option to identify your release."
119119
);
120120
} else {
121-
const code = generateGlobalInjectorCode({
121+
const code = generateReleaseInjectorCode({
122122
release: options.release.name,
123123
injectBuildInformation: options._experiments.injectBuildInformation || false,
124124
});
@@ -227,7 +227,7 @@ export function sentryCliBinaryExists(): boolean {
227227

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

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

375+
export function globFiles(outputDir: string): Promise<string[]> {
376+
return glob(
377+
["/**/*.js", "/**/*.mjs", "/**/*.cjs", "/**/*.js.map", "/**/*.mjs.map", "/**/*.cjs.map"].map(
378+
(q) => `${q}?(\\?*)?(#*)`
379+
), // We want to allow query and hashes strings at the end of files
380+
{
381+
root: outputDir,
382+
absolute: true,
383+
nodir: true,
384+
}
385+
);
386+
}
387+
372388
export function createRollupDebugIdUploadHooks(
373389
upload: (buildArtifacts: string[]) => Promise<void>,
374390
_logger: Logger,
@@ -388,21 +404,7 @@ export function createRollupDebugIdUploadHooks(
388404
try {
389405
if (outputOptions.dir) {
390406
const outputDir = outputOptions.dir;
391-
const buildArtifacts = await glob(
392-
[
393-
"/**/*.js",
394-
"/**/*.mjs",
395-
"/**/*.cjs",
396-
"/**/*.js.map",
397-
"/**/*.mjs.map",
398-
"/**/*.cjs.map",
399-
].map((q) => `${q}?(\\?*)?(#*)`), // We want to allow query and hashes strings at the end of files
400-
{
401-
root: outputDir,
402-
absolute: true,
403-
nodir: true,
404-
}
405-
);
407+
const buildArtifacts = await globFiles(outputDir);
406408
await upload(buildArtifacts);
407409
} else if (outputOptions.file) {
408410
await upload([outputOptions.file]);
@@ -492,3 +494,5 @@ export type { Logger } from "./logger";
492494
export type { Options, SentrySDKBuildFlags } from "./types";
493495
export { CodeInjection, replaceBooleanFlagsInCode, stringToUUID } from "./utils";
494496
export { createSentryBuildPluginManager } from "./build-plugin-manager";
497+
export { generateReleaseInjectorCode, generateModuleMetadataInjectorCode } from "./utils";
498+
export { createDebugIdUploadFunction } from "./debug-id-upload";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export function determineReleaseName(): string | undefined {
305305
* Generates code for the global injector which is responsible for setting the global
306306
* `SENTRY_RELEASE` & `SENTRY_BUILD_INFO` variables.
307307
*/
308-
export function generateGlobalInjectorCode({
308+
export function generateReleaseInjectorCode({
309309
release,
310310
injectBuildInformation,
311311
}: {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
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){}}();"`;
4-
5-
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){}}();"`;
6-
73
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){}}();"`;
84
95
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){}}();"`;
6+
7+
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){}}();"`;
8+
9+
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){}}();"`;

packages/bundler-plugin-core/test/utils.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
generateGlobalInjectorCode,
2+
generateReleaseInjectorCode,
33
generateModuleMetadataInjectorCode,
44
getDependencies,
55
getPackageJson,
@@ -221,9 +221,9 @@ if (false && true) {
221221
});
222222
});
223223

224-
describe("generateGlobalInjectorCode", () => {
224+
describe("generateReleaseInjectorCode", () => {
225225
it("generates code with release", () => {
226-
const generatedCode = generateGlobalInjectorCode({
226+
const generatedCode = generateReleaseInjectorCode({
227227
release: "1.2.3",
228228
injectBuildInformation: false,
229229
});
@@ -244,7 +244,7 @@ describe("generateGlobalInjectorCode", () => {
244244
})
245245
);
246246

247-
const generatedCode = generateGlobalInjectorCode({
247+
const generatedCode = generateReleaseInjectorCode({
248248
release: "1.2.3",
249249
injectBuildInformation: true,
250250
});

0 commit comments

Comments
 (0)