Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add module metadata injection for vite and rollup #380

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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
45 changes: 38 additions & 7 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@
};
}

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

export function createRollupDebugIdInjectionHooks() {
return {
renderChunk(code: string, chunk: { fileName: string }) {
Expand All @@ -382,13 +388,7 @@

const ms = new MagicString(code, { filename: chunk.fileName });

// 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 commentUseStrictRegex =
// Note: CodeQL complains that this regex potentially has n^2 runtime. This likely won't affect realistic files.
/^(?:\s*|\/\*(?:.|\r|\n)*?\*\/|\/\/.*[\n\r])*(?:"[^"]*";|'[^']*';)?/;

const match = code.match(commentUseStrictRegex)?.[0];
const match = code.match(COMMENT_USE_STRICT_REGEX)?.[0];
Dismissed Show dismissed Hide dismissed

if (match) {
// Add injected code after any comments or "use strict" at the beginning of the bundle.
Expand All @@ -411,6 +411,37 @@
};
}

export function createRollupModuleMetadataInjectionHooks(injectionCode: string) {
return {
renderChunk(code: string, chunk: { fileName: string }) {
if (
[".js", ".mjs", ".cjs"].some((ending) => chunk.fileName.endsWith(ending)) // chunks could be any file (html, md, ...)
) {
const ms = new MagicString(code, { filename: chunk.fileName });

const match = code.match(COMMENT_USE_STRICT_REGEX)?.[0];
Dismissed Show dismissed Hide dismissed

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({ file: chunk.fileName }),
};
} else {
return null; // returning null means not modifying the chunk at all
}
},
};
}

export function createRollupDebugIdUploadHooks(
upload: (buildArtifacts: string[]) => Promise<void>
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ describe("metadata injection", () => {
test("webpack 5 bundle", () => {
checkBundle(path.join(__dirname, "out", "webpack5", "bundle.js"));
});

test("rollup bundle", () => {
checkBundle(path.join(__dirname, "out", "rollup", "bundle.js"));
});

test("vite bundle", () => {
checkBundle(path.join(__dirname, "out", "vite", "bundle.js"));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ createCjsBundles(
moduleMetadata: { team: "frontend" },
},
},
["webpack4", "webpack5"]
["webpack4", "webpack5", "rollup", "vite"]
);
9 changes: 9 additions & 0 deletions packages/rollup-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
sentryUnpluginFactory,
Options,
createRollupReleaseInjectionHooks,
createRollupModuleMetadataInjectionHooks,
createRollupDebugIdInjectionHooks,
createRollupDebugIdUploadHooks,
} from "@sentry/bundler-plugin-core";
Expand All @@ -21,6 +22,13 @@ function rollupDebugIdInjectionPlugin(): UnpluginOptions {
};
}

function rollupModuleMetadataInjectionPlugin(injectionCode: string): UnpluginOptions {
return {
name: "sentry-rollup-module-metadata-injection-plugin",
rollup: createRollupModuleMetadataInjectionHooks(injectionCode),
};
}

function rollupDebugIdUploadPlugin(
upload: (buildArtifacts: string[]) => Promise<void>
): UnpluginOptions {
Expand All @@ -33,6 +41,7 @@ function rollupDebugIdUploadPlugin(
const sentryUnplugin = sentryUnpluginFactory({
releaseInjectionPlugin: rollupReleaseInjectionPlugin,
debugIdInjectionPlugin: rollupDebugIdInjectionPlugin,
moduleMetadataInjectionPlugin: rollupModuleMetadataInjectionPlugin,
debugIdUploadPlugin: rollupDebugIdUploadPlugin,
});

Expand Down
9 changes: 9 additions & 0 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
sentryUnpluginFactory,
Options,
createRollupReleaseInjectionHooks,
createRollupModuleMetadataInjectionHooks,
createRollupDebugIdInjectionHooks,
createRollupDebugIdUploadHooks,
} from "@sentry/bundler-plugin-core";
Expand All @@ -22,6 +23,13 @@ function viteDebugIdInjectionPlugin(): UnpluginOptions {
};
}

function viteModuleMetadataInjectionPlugin(injectionCode: string): UnpluginOptions {
return {
name: "sentry-vite-module-metadata-injection-plugin",
vite: createRollupModuleMetadataInjectionHooks(injectionCode),
};
}

function viteDebugIdUploadPlugin(
upload: (buildArtifacts: string[]) => Promise<void>
): UnpluginOptions {
Expand All @@ -34,6 +42,7 @@ function viteDebugIdUploadPlugin(
const sentryUnplugin = sentryUnpluginFactory({
releaseInjectionPlugin: viteReleaseInjectionPlugin,
debugIdInjectionPlugin: viteDebugIdInjectionPlugin,
moduleMetadataInjectionPlugin: viteModuleMetadataInjectionPlugin,
debugIdUploadPlugin: viteDebugIdUploadPlugin,
});

Expand Down