-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathindex.ts
271 lines (240 loc) · 10.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import {
sentryUnpluginFactory,
Options,
getDebugIdSnippet,
SentrySDKBuildFlags,
} from "@sentry/bundler-plugin-core";
import type { Logger } from "@sentry/bundler-plugin-core";
import type { UnpluginOptions } from "unplugin";
import * as path from "path";
import { v4 as uuidv4 } from "uuid";
function esbuildReleaseInjectionPlugin(injectionCode: string): UnpluginOptions {
const pluginName = "sentry-esbuild-release-injection-plugin";
const virtualReleaseInjectionFilePath = path.resolve("_sentry-release-injection-stub"); // needs to be an absolute path for older eslint versions
return {
name: pluginName,
esbuild: {
setup({ initialOptions, onLoad, onResolve }) {
initialOptions.inject = initialOptions.inject || [];
initialOptions.inject.push(virtualReleaseInjectionFilePath);
onResolve({ filter: /_sentry-release-injection-stub/ }, (args) => {
return {
path: args.path,
sideEffects: true,
pluginName,
};
});
onLoad({ filter: /_sentry-release-injection-stub/ }, () => {
return {
loader: "js",
pluginName,
contents: injectionCode,
};
});
},
},
};
}
function esbuildDebugIdInjectionPlugin(logger: Logger): UnpluginOptions {
const pluginName = "sentry-esbuild-debug-id-injection-plugin";
const stubNamespace = "sentry-debug-id-stub";
return {
name: pluginName,
esbuild: {
setup({ initialOptions, onLoad, onResolve }) {
if (initialOptions.bundle) {
logger.warn(
"Esbuild's `bundle: true` option is currently not supported! Esbuild will probably crash now. Sorry about that. If you need to upload sourcemaps with the `bundle` option, it is recommended to use Sentry CLI instead: https://docs.sentry.io/platforms/javascript/sourcemaps/uploading/cli/"
);
}
onResolve({ filter: /.*/ }, (args) => {
if (args.kind !== "entry-point") {
return;
} else {
// Injected modules via the esbuild `inject` option do also have `kind == "entry-point"`.
// We do not want to inject debug IDs into those files because they are already bundled into the entrypoints
if (initialOptions.inject?.includes(args.path)) {
return;
}
return {
pluginName,
// needs to be an abs path, otherwise esbuild will complain
path: path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path),
pluginData: {
isProxyResolver: true,
originalPath: args.path,
originalResolveDir: args.resolveDir,
},
// We need to add a suffix here, otherwise esbuild will mark the entrypoint as resolved and won't traverse
// the module tree any further down past the proxy module because we're essentially creating a dependency
// loop back to the proxy module.
// By setting a suffix we're telling esbuild that the entrypoint and proxy module are two different things,
// making it re-resolve the entrypoint when it is imported from the proxy module.
// Super confusing? Yes. Works? Apparently... Let's see.
suffix: "?sentryProxyModule=true",
};
}
});
onLoad({ filter: /.*/ }, (args) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!(args.pluginData?.isProxyResolver as undefined | boolean)) {
return null;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const originalPath = args.pluginData.originalPath as string;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const originalResolveDir = args.pluginData.originalResolveDir as string;
return {
loader: "js",
pluginName,
// We need to use JSON.stringify below so that any escape backslashes stay escape backslashes, in order not to break paths on windows
contents: `
import "_sentry-debug-id-injection-stub";
import * as OriginalModule from ${JSON.stringify(originalPath)};
export default OriginalModule.default;
export * from ${JSON.stringify(originalPath)};`,
resolveDir: originalResolveDir,
};
});
onResolve({ filter: /_sentry-debug-id-injection-stub/ }, (args) => {
return {
path: args.path,
sideEffects: true,
pluginName,
namespace: stubNamespace,
suffix: "?sentry-module-id=" + uuidv4(), // create different module, each time this is resolved
};
});
onLoad({ filter: /_sentry-debug-id-injection-stub/, namespace: stubNamespace }, () => {
return {
loader: "js",
pluginName,
contents: getDebugIdSnippet(uuidv4()),
};
});
},
},
};
}
function esbuildModuleMetadataInjectionPlugin(injectionCode: string): UnpluginOptions {
const pluginName = "sentry-esbuild-module-metadata-injection-plugin";
const stubNamespace = "sentry-module-metadata-stub";
return {
name: pluginName,
esbuild: {
setup({ initialOptions, onLoad, onResolve }) {
onResolve({ filter: /.*/ }, (args) => {
if (args.kind !== "entry-point") {
return;
} else {
// Injected modules via the esbuild `inject` option do also have `kind == "entry-point"`.
// We do not want to inject debug IDs into those files because they are already bundled into the entrypoints
if (initialOptions.inject?.includes(args.path)) {
return;
}
return {
pluginName,
// needs to be an abs path, otherwise esbuild will complain
path: path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path),
pluginData: {
isMetadataProxyResolver: true,
originalPath: args.path,
originalResolveDir: args.resolveDir,
},
// We need to add a suffix here, otherwise esbuild will mark the entrypoint as resolved and won't traverse
// the module tree any further down past the proxy module because we're essentially creating a dependency
// loop back to the proxy module.
// By setting a suffix we're telling esbuild that the entrypoint and proxy module are two different things,
// making it re-resolve the entrypoint when it is imported from the proxy module.
// Super confusing? Yes. Works? Apparently... Let's see.
suffix: "?sentryMetadataProxyModule=true",
};
}
});
onLoad({ filter: /.*/ }, (args) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!(args.pluginData?.isMetadataProxyResolver as undefined | boolean)) {
return null;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const originalPath = args.pluginData.originalPath as string;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const originalResolveDir = args.pluginData.originalResolveDir as string;
return {
loader: "js",
pluginName,
// We need to use JSON.stringify below so that any escape backslashes stay escape backslashes, in order not to break paths on windows
contents: `
import "_sentry-module-metadata-injection-stub";
import * as OriginalModule from ${JSON.stringify(originalPath)};
export default OriginalModule.default;
export * from ${JSON.stringify(originalPath)};`,
resolveDir: originalResolveDir,
};
});
onResolve({ filter: /_sentry-module-metadata-injection-stub/ }, (args) => {
return {
path: args.path,
sideEffects: true,
pluginName,
namespace: stubNamespace,
suffix: "?sentry-module-id=" + uuidv4(), // create different module, each time this is resolved
};
});
onLoad(
{ filter: /_sentry-module-metadata-injection-stub/, namespace: stubNamespace },
() => {
return {
loader: "js",
pluginName,
contents: injectionCode,
};
}
);
},
},
};
}
function esbuildDebugIdUploadPlugin(
upload: (buildArtifacts: string[]) => Promise<void>
): UnpluginOptions {
return {
name: "sentry-esbuild-debug-id-upload-plugin",
esbuild: {
setup({ initialOptions, onEnd }) {
initialOptions.metafile = true;
onEnd(async (result) => {
const buildArtifacts = result.metafile ? Object.keys(result.metafile.outputs) : [];
await upload(buildArtifacts);
});
},
},
};
}
function esbuildBundleSizeOptimizationsPlugin(
replacementValues: SentrySDKBuildFlags
): UnpluginOptions {
return {
name: "sentry-esbuild-bundle-size-optimizations-plugin",
esbuild: {
setup({ initialOptions }) {
const replacementStringValues: Record<string, string> = {};
Object.entries(replacementValues).forEach(([key, value]) => {
replacementStringValues[key] = JSON.stringify(value);
});
initialOptions.define = { ...initialOptions.define, ...replacementStringValues };
},
},
};
}
const sentryUnplugin = sentryUnpluginFactory({
releaseInjectionPlugin: esbuildReleaseInjectionPlugin,
debugIdInjectionPlugin: esbuildDebugIdInjectionPlugin,
moduleMetadataInjectionPlugin: esbuildModuleMetadataInjectionPlugin,
debugIdUploadPlugin: esbuildDebugIdUploadPlugin,
bundleSizeOptimizationsPlugin: esbuildBundleSizeOptimizationsPlugin,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const sentryEsbuildPlugin: (options?: Options) => any = sentryUnplugin.esbuild;
export type { Options as SentryEsbuildPluginOptions } from "@sentry/bundler-plugin-core";
export { sentryCliBinaryExists } from "@sentry/bundler-plugin-core";