Skip to content

feat(core): Accept and await a promise in sourcemaps.filesToDeleteAfterUpload #677

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

Merged
merged 3 commits into from
Feb 18, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface FileDeletionPlugin {
waitUntilSourcemapFileDependenciesAreFreed: () => Promise<void>;
sentryScope: Scope;
sentryClient: Client;
filesToDeleteAfterUpload: string | string[] | undefined;
filesToDeleteAfterUpload: string | string[] | Promise<string | string[] | undefined> | undefined;
logger: Logger;
}

Expand All @@ -27,8 +27,9 @@ export function fileDeletionPlugin({
name: "sentry-file-deletion-plugin",
async writeBundle() {
try {
if (filesToDeleteAfterUpload !== undefined) {
const filePathsToDelete = await glob(filesToDeleteAfterUpload, {
const filesToDelete = await filesToDeleteAfterUpload;
if (filesToDelete !== undefined) {
const filePathsToDelete = await glob(filesToDelete, {
absolute: true,
nodir: true,
});
Expand Down
6 changes: 5 additions & 1 deletion packages/bundler-plugin-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,13 @@ export interface Options {
*
* The globbing patterns follow the implementation of the `glob` package. (https://www.npmjs.com/package/glob)
*
* Note: If you pass in a promise that resolves to a string or array, the plugin will await the promise and use
* the resolved value globs. This is useful if you need to dynamically determine the files to delete. Some
* higher-level Sentry SDKs or options use this feature (e.g. SvelteKit).
*
* Use the `debug` option to print information about which files end up being deleted.
*/
filesToDeleteAfterUpload?: string | string[];
filesToDeleteAfterUpload?: string | string[] | Promise<string | string[] | undefined>;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/dev-utils/src/generate-documentation-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ errorHandler: (err) => {
},
{
name: "filesToDeleteAfterUpload",
type: "string | string[]",
type: "string | string[] | Promise<string | string[]>",
fullDescription:
"A glob or an array of globs that specifies the build artifacts that should be deleted after the artifact upload to Sentry has been completed.\n\nThe globbing patterns follow the implementation of the `glob` package. (https://www.npmjs.com/package/glob)\n\nUse the `debug` option to print information about which files end up being deleted.",
"A glob, an array of globs or a promise resolving a glob or array of globs that specifies the build artifacts that should be deleted after the artifact upload to Sentry has been completed.\n\nThe globbing patterns follow the implementation of the `glob` package. (https://www.npmjs.com/package/glob)\n\nUse the `debug` option to print information about which files end up being deleted.",
},
{
name: "disable",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable jest/no-standalone-expect */
/* eslint-disable jest/expect-expect */
import path from "path";
import fs from "fs";
import { testIfNodeMajorVersionIsLessThan18 } from "../../utils/testIf";

describe("Deletes files with `filesToDeleteAfterUpload` set to a promise", () => {
testIfNodeMajorVersionIsLessThan18("webpack 4 bundle", () => {
expect(fs.existsSync(path.join(__dirname, "out", "webpack4", "bundle.js.map"))).toBe(false);
});

test("webpack 5 bundle", () => {
expect(fs.existsSync(path.join(__dirname, "out", "webpack5", "bundle.js.map"))).toBe(false);
});

test("esbuild bundle", () => {
expect(fs.existsSync(path.join(__dirname, "out", "esbuild", "bundle.js.map"))).toBe(false);
});

test("rollup bundle", () => {
expect(fs.existsSync(path.join(__dirname, "out", "rollup", "bundle.js.map"))).toBe(false);
});

test("vite bundle", () => {
expect(fs.existsSync(path.join(__dirname, "out", "vite", "bundle.js.map"))).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-console
console.log("whatever");
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as path from "path";
import { createCjsBundles } from "../../utils/create-cjs-bundles";

const outputDir = path.resolve(__dirname, "out");

["webpack4", "webpack5", "esbuild", "rollup", "vite"].forEach((bundler) => {
const fileDeletionGlobPromise = new Promise<string[]>((resolve) => {
setTimeout(() => {
resolve([path.join(__dirname, "out", bundler, "bundle.js.map")]);
}, 1000);
});

createCjsBundles(
{
bundle: path.resolve(__dirname, "input", "bundle.js"),
},
outputDir,
{
sourcemaps: {
filesToDeleteAfterUpload: fileDeletionGlobPromise,
},
},
[bundler]
);
});
Loading