Skip to content

Commit bae2d11

Browse files
committed
feat(core): Accept and await a promise in sourcemaps.filesToDeleteAfterUpload
1 parent 7e71b59 commit bae2d11

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

packages/bundler-plugin-core/src/plugins/sourcemap-deletion.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface FileDeletionPlugin {
1111
waitUntilSourcemapFileDependenciesAreFreed: () => Promise<void>;
1212
sentryScope: Scope;
1313
sentryClient: Client;
14-
filesToDeleteAfterUpload: string | string[] | undefined;
14+
filesToDeleteAfterUpload: string | string[] | Promise<string | string[]> | undefined;
1515
logger: Logger;
1616
}
1717

@@ -28,7 +28,9 @@ export function fileDeletionPlugin({
2828
async writeBundle() {
2929
try {
3030
if (filesToDeleteAfterUpload !== undefined) {
31-
const filePathsToDelete = await glob(filesToDeleteAfterUpload, {
31+
const filesToDelete = await filesToDeleteAfterUpload;
32+
33+
const filePathsToDelete = await glob(filesToDelete, {
3234
absolute: true,
3335
nodir: true,
3436
});

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ export interface Options {
132132
*
133133
* The globbing patterns follow the implementation of the `glob` package. (https://www.npmjs.com/package/glob)
134134
*
135+
* Note: If you pass in a promise that resolves to a string or array, the plugin will await the promise and use
136+
* the resolved value globs. This is useful if you need to dynamically determine the files to delete. Some
137+
* higher-level Sentry SDKs or options use this feature (e.g. SvelteKit).
138+
*
135139
* Use the `debug` option to print information about which files end up being deleted.
136140
*/
137-
filesToDeleteAfterUpload?: string | string[];
141+
filesToDeleteAfterUpload?: string | string[] | Promise<string | string[]>;
138142
};
139143

140144
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable jest/no-standalone-expect */
2+
/* eslint-disable jest/expect-expect */
3+
import path from "path";
4+
import fs from "fs";
5+
import { testIfNodeMajorVersionIsLessThan18 } from "../../utils/testIf";
6+
7+
describe("Deletes files with `filesToDeleteAfterUpload` set to a promise", () => {
8+
testIfNodeMajorVersionIsLessThan18("webpack 4 bundle", () => {
9+
expect(fs.existsSync(path.join(__dirname, "out", "webpack4", "bundle.js.map"))).toBe(false);
10+
});
11+
12+
test("webpack 5 bundle", () => {
13+
expect(fs.existsSync(path.join(__dirname, "out", "webpack5", "bundle.js.map"))).toBe(false);
14+
});
15+
16+
test("esbuild bundle", () => {
17+
expect(fs.existsSync(path.join(__dirname, "out", "esbuild", "bundle.js.map"))).toBe(false);
18+
});
19+
20+
test("rollup bundle", () => {
21+
expect(fs.existsSync(path.join(__dirname, "out", "rollup", "bundle.js.map"))).toBe(false);
22+
});
23+
24+
test("vite bundle", () => {
25+
expect(fs.existsSync(path.join(__dirname, "out", "vite", "bundle.js.map"))).toBe(false);
26+
});
27+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-console
2+
console.log("whatever");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as path from "path";
2+
import { createCjsBundles } from "../../utils/create-cjs-bundles";
3+
4+
const outputDir = path.resolve(__dirname, "out");
5+
6+
["webpack4", "webpack5", "esbuild", "rollup", "vite"].forEach((bundler) => {
7+
const fileDeletionGlobPromise = new Promise<string[]>((resolve) => {
8+
setTimeout(() => {
9+
resolve([path.join(__dirname, "out", bundler, "bundle.js.map")]);
10+
}, 1000);
11+
});
12+
13+
createCjsBundles(
14+
{
15+
bundle: path.resolve(__dirname, "input", "bundle.js"),
16+
},
17+
outputDir,
18+
{
19+
sourcemaps: {
20+
filesToDeleteAfterUpload: fileDeletionGlobPromise,
21+
},
22+
},
23+
[bundler]
24+
);
25+
});

0 commit comments

Comments
 (0)