Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.
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
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ export interface SentryCliPluginOptions {
*/
finalize?: boolean;

/**
* Determines whether plugin should be applied not more than once during whole webpack run.
* Useful when the process is performing multiple builds using the same config.
* Defaults to `false`.
*/
runOnce?: boolean;

/**
* Attempts a dry run (useful for dev environments).
*/
Expand Down
32 changes: 26 additions & 6 deletions src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ const mockCli = {

const SentryCliMock = jest.fn((configFile, options) => mockCli);
const SentryCli = jest.mock('@sentry/cli', () => SentryCliMock);
const SentryCliPlugin = require('../..');

afterEach(() => {
jest.clearAllMocks();
});
let SentryCliPlugin = require('../..');

const defaults = {
debug: false,
finalize: true,
rewrite: true,
};

beforeEach(() => {
jest.clearAllMocks();
});

describe('constructor', () => {
test('uses defaults without options', () => {
const sentryCliPlugin = new SentryCliPlugin();
Expand Down Expand Up @@ -287,6 +286,27 @@ describe('afterEmitHook', () => {
done();
});
});

test('does not skip the release if `runOnce` option is not set and its called more than once', () => {
const sentryCliPlugin = new SentryCliPlugin();
sentryCliPlugin.apply(compiler);
expect(compiler.hooks.afterEmit.tapAsync).toHaveBeenCalledTimes(1);
sentryCliPlugin.apply(compiler);
expect(compiler.hooks.afterEmit.tapAsync).toHaveBeenCalledTimes(2);
});

test('skips the release if `runOnce` option is set and its called more than once', () => {
// Reset the state of a module to verify `runOnce` behavior
jest.resetModules();
SentryCliPlugin = require('../..');
const sentryCliPlugin = new SentryCliPlugin({
runOnce: true,
});
sentryCliPlugin.apply(compiler);
expect(compiler.hooks.afterEmit.tapAsync).toHaveBeenCalledTimes(1);
sentryCliPlugin.apply(compiler);
expect(compiler.hooks.afterEmit.tapAsync).toHaveBeenCalledTimes(1);
});
});

describe('module rule overrides', () => {
Expand Down
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function attachAfterEmitHook(compiler, callback) {
class SentryCliPlugin {
constructor(options = {}) {
const defaults = {
debug: false,
finalize: true,
rewrite: true,
};
Expand Down Expand Up @@ -452,6 +451,21 @@ class SentryCliPlugin {

/** Webpack lifecycle hook to update compiler options. */
apply(compiler) {
/**
* Determines whether plugin should be applied not more than once during whole webpack run.
* Useful when the process is performing multiple builds using the same config.
* It cannot be stored on the instance, as every run is creating a new one.
*/
if (this.options.runOnce && module.alreadyRun) {
if (this.options.debug) {
this.outputDebug(
'`runOnce` option set and plugin already ran. Skipping release.'
);
}
return;
}
module.alreadyRun = true;

const compilerOptions = compiler.options || {};
ensure(compilerOptions, 'module', Object);

Expand Down