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 experimental module metadata injection #334

Merged
merged 7 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { allowedToSendTelemetry, createSentryInstance } from "./sentry/telemetry
import { Options } from "./types";
import {
generateGlobalInjectorCode,
generateModuleMetadataInjectorCode,
getDependencies,
getPackageJson,
parseMajorVersion,
Expand All @@ -22,6 +23,7 @@ import * as dotenv from "dotenv";

interface SentryUnpluginFactoryOptions {
releaseInjectionPlugin: (injectionCode: string) => UnpluginOptions;
moduleMetadataInjectionPlugin?: (injectionCode: string) => UnpluginOptions;
debugIdInjectionPlugin: () => UnpluginOptions;
debugIdUploadPlugin: (upload: (buildArtifacts: string[]) => Promise<void>) => UnpluginOptions;
}
Expand Down Expand Up @@ -55,6 +57,7 @@ interface SentryUnpluginFactoryOptions {
*/
export function sentryUnpluginFactory({
releaseInjectionPlugin,
moduleMetadataInjectionPlugin,
debugIdInjectionPlugin,
debugIdUploadPlugin,
}: SentryUnpluginFactoryOptions) {
Expand Down Expand Up @@ -173,6 +176,27 @@ export function sentryUnpluginFactory({
plugins.push(releaseInjectionPlugin(injectionCode));
}

if (moduleMetadataInjectionPlugin && options._experiments.moduleMetadata) {
let metadata: object;
if (typeof options._experiments.moduleMetadata === "function") {
const args = {
org: options.org,
project: options.project,
release: options.release.name,
dist: options.release.dist,
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
metadata = options._experiments.moduleMetadata(args);
} else {
metadata = options._experiments.moduleMetadata;
}

const injectionCode = generateModuleMetadataInjectorCode(metadata);
plugins.push(moduleMetadataInjectionPlugin(injectionCode));
} else if (options._experiments.moduleMetadata) {
logger.warn("'moduleMetadata' is currently only supported by '@sentry/webpack-plugin'");
}

if (!options.release.name) {
logger.warn(
"No release name provided. Will not create release. Please set the `release.name` option to identifiy your release."
Expand Down
17 changes: 17 additions & 0 deletions packages/bundler-plugin-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,26 @@ export interface Options {
* Defaults to `false`.
*/
injectBuildInformation?: boolean;

/**
* Metadata associated with this module.
* The metadata is serialized and can be looked up at runtime by URL.
*
* Note: This option is currently only supported by `@sentry/webpack-plugin`.
*/
moduleMetadata?: object | ModuleMetadataCallback;
timfish marked this conversation as resolved.
Show resolved Hide resolved
};
}

export interface ModuleMetadataCallbackArgs {
org?: string;
project?: string;
release?: string;
dist?: string;
timfish marked this conversation as resolved.
Show resolved Hide resolved
}

export type ModuleMetadataCallback = (args: ModuleMetadataCallbackArgs) => object;
timfish marked this conversation as resolved.
Show resolved Hide resolved

export type IncludeEntry = {
/**
* One or more paths to scan for files to upload.
Expand Down
18 changes: 18 additions & 0 deletions packages/bundler-plugin-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,24 @@ export function generateGlobalInjectorCode({
return code;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function generateModuleMetadataInjectorCode(metadata: any) {
// The code below is mostly ternary operators because it saves bundle size.
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
return `
var _global2 =
typeof window !== 'undefined' ?
window :
typeof global !== 'undefined' ?
global :
typeof self !== 'undefined' ?
self :
{};

_global2.__MODULE_METADATA__ = _global2.__MODULE_METADATA__ || {};
_global2.__MODULE_METADATA__[new Error().stack] = ${JSON.stringify(metadata)};`;
timfish marked this conversation as resolved.
Show resolved Hide resolved
}

function getBuildInformation() {
const packageJson = getPackageJson();

Expand Down
22 changes: 22 additions & 0 deletions packages/webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,30 @@ function webpackDebugIdUploadPlugin(
};
}

function webpackModuleMetadataInjectionPlugin(injectionCode: string): UnpluginOptions {
return {
name: "sentry-webpack-module-metadata-injection-plugin",
webpack(compiler) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore webpack version compatibility shenanigans
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const BannerPlugin = compiler?.webpack?.BannerPlugin || webback4or5?.BannerPlugin;
compiler.options.plugins = compiler.options.plugins || [];
compiler.options.plugins.push(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
new BannerPlugin({
raw: true,
include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
banner: injectionCode,
})
);
},
};
}

const sentryUnplugin = sentryUnpluginFactory({
releaseInjectionPlugin: webpackReleaseInjectionPlugin,
moduleMetadataInjectionPlugin: webpackModuleMetadataInjectionPlugin,
debugIdInjectionPlugin: webpackDebugIdInjectionPlugin,
debugIdUploadPlugin: webpackDebugIdUploadPlugin,
});
Expand Down