forked from dpwilhelmsen/new-relic-source-map-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
79 lines (73 loc) · 3.15 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'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.apiKey = enforceExists(options, 'apiKey');
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) {
compiler.hooks.done.tapPromise('new-relic-source-map-webpack-plugin', stats => {
const chunks = Array.from(stats.compilation.chunks);
const assets = [];
chunks
.map(chunk => [...Array.from(chunk.files), ...Array.from(chunk.auxiliaryFiles)])
.map(files => {
const mapRegex = /\.map(\?|$)/;
const fileName = files.find(file => this.extensionRegex.test(file));
const mapName = files.find(
file =>
this.extensionRegex.test(file.split('.map')[0]) && mapRegex.test(file)
);
if (fileName && mapName) {
assets.push({ fileName, mapName });
}
});
if (assets.length === 0) {
this.errorCallback(
'No sourcemaps were found. Check if sourcemaps are enabled: https://webpack.js.org/configuration/devtool/'
);
return Promise.resolve();
}
return Promise.all(
assets.map(
uploadSourceMap({
staticAssetUrlBuilder: this.staticAssetUrlBuilder,
url: this.staticAssetUrl,
publicPath: stats.compilation.outputOptions.publicPath,
outputPath: stats.compilation.outputOptions.path,
apiKey: this.apiKey,
applicationId: this.applicationId,
releaseName: this.releaseName,
releaseId: this.releaseId,
})
)
)
.then(values => {
// eslint-disable-next-line no-console
values.forEach(v => console.log(`sourceMap for ${v} uploaded to newrelic`));
})
.catch(err => {
this.errorCallback(err);
});
});
}
_getDefaultErrorCallback() {
return err => {
// eslint-disable-next-line no-console
console.warn(`New Relic sourcemap upload error: ${err}`);
};
}
}
module.exports = NewRelicPlugin;