forked from dpwilhelmsen/new-relic-source-map-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (56 loc) · 2.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"use strict";
const uploadSourceMap = require('./src/uploadSourceMap');
const staticAssetUrlBuilder = require('./src/staticAssetUrlBuilder');
const enforceExists = require('./src/enforceExists');
class NewRelicPlugin {
constructor(options) {
if (options.noop) {
this.apply = () => {};
return;
}
this.applicationId = enforceExists(options, 'applicationId');
this.nrAdminKey = enforceExists(options, 'nrAdminKey');
this.staticAssetUrl = enforceExists(options, 'staticAssetUrl');
this.staticAssetUrlBuilder = options.staticAssetUrlBuilder || staticAssetUrlBuilder;
this.extensionRegex = options.extensionRegex || /\.js(\?.+)?/;
this.releaseName = options.releaseName || null;
this.releaseId = options.releaseId || null;
this.errorCallback = options.errorCallback || this._getDefaultErrorCallback();
}
apply(compiler) {
return compiler.hooks.done.tap('new-relic-source-map-webpack-plugin', stats => {
return Promise.all(
Object.keys(stats.compilation.assets)
.filter(item => this.extensionRegex.test(item))
.map(uploadSourceMap({
assets: stats.compilation.assets,
staticAssetUrlBuilder: this.staticAssetUrlBuilder,
url: this.staticAssetUrl,
publicPath: stats.compilation.outputOptions.publicPath,
nrAdminKey: this.nrAdminKey,
applicationId: this.applicationId,
releaseName: this.releaseName,
releaseId: this.releaseId,
stats
}))
).then(values => values.filter(value => typeof value !== 'undefined')
).then(values => {
values.length === 0 &&
this.errorCallback(
'No sourcemaps were found. Check if sourcemaps are enabled: https://webpack.js.org/configuration/devtool/'
);
return values;
}).then((values) => {
values.forEach(v => console.log(`sourceMap for ${v} uploaded to newrelic`));
}).catch((err) => {
this.errorCallback(err);
});
});
}
_getDefaultErrorCallback() {
return (err) => {
console.warn(`New Relic sourcemap upload error: ${err}`);
}
}
};
module.exports = NewRelicPlugin;