Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit 093e61b

Browse files
authored
feat: Inject global.SENTRY_RELEASE.id into source
2 parents b61c703 + 0ae873c commit 093e61b

File tree

7 files changed

+79
-25
lines changed

7 files changed

+79
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const config = {
5555

5656
#### Options
5757

58-
* `release [optional]` - unique name of a release, can be either a `string` or a `function` which will expose you a compilation hash as it's first argument, which is 20-char long string, unique for a given codebase, defaults to `sentry-cli releases propose-version` command which should always return the correct version
58+
* `release [optional]` - unique name of a release, must be a `string`, should uniquely identify your release, defaults to `sentry-cli releases propose-version` command which should always return the correct version
5959
* `include [required]` - `string` or `array`, one or more paths that Sentry CLI should scan recursively for sources. It will upload all `.map` files and match associated `.js` files
6060
* `ignoreFile [optional]` - `string`, path to a file containing list of files/directories to ignore. Can point to `.gitignore` or anything with same format
6161
* `ignore [optional]` - `string` or `array`, one or more paths to ignore during upload. Overrides entries in `ignoreFile` file. If neither `ignoreFile` or `ignore` are present, defaults to `['node_modules']`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"node": ">= 6.0.0"
1818
},
1919
"dependencies": {
20-
"@sentry/cli": "^1.28.0"
20+
"@sentry/cli": "^1.28.1"
2121
},
2222
"devDependencies": {
2323
"codecov": "^3.0.0",

src/__tests__/index.spec.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const newMock = jest.fn(() => Promise.resolve());
22
const uploadSourceMapsMock = jest.fn(() => Promise.resolve());
33
const finalizeMock = jest.fn(() => Promise.resolve());
4+
const proposeVersionMock = jest.fn(() => Promise.resolve());
45
const SentryCliMock = jest.fn(configFile => ({
56
releases: {
67
new: newMock,
78
uploadSourceMaps: uploadSourceMapsMock,
8-
finalize: finalizeMock
9+
finalize: finalizeMock,
10+
proposeVersion: proposeVersionMock
911
}
1012
}));
1113

@@ -74,7 +76,7 @@ describe('.apply', () => {
7476

7577
test('should create exactly one instance of SentryCli with `configFile` if passed', () => {
7678
const sentryCliPlugin = new SentryCliPlugin({
77-
release: 42,
79+
release: '42',
7880
include: 'src',
7981
configFile: './some/file'
8082
});
@@ -125,9 +127,7 @@ describe('.apply callback function', () => {
125127
test('should evaluate `release` option with compilation hash if its passed as a function', done => {
126128
expect.assertions(2);
127129
const sentryCliPlugin = new SentryCliPlugin({
128-
release(hash) {
129-
return hash + 'Evaluated';
130-
},
130+
release: 'someHashEvaluated',
131131
include: 'src'
132132
});
133133
sentryCliPlugin.apply(compiler);
@@ -144,7 +144,7 @@ describe('.apply callback function', () => {
144144

145145
beforeEach(() => {
146146
sentryCliPlugin = new SentryCliPlugin({
147-
release: 42,
147+
release: '42',
148148
include: 'src'
149149
});
150150
});
@@ -154,14 +154,14 @@ describe('.apply callback function', () => {
154154
sentryCliPlugin.apply(compiler);
155155

156156
setImmediate(() => {
157-
expect(newMock).toBeCalledWith(42);
158-
expect(uploadSourceMapsMock).toBeCalledWith(42, {
157+
expect(newMock).toBeCalledWith('42');
158+
expect(uploadSourceMapsMock).toBeCalledWith('42', {
159159
ignore: undefined,
160-
release: 42,
160+
release: '42',
161161
include: ['src'],
162162
rewrite: true
163163
});
164-
expect(finalizeMock).toBeCalledWith(42);
164+
expect(finalizeMock).toBeCalledWith('42');
165165
expect(compilationDoneCallback).toBeCalled();
166166
done();
167167
});

src/index.js

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var SentryCli = require('@sentry/cli');
2+
var path = require('path');
3+
var fs = require('fs');
24

35
function SentryCliPlugin(options = {}) {
46
// By default we want that rewrite is true
@@ -10,12 +12,62 @@ function SentryCliPlugin(options = {}) {
1012
options.ignore && (Array.isArray(options.ignore) ? options.ignore : [options.ignore]);
1113
}
1214

15+
function injectEntry(originalEntry, newEntry) {
16+
if (Array.isArray(originalEntry)) {
17+
return [newEntry].concat(originalEntry);
18+
}
19+
20+
if (originalEntry !== null && typeof originalEntry === 'object') {
21+
var nextEntries = {};
22+
Object.keys(originalEntry).forEach(function(key) {
23+
nextEntries[key] = injectEntry(originalEntry[key], newEntry);
24+
});
25+
return nextEntries;
26+
}
27+
28+
if (typeof originalEntry === 'string') {
29+
return [newEntry, originalEntry];
30+
}
31+
32+
return newEntry;
33+
}
34+
35+
function injectRelease(compiler, versionPromise) {
36+
if (typeof compiler.options === 'undefined') {
37+
// Gatekeeper because we are not running in webpack env
38+
// probably just tests
39+
return;
40+
}
41+
compiler.options.entry = injectEntry(
42+
compiler.options.entry,
43+
path.join(__dirname, 'sentry-webpack.module.js')
44+
);
45+
compiler.options.module = {};
46+
compiler.options.module.rules = [];
47+
compiler.options.module.rules.push({
48+
test: /sentry-webpack\.module\.js$/,
49+
use: [
50+
{
51+
loader: path.resolve(__dirname, 'sentry.loader.js'),
52+
query: {versionPromise}
53+
}
54+
]
55+
});
56+
}
57+
1358
SentryCliPlugin.prototype.apply = function(compiler) {
1459
var sentryCli = new SentryCli(this.options.configFile);
1560
var release = this.options.release;
1661
var include = this.options.include;
1762
var options = this.options;
1863

64+
var versionPromise = Promise.resolve(release);
65+
if (typeof release === 'undefined') {
66+
versionPromise = sentryCli.releases.proposeVersion();
67+
}
68+
69+
injectRelease(compiler, versionPromise);
70+
1971
compiler.plugin('after-emit', function(compilation, cb) {
2072
function handleError(message, cb) {
2173
compilation.errors.push(`Sentry CLI Plugin: ${message}`);
@@ -24,18 +76,9 @@ SentryCliPlugin.prototype.apply = function(compiler) {
2476

2577
if (!include) return handleError('`include` option is required', cb);
2678

27-
if (typeof release === 'function') {
28-
release = release(compilation.hash);
29-
}
30-
31-
var versionPromise = Promise.resolve(release);
32-
if (typeof release === 'undefined') {
33-
versionPromise = sentryCli.releases.proposeVersion();
34-
}
35-
3679
return versionPromise
3780
.then(function(proposedVersion) {
38-
options.release = proposedVersion;
81+
options.release = (proposedVersion + '').trim();
3982
return sentryCli.releases.new(options.release);
4083
})
4184
.then(function() {

src/sentry-webpack.module.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This will be replaced

src/sentry.loader.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function(content, map, meta) {
2+
var versionPromise = this.query.versionPromise;
3+
var callback = this.async();
4+
versionPromise.then(function(version) {
5+
var version = (version + '').trim();
6+
var sentryRelease =
7+
'global.SENTRY_RELEASE={};\nglobal.SENTRY_RELEASE.id="' + version + '";';
8+
callback(null, sentryRelease, map, meta);
9+
});
10+
};

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
esutils "^2.0.2"
1111
js-tokens "^3.0.0"
1212

13-
"@sentry/cli@^1.27.0":
14-
version "1.28.0"
15-
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.28.0.tgz#ef0bb4fd06d4af918f22e4e253e330bd7855cb1d"
13+
"@sentry/cli@^1.28.1":
14+
version "1.28.1"
15+
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.28.1.tgz#42feaa7f5db056dad1ebfbb80af5c01c1f46b82d"
1616
dependencies:
1717
progress "2.0.0"
1818

0 commit comments

Comments
 (0)