forked from newrelic/newrelic-browser-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulp-build.js
186 lines (158 loc) · 6.03 KB
/
gulp-build.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
var fs = require('fs')
var gulp = require('gulp')
var browserify = require('browserify')
var source = require('vinyl-source-stream')
var buffer = require('vinyl-buffer')
var uglify = require('gulp-uglify')
var file = require('gulp-file')
var sourcemaps = require('gulp-sourcemaps')
var versionify = require('./lib/versionify')
var loaders = require('./loaders')
var preprocessify = require('preprocessify')
var globalRequire = '__nr_require'
var collapser = require('bundle-collapser/plugin')
var cleanDeps = require('./tools/scripts/clean-deps')
module.exports = 'build'
// Exposes some of its own modules using __nr_require, but doesn't look
// anywhere else for modules it can't find.
var loaderPrelude = fs.readFileSync('prelude.js', 'utf8')
// Looks for modules using global __nr_require if it can't find them in its
// own bundle, but doesn't expose its own modules.
var agentPrelude = fs.readFileSync('agent-prelude.js', 'utf8')
var payloadFeatures = loaders.filter(function (elm) { return elm.name === 'full' })[0].features
var allLoaderNames = []
var allPayloadNames = []
var allPluginNames = []
var loaderExports = ['handle', 'ee', 'id', 'gos', 'wrap-function']
loaders.forEach(function (opt) {
var name = opt.name + '-loader'
var minName = name + '.min'
var aggregator = opt.name + '-aggregator'
var minAggregator = aggregator + '.min'
allLoaderNames.push(name, minName)
gulp.task(name, loader(opt.name, opt.features, false, opt.payload))
gulp.task(minName, loader(opt.name, opt.features, true, opt.payload))
if (!opt.payload) return
gulp.task(aggregator, payload(false, opt.features, opt.name))
gulp.task(minAggregator, payload(true, opt.features, opt.name))
allPayloadNames.push(aggregator, minAggregator)
})
gulp.task('nr', payload(false, payloadFeatures))
gulp.task('nr.min', payload(true, payloadFeatures))
allPayloadNames.push('nr', 'nr.min')
// create plugin tasks
fs.readdirSync('plugins')
.map(function(filename) {
return filename.substring(0, filename.lastIndexOf('.'))
})
.forEach(function(pluginName) {
gulp.task(pluginName, plugin(pluginName, false))
gulp.task(pluginName + '.min', plugin(pluginName, true))
allPluginNames.push(pluginName, pluginName + '.min')
})
gulp.task('loader', gulp.parallel(allLoaderNames))
gulp.task('payload', gulp.parallel(allPayloadNames))
gulp.task('plugin', gulp.parallel(allPluginNames))
gulp.task('sizes', measureSizes)
gulp.task('build', gulp.series(gulp.parallel('loader', 'payload', 'plugin'), 'sizes'))
function measureSizes () {
var artifacts = ['nr-loader-spa.min.js', 'nr-loader-full.min.js', 'nr-loader-rum.min.js', 'nr.min.js']
var sizes = artifacts.map(function (artifact) {
return fs.statSync('build/' + artifact).size / 1024.0
})
var csv = artifacts.join(',') + '\n' + sizes.join(',')
return file('artifact-sizes.csv', csv, {src: true})
.pipe(gulp.dest('./build/'))
}
function loader (name, features, min, payloadName) {
return function () {
var filename = 'nr-loader-' + name + (min ? '.min' : '') + '.js'
var bundler = browserify({
externalRequireName: 'window.NREUM || (NREUM={});' + globalRequire,
prelude: loaderPrelude,
transform: [preprocessify(), versionify(min, payloadName)],
plugin: [collapser, cleanDeps],
debug: true
})
bundler.require('loader', {entry: true, expose: 'loader'})
loaderExports.forEach(function (name) { bundler.require(name) })
features.forEach(function (feature) { bundler.add('./feature/' + feature + '/instrument/index.js') })
var bundleStream = bundler.bundle()
.pipe(source(filename))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
if (min) {
bundleStream = bundleStream.pipe(uglify({
mangle: {except: ['UncaughtException', 'nrWrapper']}
}))
}
return bundleStream
.pipe(sourcemaps.write('./', {addComment: false}))
.pipe(gulp.dest('./build/'))
}
}
function payload (min, features, type) {
return function () {
var filename = 'nr' + (type ? '-' + type : '') + (min ? '.min' : '') + '.js'
var bundler = browserify({
externalRequireName: 'window.NREUM || (NREUM={});' + globalRequire,
prelude: agentPrelude,
transform: [preprocessify(), versionify(min)],
plugin: [collapser, cleanDeps],
debug: true
})
features
.map(function (feature) { return './feature/' + feature + '/aggregate/index.js' })
.forEach(function (item) { bundler.add(item) })
bundler.add('./agent/index.js', { entry: true })
bundler.external('loader')
loaderExports.forEach(function (module) {
bundler.external(module)
})
if (!min) bundler.transform('./lib/licenseify')
var bundleStream = bundler.bundle()
.pipe(source(filename))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
if (min) {
bundleStream = bundleStream.pipe(uglify({
mangle: {except: ['UncaughtException', 'nrWrapper']}
}))
}
return bundleStream
.pipe(sourcemaps.write('./', {addComment: false}))
.pipe(gulp.dest('./build/'))
}
}
function plugin (name, min) {
return function () {
var filename = 'nr-plugin-' + name + (min ? '.min' : '') + '.js'
var bundler = browserify({
externalRequireName: 'window.NREUM || (NREUM={});' + globalRequire,
prelude: agentPrelude,
transform: [preprocessify(), versionify(min)],
plugin: [collapser, cleanDeps],
debug: true
})
bundler.add('./plugins/' + name + '.js', { entry: true })
loaderExports.forEach(function (module) {
bundler.external(module)
})
var bundleStream = bundler.bundle()
.pipe(source(filename))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
if (min) {
bundleStream = bundleStream.pipe(uglify({
mangle: {except: ['UncaughtException', 'nrWrapper']}
}))
}
return bundleStream
.pipe(sourcemaps.write('./', {addComment: false}))
.pipe(gulp.dest('./build/'))
}
}