From 121446f62143ede4e527c154e6c7b506700f1f08 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 28 Jun 2023 09:46:54 +0200 Subject: [PATCH] feat: Add configuration via `.env.sentry-build-plugin` file (#333) --- packages/bundler-plugin-core/package.json | 1 + packages/bundler-plugin-core/src/index.ts | 26 ++++++++++++++----- packages/esbuild-plugin/README_TEMPLATE.md | 4 +++ .../scripts/run-fixture-setups.ts | 5 +++- packages/playground/.gitignore | 1 + packages/rollup-plugin/README_TEMPLATE.md | 4 +++ packages/vite-plugin/README_TEMPLATE.md | 4 +++ packages/webpack-plugin/README_TEMPLATE.md | 4 +++ yarn.lock | 5 ++++ 9 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/bundler-plugin-core/package.json b/packages/bundler-plugin-core/package.json index 7f49f062..0c295d81 100644 --- a/packages/bundler-plugin-core/package.json +++ b/packages/bundler-plugin-core/package.json @@ -55,6 +55,7 @@ "@sentry/cli": "^2.17.0", "@sentry/node": "7.53.1", "@sentry/utils": "7.53.1", + "dotenv": "^16.3.1", "find-up": "5.0.0", "glob": "9.3.2", "magic-string": "0.27.0", diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index 0132b73e..b4c4db51 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -18,6 +18,7 @@ import { stringToUUID, stripQueryAndHashFromPath, } from "./utils"; +import * as dotenv from "dotenv"; interface SentryUnpluginFactoryOptions { releaseInjectionPlugin: (injectionCode: string) => UnpluginOptions; @@ -58,6 +59,25 @@ export function sentryUnpluginFactory({ debugIdUploadPlugin, }: SentryUnpluginFactoryOptions) { return createUnplugin((userOptions, unpluginMetaContext) => { + const logger = createLogger({ + prefix: `[sentry-${unpluginMetaContext.framework}-plugin]`, + silent: userOptions.silent ?? false, + debug: userOptions.debug ?? false, + }); + + const dotenvResult = dotenv.config({ + path: path.join(process.cwd(), ".env.sentry-build-plugin"), + }); + + // Ignore "file not found" errors but throw all others + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore Accessing `code` on error should be safe + if (dotenvResult.error && dotenvResult.error.code !== "ENOENT") { + throw dotenvResult.error; + } else if (dotenvResult.parsed) { + logger.info('Using environment variables configured in ".env.sentry-build-plugin".'); + } + const options = normalizeUserOptions(userOptions); if (unpluginMetaContext.watchMode || options.disable) { @@ -86,12 +106,6 @@ export function sentryUnpluginFactory({ } }); - const logger = createLogger({ - prefix: `[sentry-${unpluginMetaContext.framework}-plugin]`, - silent: options.silent, - debug: options.debug, - }); - // Set the User-Agent that Sentry CLI will use when interacting with Sentry process.env[ "SENTRY_PIPELINE" diff --git a/packages/esbuild-plugin/README_TEMPLATE.md b/packages/esbuild-plugin/README_TEMPLATE.md index 04108466..67273dcf 100644 --- a/packages/esbuild-plugin/README_TEMPLATE.md +++ b/packages/esbuild-plugin/README_TEMPLATE.md @@ -56,6 +56,10 @@ require("esbuild").build({ #OPTIONS_SECTION_INSERT# +### Configuration File + +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. + ## More information - [Sentry Documentation](https://docs.sentry.io/quickstart/) diff --git a/packages/integration-tests/scripts/run-fixture-setups.ts b/packages/integration-tests/scripts/run-fixture-setups.ts index 7bd4ec6f..07b0678f 100644 --- a/packages/integration-tests/scripts/run-fixture-setups.ts +++ b/packages/integration-tests/scripts/run-fixture-setups.ts @@ -1,5 +1,6 @@ import fs from "fs"; import path from "path"; +import childProcess from "child_process"; const fixturePaths = fs .readdirSync(path.join(__dirname, "..", "fixtures")) @@ -8,6 +9,8 @@ const fixturePaths = fs fixturePaths.forEach((fixturePath) => { const setupScriptPath = path.join(fixturePath, "setup.ts"); if (fs.existsSync(setupScriptPath)) { - require(setupScriptPath); + childProcess.execSync(`yarn ts-node --transpileOnly ${setupScriptPath}`, { + stdio: "inherit", + }); } }); diff --git a/packages/playground/.gitignore b/packages/playground/.gitignore index 01984667..4e8ee24b 100644 --- a/packages/playground/.gitignore +++ b/packages/playground/.gitignore @@ -1,3 +1,4 @@ out/ .env +.env.sentry-build-plugin scripts/request-logger-logs/*.txt diff --git a/packages/rollup-plugin/README_TEMPLATE.md b/packages/rollup-plugin/README_TEMPLATE.md index dbece44e..dd41bc30 100644 --- a/packages/rollup-plugin/README_TEMPLATE.md +++ b/packages/rollup-plugin/README_TEMPLATE.md @@ -58,6 +58,10 @@ export default { #OPTIONS_SECTION_INSERT# +### Configuration File + +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. + ## More information - [Sentry Documentation](https://docs.sentry.io/quickstart/) diff --git a/packages/vite-plugin/README_TEMPLATE.md b/packages/vite-plugin/README_TEMPLATE.md index 7ae4bfff..1110e071 100644 --- a/packages/vite-plugin/README_TEMPLATE.md +++ b/packages/vite-plugin/README_TEMPLATE.md @@ -63,6 +63,10 @@ export default defineConfig({ #OPTIONS_SECTION_INSERT# +### Configuration File + +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. + ## More information - [Sentry Documentation](https://docs.sentry.io/quickstart/) diff --git a/packages/webpack-plugin/README_TEMPLATE.md b/packages/webpack-plugin/README_TEMPLATE.md index 07f4b55e..5465b1ae 100644 --- a/packages/webpack-plugin/README_TEMPLATE.md +++ b/packages/webpack-plugin/README_TEMPLATE.md @@ -57,6 +57,10 @@ module.exports = { #OPTIONS_SECTION_INSERT# +### Configuration File + +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. + ## More information - [Sentry Documentation](https://docs.sentry.io/quickstart/) diff --git a/yarn.lock b/yarn.lock index 9ce828f8..2295746f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5080,6 +5080,11 @@ dot-prop@^5.1.0: dependencies: is-obj "^2.0.0" +dotenv@^16.3.1: + version "16.3.1" + resolved "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e" + integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ== + dotenv@~10.0.0: version "10.0.0" resolved "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"