forked from duckduckgo/duckduckgo-privacy-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
223 lines (199 loc) · 8.14 KB
/
Gruntfile.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module.exports = function(grunt) {
const through = require('through2')
require('load-grunt-tasks')(grunt)
grunt.loadNpmTasks('grunt-execute')
grunt.loadNpmTasks('grunt-karma')
var values = require('object.values');
if(!Object.values) {
values.shim()
}
let browser = grunt.option('browser')
let buildType = grunt.option('type')
if(!(browser && buildType)) {
console.error("Missing browser or build type: --browser=<browser-name> --type=<dev,release>")
process.exit(1)
}
let buildPath = `build/${browser}/${buildType}`
/* These are files common to all browsers. To add or override any of these files
* see the browserMap object below */
let baseFileMap = {
ui: {
'<%= dirs.public.js %>/base.js': ['<%= dirs.src.js %>/ui/base/index.es6.js'],
'<%= dirs.public.js %>/popup.js': ['<%= dirs.src.js %>/ui/pages/popup.es6.js'],
'<%= dirs.public.js %>/options.js': ['<%= dirs.src.js %>/ui/pages/options.es6.js'],
'<%= dirs.public.js %>/feedback.js': ['<%= dirs.src.js %>/ui/pages/feedback.es6.js']
},
background: {
'<%= dirs.public.js %>/background.js': ['<%= dirs.src.js %>/background/background.es6.js']
},
backgroundTest: {
'<%= dirs.test %>/background.js': ['<%= dirs.src.js %>/background/background.es6.js', '<%= dirs.test %>/requireHelper.js']
},
unitTest: {
'<%= dirs.unitTest.build %>/background.js': ['<%= dirs.unitTest.background %>/**/*.js'],
'<%= dirs.unitTest.build %>/ui.js': ['<%= dirs.src.js %>/ui/base/index.es6.js', '<%= dirs.unitTest.ui %>/**/*.js'],
'<%= dirs.unitTest.build %>/shared-utils.js': ['<%= dirs.unitTest.sharedUtils %>/**/*.js']
},
sass: {
'<%= dirs.public.css %>/noatb.css': ['<%= dirs.src.scss %>/noatb.scss'],
'<%= dirs.public.css %>/base.css': ['<%= dirs.src.scss %>/base/base.scss'],
'<%= dirs.public.css %>/popup.css': ['<%= dirs.src.scss %>/popup.scss'],
'<%= dirs.public.css %>/options.css': ['<%= dirs.src.scss %>/options.scss'],
'<%= dirs.public.css %>/feedback.css': ['<%= dirs.src.scss %>/feedback.scss']
}
}
// for the dev version of the extension only, add some extra debug code
if (buildType === 'dev') {
baseFileMap.background['<%= dirs.public.js %>/background.js'].unshift('<%= dirs.src.js %>/background/debug.es6.js')
}
/* watch any base files and browser specific files */
let watch = {
sass: ['<%= dirs.src.scss %>/**/*.scss'],
ui: ['<%= dirs.src.js %>/ui/**/*.es6.js','<%= dirs.data %>/*.js'],
background: ['<%= dirs.src.js %>/background/**/*.js','<%= dirs.data %>/*.js']
}
let karmaOps = {
configFile: 'karma.conf.js',
basePath: 'build/test/',
files: ['background.js','ui.js','shared-utils.js']
}
// override some options to allow the devs
// to open the test page manually and debug
if (grunt.option('test-debug')) {
Object.assign(karmaOps, {
// don't kill the process when first test is run
singleRun: false,
// INFO outputs the url/port for the test page
logLevel: 'INFO',
// don't run headless chrome tests
browsers: []
})
}
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
dirs: {
cache: '.cache',
src: {
js: 'shared/js',
scss: 'shared/scss',
templates: 'shared/templates'
},
data: 'shared/data',
public: {
js: `${buildPath}/public/js`,
css: `${buildPath}/public/css`
},
test: 'test',
unitTest: {
background: `unit-test/background`,
ui: `unit-test/ui`,
sharedUtils: `unit-test/shared-utils`,
build: `build/test`
}
},
browserify: {
options: {
browserifyOptions: {
debug: buildType === 'dev'
},
transform: [
['babelify'],
[(file) => {
return through( function(buf, enc, next) {
let requireName = browser
if(browser === 'duckduckgo.safariextension') {
requireName = 'safari'
}
else if (browser === 'firefox') {
requireName = 'chrome'
}
this.push(buf.toString('utf8').replace(/\$BROWSER/g, requireName))
next()
})
}]
]
},
ui: {
files: baseFileMap.ui
},
background: {
files: baseFileMap.background
},
backgroundTest: {
files: baseFileMap.backgroundTest
},
unitTest: {
options: {
browserifyOptions: {
debug: true
}
},
files: baseFileMap.unitTest
}
},
sass: {
dist: {
files: baseFileMap.sass
}
},
execute: {
preProcessLists: {
src: ['scripts/buildEntityMap.js']
},
tosdr: {
src: []//'scripts/tosdr.js']
}
},
// used by watch to copy shared/js to build dir
exec: {
copyjs: `cp shared/js/*.js build/${browser}/${buildType}/js/ && rm build/${browser}/${buildType}/js/*.es6.js`,
tmpSafari: `mv build/${browser}/${buildType} build/${browser}/tmp && mkdir -p build/${browser}/${buildType}/`,
mvSafari: `mv build/${browser}/tmp build/${browser}/${buildType}/ && mv build/${browser}/${buildType}/tmp build/${browser}/${buildType}/${browser}`,
mvWatchSafari: `rsync -ar build/${browser}/${buildType}/public build/${browser}/${buildType}/${browser}/ && rm -rf build/${browser}/${buildType}/public`
},
watch: {
scss: {
files: watch.sass,
tasks: ['sass']
},
ui: {
files: watch.ui,
tasks: ['browserify:ui', 'watchSafari']
},
backgroundES6JS: {
files: watch.background,
tasks: ['browserify:background', 'watchSafari']
},
backgroundJS: {
files: ['<%= dirs.src.js %>/*.js'],
tasks: ['exec:copyjs', 'watchSafari']
}
},
karma: {
unit: {
options: karmaOps
}
}
})
// sets up safari directory structure so that it can be loaded in extension builder
// duckduckgo.safariextension -> build type -> duckduckgo.safariextension -> build files
grunt.registerTask('safari', 'Move Safari build', (() => {
if (browser === 'duckduckgo.safariextension') {
console.log("Moving Safari build")
grunt.task.run('exec:tmpSafari')
grunt.task.run('exec:mvSafari')
}
}))
// moves generated files from watch into the correct build directory
grunt.registerTask('watchSafari', 'Moves Safari files after watch', (() => {
if (browser === 'duckduckgo.safariextension') {
grunt.task.run('exec:mvWatchSafari')
}
}))
grunt.registerTask('build', 'Build project(s)css, templates, js', ['sass', 'browserify:ui', 'browserify:background', 'browserify:backgroundTest', 'execute:preProcessLists', 'safari'])
const devTasks = ['build']
if (grunt.option('watch')) { devTasks.push('watch') }
grunt.registerTask('dev', 'Build and optionally watch files for development', devTasks)
grunt.registerTask('test','Build and run tests', ['browserify:unitTest','karma'])
grunt.registerTask('default', 'build')
}