Skip to content

Commit 1f994fc

Browse files
committed
feat: adds configurable dev environments for source map upload plugins
1 parent 54c1fdb commit 1f994fc

File tree

11 files changed

+63
-18
lines changed

11 files changed

+63
-18
lines changed

packages/esbuild-plugin/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ These plugin parameters correspond to the Honeybadger [Source Map Upload API](ht
6868
<dd>The name of the user that triggered this deploy, for example, "Jane"</dd>
6969
</dl>
7070
</dd>
71+
72+
<dt><code>developmentEnvironments</code> (optional &mdash; default: ["dev", "development", "test"])</dt>
73+
<dd>Used to decide whether source maps should be uploaded or not.</dd>
7174
</dl>
7275

7376
### esbuild.config.js

packages/esbuild-plugin/src/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class HoneybadgerSourceMapPlugin {
2424
return {
2525
name: PLUGIN_NAME,
2626
setup(build: PluginBuild) {
27-
if (self.isNonProdEnv()) {
27+
if (self.isDevEnv(self.options.developmentEnvironments)) {
2828
return
2929
}
3030

@@ -95,8 +95,12 @@ class HoneybadgerSourceMapPlugin {
9595
return assets
9696
}
9797

98-
private isNonProdEnv(): boolean {
99-
return !!process.env.NODE_ENV && process.env.NODE_ENV !== 'production'
98+
private isDevEnv(devEnvironments: string[]): boolean {
99+
if (!process.env.NODE_ENV || process.env.NODE_ENV === '') {
100+
return false
101+
}
102+
103+
return devEnvironments.includes(process.env.NODE_ENV)
100104
}
101105

102106
private handleError(error: Error): Message {

packages/plugin-core/src/options.ts

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const DEFAULT_DEPLOY = false
1111
export const DEFAULT_DEPLOY_ENDPOINT = 'https://api.honeybadger.io/v1/deploys'
1212
export const DEFAULT_IGNORE_PATHS = []
1313
export const DEFAULT_IGNORE_ERRORS = false
14+
export const DEFAULT_DEVELOPMENT_ENVIRONMENTS = ['dev', 'development', 'test'];
1415

1516
const required = [
1617
'apiKey',
@@ -27,6 +28,7 @@ const defaultOptions = {
2728
ignorePaths: DEFAULT_IGNORE_PATHS,
2829
ignoreErrors: DEFAULT_IGNORE_ERRORS,
2930
workerCount: DEFAULT_WORKER_COUNT,
31+
developmentEnvironments: DEFAULT_DEVELOPMENT_ENVIRONMENTS
3032
}
3133

3234
export function cleanOptions(
@@ -44,6 +46,11 @@ export function cleanOptions(
4446
throw new Error('ignorePaths must be an array')
4547
}
4648

49+
// Validate developmentEnvironments
50+
if (options.developmentEnvironments && !Array.isArray(options.developmentEnvironments)) {
51+
throw new Error('developmentEnvironments must be an array')
52+
}
53+
4754
// Don't allow excessive retries
4855
if (options.retries && options.retries > MAX_RETRIES) {
4956
if (!options.silent) {

packages/plugin-core/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type HbPluginOptions = {
1111
ignorePaths: Array<string>;
1212
ignoreErrors: boolean;
1313
workerCount: number;
14+
developmentEnvironments: Array<string>;
1415
}
1516

1617
// Options passed in by a user

packages/rollup-plugin/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ These plugin parameters correspond to the Honeybadger [Source Map Upload API](ht
7070
<dd>The name of the user that triggered this deploy, for example, "Jane"</dd>
7171
</dl>
7272
</dd>
73+
74+
<dt><code>developmentEnvironments</code> (optional &mdash; default: ["dev", "development", "test"])</dt>
75+
<dd>Used to decide whether source maps should be uploaded or not.</dd>
7376
</dl>
7477

7578
### rollup.config.js

packages/rollup-plugin/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extractSourcemapDataFromBundle, isNonProdEnv } from './rollupUtils'
1+
import { extractSourcemapDataFromBundle, isDevEnv } from './rollupUtils'
22
import { sendDeployNotification, uploadSourcemaps, cleanOptions, Types } from '@honeybadger-io/plugin-core'
33
import type { OutputBundle, Plugin, NormalizedOutputOptions } from 'rollup'
44

@@ -13,7 +13,7 @@ export default function honeybadgerRollupPlugin(
1313
outputOptions: NormalizedOutputOptions,
1414
bundle: OutputBundle
1515
) => {
16-
if (isNonProdEnv()) {
16+
if (isDevEnv(hbOptions.developmentEnvironments)) {
1717
if (!hbOptions.silent) {
1818
console.info('Honeybadger will not upload sourcemaps in non-production environment.')
1919
}

packages/rollup-plugin/src/rollupUtils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ function formatSourcemapData(
5555
* In Rollup without Vite, it may or may not be available,
5656
* so if it's missing we'll assume prod
5757
*/
58-
export function isNonProdEnv(): boolean {
59-
return !!process.env.NODE_ENV && process.env.NODE_ENV !== 'production'
58+
export function isDevEnv(devEnvironments: string[]): boolean {
59+
if (!process.env.NODE_ENV || process.env.NODE_ENV === '') {
60+
return false
61+
}
62+
63+
return devEnvironments.includes(process.env.NODE_ENV)
6064
}

packages/rollup-plugin/test/rollupUtils.test.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai'
2-
import { extractSourcemapDataFromBundle, isNonProdEnv } from '../src/rollupUtils';
2+
import { extractSourcemapDataFromBundle, isDevEnv } from '../src/rollupUtils';
33
import bundle from './fixtures/bundle'
44
import path from 'node:path'
55
import { NormalizedOutputOptions } from 'rollup';
@@ -91,29 +91,40 @@ describe('extractSourcemapDataFromBundle', () => {
9191
})
9292
})
9393

94-
describe('isNonProdEnv', () => {
94+
describe('isDevEnv', () => {
95+
const developmentEnvironments = ['development', 'test', 'staging']
9596
let restore
9697

9798
beforeEach(() => {
9899
restore = process.env.NODE_ENV
99100
})
100101

101102
afterEach(() => {
103+
// @ts-expect-error
102104
process.env.NODE_ENV = restore
103105
})
104106

105107
it('returns true if NODE_ENV is non-prod', () => {
108+
// @ts-expect-error
106109
process.env.NODE_ENV = 'development'
107-
expect(isNonProdEnv()).to.equal(true)
110+
expect(isDevEnv(developmentEnvironments)).to.equal(true)
111+
})
112+
113+
it('returns false if NODE_ENV is non-prod but not in developmentEnvironments array', () => {
114+
// @ts-expect-error
115+
process.env.NODE_ENV = 'staging'
116+
expect(isDevEnv(developmentEnvironments)).to.equal(false)
108117
})
109118

110119
it('returns false if NODE_ENV is missing', () => {
120+
// @ts-expect-error
111121
delete process.env.NODE_ENV
112-
expect(isNonProdEnv()).to.equal(false)
122+
expect(isDevEnv(developmentEnvironments)).to.equal(false)
113123
})
114124

115125
it('returns false if NODE_ENV is prod', () => {
126+
// @ts-expect-error
116127
process.env.NODE_ENV = 'production'
117-
expect(isNonProdEnv()).to.equal(false)
128+
expect(isDevEnv(developmentEnvironments)).to.equal(false)
118129
})
119130
})

packages/rollup-plugin/tsconfig.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
"extends": "../../tsconfig.base.json",
33
"include": [
44
"src/**/*.ts"
5-
],
5+
],
66
"compilerOptions": {
7-
"rootDir": "./",
7+
"rootDir": "./",
88
"allowSyntheticDefaultImports": true
99
},
10-
}
10+
"references": [
11+
{
12+
"path": "../plugin-core"
13+
}
14+
]
15+
}

packages/webpack/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ These plugin parameters correspond to the Honeybadger [Source Map Upload API](ht
7777
<dd>The name of the user that triggered this deploy, for example, "Jane"</dd>
7878
</dl>
7979
</dd>
80+
81+
<dt><code>developmentEnvironments</code> (optional &mdash; default: ["dev", "development", "test"])</dt>
82+
<dd>Source maps upload will be skipped when the environment matches any of the values in this array or the webpack dev server is running.</dd>
8083
</dl>
8184

8285
### Vanilla webpack.config.js

packages/webpack/src/HoneybadgerSourceMapPlugin.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class HoneybadgerSourceMapPlugin {
1919
}
2020

2121
async afterEmit (compilation) {
22-
if (this.isDevServerRunning()) {
22+
if (this.isDevEnv(this.options.developmentEnvironments)) {
2323
if (!this.options.silent) {
2424
console.info('\nHoneybadgerSourceMapPlugin will not upload source maps because webpack-dev-server is running.')
2525
}
@@ -41,8 +41,12 @@ class HoneybadgerSourceMapPlugin {
4141
}
4242
}
4343

44-
isDevServerRunning () {
45-
return process.env.WEBPACK_DEV_SERVER === 'true'
44+
isDevEnv (devEnvironments) {
45+
if (process.env.WEBPACK_DEV_SERVER === 'true') {
46+
return true
47+
}
48+
49+
return !!(devEnvironments && devEnvironments.includes(process.env.NODE_ENV));
4650
}
4751

4852
apply (compiler) {

0 commit comments

Comments
 (0)