Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
a53c5df
Initial commit on the React annotation experiment
0Calories Jan 18, 2024
04c3099
delete some stuff
0Calories Jan 18, 2024
d2409f5
Properly configure the parser plugins
0Calories Jan 22, 2024
96adebe
Simplify transform hook and allow JSX files to be transformed
0Calories Jan 23, 2024
045ae81
Remove unnecessary presets from babelrc
0Calories Jan 23, 2024
9f22305
remove packages
0Calories Jan 23, 2024
9898f32
change version of babel core back
0Calories Jan 23, 2024
11c45c8
remove ds store
0Calories Jan 23, 2024
c2a37d3
remove unnecessary package
0Calories Jan 23, 2024
10b08c4
cleanup dead comments, fix broken logic
0Calories Jan 23, 2024
9102567
Adjust incompatible plugins and add comment
0Calories Jan 23, 2024
e2b42bc
Delete DS store
0Calories Jan 23, 2024
55ab44d
remove unnecessary babel options from eslint
0Calories Jan 23, 2024
f7b3622
remove unnecessary packages
0Calories Jan 23, 2024
f611ea8
remove unnecessary config file
0Calories Jan 23, 2024
6b16b2a
Start working on webpack compatibility
0Calories Jan 24, 2024
f1beec7
Add React types
gggritso Jan 23, 2024
51ae921
Add test harness
gggritso Jan 23, 2024
b5e3324
Simplify test harness
gggritso Jan 23, 2024
a105893
Mark React JSX runtime as external dependency
gggritso Jan 24, 2024
c8ac897
Unhook annotation plugin for unsupported bundlers
gggritso Jan 24, 2024
0928c68
Add actual spec for bundle output
gggritso Jan 24, 2024
5e9fc7a
More complicated import scheme
gggritso Jan 24, 2024
3340184
Test using real React rendering
gggritso Jan 24, 2024
fbe80fd
Set up Rollup annotation tests
gggritso Jan 24, 2024
c86dd16
Improve Webpack 5 error handling
gggritso Jan 25, 2024
96765d9
Set up Webpack annotation tests
gggritso Jan 25, 2024
0855cfc
Downgrade `babel-loader`
gggritso Jan 25, 2024
712c93b
Improve Webpack 4 error reporting
gggritso Jan 25, 2024
bf9c2a8
Update yarn.lock?
gggritso Jan 25, 2024
08ec6b1
Add Webpack 4 test
gggritso Jan 25, 2024
c140195
Extract Webpack handling
gggritso Jan 25, 2024
e311706
Extract constants
gggritso Jan 25, 2024
972b761
Remove unnecessary line moves
gggritso Jan 25, 2024
c37750d
Use consistent test order
gggritso Jan 25, 2024
965d2db
Split up the CJS bundle helpers
gggritso Jan 25, 2024
2e5c7ee
try adding a load hook
0Calories Jan 25, 2024
c554b3c
Update spec
gggritso Jan 25, 2024
2072848
Fix webpack plugin by using `transformInclude` hook
0Calories Jan 25, 2024
e58de0b
Add types for the options object
0Calories Jan 25, 2024
210835f
merge tests
0Calories Jan 25, 2024
a8fe6e8
merge conflict
0Calories Jan 25, 2024
e013cbb
Add options support
0Calories Jan 25, 2024
a4414ff
Adjust options API for react annotate plugin
0Calories Jan 25, 2024
bd5d4cc
Wire up the options
0Calories Jan 25, 2024
057e280
Use options object instead
0Calories Jan 25, 2024
fa85813
Nevermind lets not do that
0Calories Jan 25, 2024
5f77ceb
Wire up the excluded components option, but it does not work yet
0Calories Jan 25, 2024
94a0327
extra vite configs not necessary, also delete options
0Calories Jan 26, 2024
7e8ac0f
Deleted options
0Calories Jan 26, 2024
57d3963
Add ESBuild support
0Calories Jan 26, 2024
49145d3
ESBuild support has been added so the conditional can be removed
0Calories Jan 26, 2024
303b033
Enable annotation in test harness
gggritso Jan 26, 2024
6394e93
Add default export for test app
gggritso Jan 26, 2024
298487a
Add esbuild integration test
gggritso Jan 26, 2024
5cb45e0
Add missing Webpack 4 snapshot
gggritso Jan 26, 2024
84d88f0
Fix path typo
gggritso Jan 26, 2024
caf0f6f
Add telemetry data for react annotate
0Calories Jan 26, 2024
3b667bf
Fix faulty Vite config
gggritso Jan 26, 2024
09a0e99
Fix faulty Rollup config
gggritso Jan 26, 2024
4a9bc92
fix the option for reactAnnotate
0Calories Jan 26, 2024
5f9913f
Merge branch 'experiment/react-annotate-plugin-esbuild-tests' of http…
0Calories Jan 26, 2024
fd748b8
Remove some ts-ignores and fix typing
0Calories Jan 27, 2024
4293a38
remove types.d.ts
0Calories Jan 27, 2024
5f2a93b
fix dependencies
0Calories Jan 27, 2024
5aeaf95
Ignore vendored file
gggritso Jan 29, 2024
a70cc64
Fix logger line
gggritso Jan 29, 2024
5166fc4
Handle all possible plugin results
gggritso Jan 29, 2024
f4b0d39
Allow errors during test setup
gggritso Jan 29, 2024
da92b05
Add missing dependency
gggritso Jan 29, 2024
8749bdf
Enforce no `this` binding in `transform`
gggritso Jan 29, 2024
c787dbe
Rename incorrect snapshot name
gggritso Jan 29, 2024
3864fe6
no unsafe assignment
0Calories Jan 29, 2024
85b4cfd
Switch to snapshot tests
gggritso Jan 29, 2024
022443c
resolve merge conflict
0Calories Jan 29, 2024
f52691a
Update packages/bundler-plugin-core/src/index.ts
0Calories Jan 29, 2024
f84648e
do not use default export
0Calories Jan 29, 2024
6ae09be
Merge branch 'experiment/react-annotate-plugin' of https://github.com…
0Calories Jan 29, 2024
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
1 change: 1 addition & 0 deletions packages/bundler-plugin-core/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
"test/fixtures/**/*",
"sentry-release-injection-file.js",
"sentry-esbuild-debugid-injection-file.js",
"react-annotate-plugin.js",
],
parserOptions: {
tsconfigRootDir: __dirname,
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-plugin-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"fix": "eslint ./src ./test --format stylish --fix"
},
"dependencies": {
"@babel/core": "7.18.5",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to move this to a regular dependency, since it is necessary to run transformAsync from @babel/core during the transform hook

"@sentry/cli": "^2.22.3",
"@sentry/node": "^7.60.0",
"@sentry/utils": "^7.60.0",
Expand All @@ -62,7 +63,6 @@
"unplugin": "1.0.1"
},
"devDependencies": {
"@babel/core": "7.18.5",
"@babel/preset-env": "7.18.2",
"@babel/preset-typescript": "7.17.12",
"@rollup/plugin-babel": "5.3.1",
Expand Down
78 changes: 76 additions & 2 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import SentryCli from "@sentry/cli";
import { transformAsync } from "@babel/core";
import * as fs from "fs";
import * as path from "path";
import MagicString from "magic-string";
import { createUnplugin, UnpluginOptions } from "unplugin";
import { createUnplugin, TransformResult, UnpluginOptions } from "unplugin";
import { normalizeUserOptions, validateOptions } from "./options-mapping";
import { createDebugIdUploadFunction } from "./debug-id-upload";
import { releaseManagementPlugin } from "./plugins/release-management";
Expand All @@ -22,9 +23,16 @@ import {
} from "./utils";
import * as dotenv from "dotenv";
import { glob } from "glob";
import pkg from "@sentry/utils";
const { logger } = pkg;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore Importing the annotate plugin which is not concerned with types
import { reactAnnotate } from "./plugins/react-annotate-plugin";

interface SentryUnpluginFactoryOptions {
releaseInjectionPlugin: (injectionCode: string) => UnpluginOptions;
reactAnnotatePlugin: () => UnpluginOptions;
moduleMetadataInjectionPlugin?: (injectionCode: string) => UnpluginOptions;
debugIdInjectionPlugin: () => UnpluginOptions;
debugIdUploadPlugin: (upload: (buildArtifacts: string[]) => Promise<void>) => UnpluginOptions;
Expand Down Expand Up @@ -60,6 +68,7 @@ interface SentryUnpluginFactoryOptions {
*/
export function sentryUnpluginFactory({
releaseInjectionPlugin,
reactAnnotatePlugin,
moduleMetadataInjectionPlugin,
debugIdInjectionPlugin,
debugIdUploadPlugin,
Expand Down Expand Up @@ -317,6 +326,20 @@ export function sentryUnpluginFactory({
);
}

if (!options.reactAnnotate) {
logger.warn(
"No options provided for the react annotate plugin. Please set `reactAnnotate.enabled` to `true` if you would like to have your React components automatically annotated at build-time."
);
} else if (!options.reactAnnotate.enabled) {
logger.info(
"The react annotate plugin is currently disabled. Skipping react component name annotations."
);
} else {
{
plugins.push(reactAnnotatePlugin());
}
}

return plugins;
});
}
Expand Down Expand Up @@ -346,7 +369,6 @@ export function sentryCliBinaryExists(): boolean {

export function createRollupReleaseInjectionHooks(injectionCode: string) {
const virtualReleaseInjectionFileId = "\0sentry-release-injection-file";

return {
resolveId(id: string) {
if (id === virtualReleaseInjectionFileId) {
Expand Down Expand Up @@ -510,6 +532,58 @@ export function createRollupDebugIdUploadHooks(
};
}

export function createReactAnnotateHooks() {
return {
async transform(this: void, code: string, id: string): Promise<TransformResult> {
// 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\//)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Does this work with windows file paths?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is copied from another transform hook in our release injection plugin, so I assume so (but if not, it's broken in that plugin too :p)

return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: is returning null safe here? Do we have to return { code } instead?

Copy link
Contributor Author

@0Calories 0Calories Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, null is accepted by Unplugin as one of the return values of the transform hook, and I assumed it means that the file skips any transformations. However, I can't see any specific documentation on what returning null does in this hook

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, we already return null in our release injection plugin, under the transform hook for these same situations. Example: https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/main/packages/bundler-plugin-core/src/index.ts#L388

}

// We will only apply this plugin on jsx and tsx files
if (![".jsx", ".tsx"].some((ending) => idWithoutQueryAndHash.endsWith(ending))) {
return null;
}

const parserPlugins = [];
if (idWithoutQueryAndHash.endsWith(".jsx")) {
parserPlugins.push("jsx");
} else if (idWithoutQueryAndHash.endsWith(".tsx")) {
parserPlugins.push("jsx", "typescript");
}

try {
const result = await transformAsync(code, {
plugins: [[reactAnnotate]],
filename: id,
parserOpts: {
sourceType: "module",
allowAwaitOutsideFunction: true,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore This array will always be type compliant when using 'jsx' or 'typescript'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: What is the type error? Can we change the const parserPlugins = []; declaration to avoid having to ts-ignore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type 'string[]' is not assignable to type 'PluginConfig[]'.
  Type 'string' is not assignable to type 'PluginConfig'.ts(2322)
(property) ParserOptions.plugins?: PluginConfig[] | undefined

I've tried to make this type compliant, but it was a colossal pain. The appropriate type is available only in @babel/parser, and I didn't want to import an entire package just for one type. I've tried to use @babel/types as well, but it didn't seem like the type I needed was exported from there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pick the type from using Parameters and typeof transformAsync?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that yesterday but was not successful 😢

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NonNullable<NonNullable<Parameters<typeof transformAsync>[1]>['parserOpts']>['plugins'] should work!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, that did the trick!

plugins: parserPlugins,
},
generatorOpts: {
decoratorsBeforeExport: true,
},
sourceMaps: true,
});

return {
code: result?.code ?? code,
map: result?.map,
};
} catch (e) {
logger.error(`Failed to apply react annotate plugin`, e);
}

return { code };
},
};
}

export function getDebugIdSnippet(debugId: string): string {
return `;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="${debugId}",e._sentryDebugIdIdentifier="sentry-dbid-${debugId}")}catch(e){}}();`;
}
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-plugin-core/src/options-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function normalizeUserOptions(userOptions: UserOptions) {
cleanArtifacts: userOptions.release?.cleanArtifacts ?? false,
},
bundleSizeOptimizations: userOptions.bundleSizeOptimizations,
reactAnnotate: userOptions.reactAnnotate,
_experiments: userOptions._experiments ?? {},
};

Expand Down
Loading