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

feat: Inject global.SENTRY_RELEASE.id into source #20

Merged
merged 4 commits into from
Jan 12, 2018
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const config = {

#### Options

* `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
* `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
* `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
* `ignoreFile [optional]` - `string`, path to a file containing list of files/directories to ignore. Can point to `.gitignore` or anything with same format
* `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']`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"node": ">= 6.0.0"
},
"dependencies": {
"@sentry/cli": "^1.28.0"
"@sentry/cli": "^1.28.1"
},
"devDependencies": {
"codecov": "^3.0.0",
Expand Down
20 changes: 10 additions & 10 deletions src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const newMock = jest.fn(() => Promise.resolve());
const uploadSourceMapsMock = jest.fn(() => Promise.resolve());
const finalizeMock = jest.fn(() => Promise.resolve());
const proposeVersionMock = jest.fn(() => Promise.resolve());
const SentryCliMock = jest.fn(configFile => ({
releases: {
new: newMock,
uploadSourceMaps: uploadSourceMapsMock,
finalize: finalizeMock
finalize: finalizeMock,
proposeVersion: proposeVersionMock
}
}));

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

test('should create exactly one instance of SentryCli with `configFile` if passed', () => {
const sentryCliPlugin = new SentryCliPlugin({
release: 42,
release: '42',
include: 'src',
configFile: './some/file'
});
Expand Down Expand Up @@ -125,9 +127,7 @@ describe('.apply callback function', () => {
test('should evaluate `release` option with compilation hash if its passed as a function', done => {
expect.assertions(2);
const sentryCliPlugin = new SentryCliPlugin({
release(hash) {
return hash + 'Evaluated';
},
release: 'someHashEvaluated',
include: 'src'
});
sentryCliPlugin.apply(compiler);
Expand All @@ -144,7 +144,7 @@ describe('.apply callback function', () => {

beforeEach(() => {
sentryCliPlugin = new SentryCliPlugin({
release: 42,
release: '42',
include: 'src'
});
});
Expand All @@ -154,14 +154,14 @@ describe('.apply callback function', () => {
sentryCliPlugin.apply(compiler);

setImmediate(() => {
expect(newMock).toBeCalledWith(42);
expect(uploadSourceMapsMock).toBeCalledWith(42, {
expect(newMock).toBeCalledWith('42');
expect(uploadSourceMapsMock).toBeCalledWith('42', {
ignore: undefined,
release: 42,
release: '42',
include: ['src'],
rewrite: true
});
expect(finalizeMock).toBeCalledWith(42);
expect(finalizeMock).toBeCalledWith('42');
expect(compilationDoneCallback).toBeCalled();
done();
});
Expand Down
63 changes: 53 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var SentryCli = require('@sentry/cli');
var path = require('path');
var fs = require('fs');

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

function injectEntry(originalEntry, newEntry) {
if (Array.isArray(originalEntry)) {
return [newEntry].concat(originalEntry);
}

if (originalEntry !== null && typeof originalEntry === 'object') {
var nextEntries = {};
Object.keys(originalEntry).forEach(function(key) {
nextEntries[key] = injectEntry(originalEntry[key], newEntry);
});
return nextEntries;
}

if (typeof originalEntry === 'string') {
return [newEntry, originalEntry];
}

return newEntry;
}

function injectRelease(compiler, versionPromise) {
if (typeof compiler.options === 'undefined') {
// Gatekeeper because we are not running in webpack env
// probably just tests
return;
}
compiler.options.entry = injectEntry(
compiler.options.entry,
path.join(__dirname, 'sentry-webpack.module.js')
);
compiler.options.module = {};
compiler.options.module.rules = [];
compiler.options.module.rules.push({
test: /sentry-webpack\.module\.js$/,
use: [
{
loader: path.resolve(__dirname, 'sentry.loader.js'),
query: {versionPromise}
}
]
});
}

SentryCliPlugin.prototype.apply = function(compiler) {
var sentryCli = new SentryCli(this.options.configFile);
var release = this.options.release;
var include = this.options.include;
var options = this.options;

var versionPromise = Promise.resolve(release);
if (typeof release === 'undefined') {
versionPromise = sentryCli.releases.proposeVersion();
}

injectRelease(compiler, versionPromise);

compiler.plugin('after-emit', function(compilation, cb) {
function handleError(message, cb) {
compilation.errors.push(`Sentry CLI Plugin: ${message}`);
Expand All @@ -24,18 +76,9 @@ SentryCliPlugin.prototype.apply = function(compiler) {

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

if (typeof release === 'function') {
release = release(compilation.hash);
}

var versionPromise = Promise.resolve(release);
if (typeof release === 'undefined') {
versionPromise = sentryCli.releases.proposeVersion();
}

return versionPromise
.then(function(proposedVersion) {
options.release = proposedVersion;
options.release = (proposedVersion + '').trim();
return sentryCli.releases.new(options.release);
})
.then(function() {
Expand Down
1 change: 1 addition & 0 deletions src/sentry-webpack.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This will be replaced
10 changes: 10 additions & 0 deletions src/sentry.loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function(content, map, meta) {
var versionPromise = this.query.versionPromise;
var callback = this.async();
versionPromise.then(function(version) {
var version = (version + '').trim();
var sentryRelease =
'global.SENTRY_RELEASE={};\nglobal.SENTRY_RELEASE.id="' + version + '";';
callback(null, sentryRelease, map, meta);
});
};
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
esutils "^2.0.2"
js-tokens "^3.0.0"

"@sentry/cli@^1.27.0":
version "1.28.0"
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.28.0.tgz#ef0bb4fd06d4af918f22e4e253e330bd7855cb1d"
"@sentry/cli@^1.28.1":
version "1.28.1"
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.28.1.tgz#42feaa7f5db056dad1ebfbb80af5c01c1f46b82d"
dependencies:
progress "2.0.0"

Expand Down