Skip to content

Commit

Permalink
feat(core): Accept and await a promise in `sourcemaps.filesToDeleteAf…
Browse files Browse the repository at this point in the history
…terUpload` (#677)

Widen the accepted type for `filesToDeleteAfterSourcemaps` to allow us (as well as users) to pass in a `Promise<string | string[]>` to do so. This promise can resolve whenever we know what to set and we await the promise before calling `glob` to get all file paths to delete.
  • Loading branch information
Lms24 authored Feb 18, 2025
1 parent 7e71b59 commit 4255012
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
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]
);
});

0 comments on commit 4255012

Please sign in to comment.