Skip to content

Commit 121446f

Browse files
author
Luca Forstner
authored
feat: Add configuration via .env.sentry-build-plugin file (#333)
1 parent e5a1a36 commit 121446f

File tree

9 files changed

+47
-7
lines changed

9 files changed

+47
-7
lines changed

packages/bundler-plugin-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@sentry/cli": "^2.17.0",
5656
"@sentry/node": "7.53.1",
5757
"@sentry/utils": "7.53.1",
58+
"dotenv": "^16.3.1",
5859
"find-up": "5.0.0",
5960
"glob": "9.3.2",
6061
"magic-string": "0.27.0",

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
stringToUUID,
1919
stripQueryAndHashFromPath,
2020
} from "./utils";
21+
import * as dotenv from "dotenv";
2122

2223
interface SentryUnpluginFactoryOptions {
2324
releaseInjectionPlugin: (injectionCode: string) => UnpluginOptions;
@@ -58,6 +59,25 @@ export function sentryUnpluginFactory({
5859
debugIdUploadPlugin,
5960
}: SentryUnpluginFactoryOptions) {
6061
return createUnplugin<Options, true>((userOptions, unpluginMetaContext) => {
62+
const logger = createLogger({
63+
prefix: `[sentry-${unpluginMetaContext.framework}-plugin]`,
64+
silent: userOptions.silent ?? false,
65+
debug: userOptions.debug ?? false,
66+
});
67+
68+
const dotenvResult = dotenv.config({
69+
path: path.join(process.cwd(), ".env.sentry-build-plugin"),
70+
});
71+
72+
// Ignore "file not found" errors but throw all others
73+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
74+
// @ts-ignore Accessing `code` on error should be safe
75+
if (dotenvResult.error && dotenvResult.error.code !== "ENOENT") {
76+
throw dotenvResult.error;
77+
} else if (dotenvResult.parsed) {
78+
logger.info('Using environment variables configured in ".env.sentry-build-plugin".');
79+
}
80+
6181
const options = normalizeUserOptions(userOptions);
6282

6383
if (unpluginMetaContext.watchMode || options.disable) {
@@ -86,12 +106,6 @@ export function sentryUnpluginFactory({
86106
}
87107
});
88108

89-
const logger = createLogger({
90-
prefix: `[sentry-${unpluginMetaContext.framework}-plugin]`,
91-
silent: options.silent,
92-
debug: options.debug,
93-
});
94-
95109
// Set the User-Agent that Sentry CLI will use when interacting with Sentry
96110
process.env[
97111
"SENTRY_PIPELINE"

packages/esbuild-plugin/README_TEMPLATE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ require("esbuild").build({
5656

5757
#OPTIONS_SECTION_INSERT#
5858

59+
### Configuration File
60+
61+
As an additional configuration method, the Sentry esbuild plugin will pick up environment variables configured inside a `.env.sentry-build-plugin` file located in the current working directory when building your app.
62+
5963
## More information
6064

6165
- [Sentry Documentation](https://docs.sentry.io/quickstart/)
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "fs";
22
import path from "path";
3+
import childProcess from "child_process";
34

45
const fixturePaths = fs
56
.readdirSync(path.join(__dirname, "..", "fixtures"))
@@ -8,6 +9,8 @@ const fixturePaths = fs
89
fixturePaths.forEach((fixturePath) => {
910
const setupScriptPath = path.join(fixturePath, "setup.ts");
1011
if (fs.existsSync(setupScriptPath)) {
11-
require(setupScriptPath);
12+
childProcess.execSync(`yarn ts-node --transpileOnly ${setupScriptPath}`, {
13+
stdio: "inherit",
14+
});
1215
}
1316
});

packages/playground/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
out/
22
.env
3+
.env.sentry-build-plugin
34
scripts/request-logger-logs/*.txt

packages/rollup-plugin/README_TEMPLATE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export default {
5858

5959
#OPTIONS_SECTION_INSERT#
6060

61+
### Configuration File
62+
63+
As an additional configuration method, the Sentry Rollup plugin will pick up environment variables configured inside a `.env.sentry-build-plugin` file located in the current working directory when building your app.
64+
6165
## More information
6266

6367
- [Sentry Documentation](https://docs.sentry.io/quickstart/)

packages/vite-plugin/README_TEMPLATE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export default defineConfig({
6363

6464
#OPTIONS_SECTION_INSERT#
6565

66+
### Configuration File
67+
68+
As an additional configuration method, the Sentry Vite plugin will pick up environment variables configured inside a `.env.sentry-build-plugin` file located in the current working directory when building your app.
69+
6670
## More information
6771

6872
- [Sentry Documentation](https://docs.sentry.io/quickstart/)

packages/webpack-plugin/README_TEMPLATE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ module.exports = {
5757

5858
#OPTIONS_SECTION_INSERT#
5959

60+
### Configuration File
61+
62+
As an additional configuration method, the Sentry Webpack plugin will pick up environment variables configured inside a `.env.sentry-build-plugin` file located in the current working directory when building your app.
63+
6064
## More information
6165

6266
- [Sentry Documentation](https://docs.sentry.io/quickstart/)

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5080,6 +5080,11 @@ dot-prop@^5.1.0:
50805080
dependencies:
50815081
is-obj "^2.0.0"
50825082

5083+
dotenv@^16.3.1:
5084+
version "16.3.1"
5085+
resolved "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e"
5086+
integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==
5087+
50835088
dotenv@~10.0.0:
50845089
version "10.0.0"
50855090
resolved "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"

0 commit comments

Comments
 (0)