2016-03-02 23:06:43 -08:00
|
|
|
var watchify = require('watchify')
|
|
|
|
var browserify = require('browserify')
|
|
|
|
var gulp = require('gulp')
|
|
|
|
var source = require('vinyl-source-stream')
|
|
|
|
var buffer = require('vinyl-buffer')
|
|
|
|
var gutil = require('gulp-util')
|
|
|
|
var watch = require('gulp-watch')
|
|
|
|
var sourcemaps = require('gulp-sourcemaps')
|
2016-07-26 15:15:40 -07:00
|
|
|
var jsoneditor = require('gulp-json-editor')
|
|
|
|
var zip = require('gulp-zip')
|
2016-03-02 23:06:43 -08:00
|
|
|
var assign = require('lodash.assign')
|
|
|
|
var livereload = require('gulp-livereload')
|
2016-07-25 16:34:29 -07:00
|
|
|
var brfs = require('gulp-brfs')
|
2016-03-02 23:06:43 -08:00
|
|
|
var del = require('del')
|
2016-06-21 12:07:13 -07:00
|
|
|
var eslint = require('gulp-eslint')
|
2016-06-21 12:40:09 -07:00
|
|
|
var fs = require('fs')
|
|
|
|
var path = require('path')
|
2016-07-26 15:35:08 -07:00
|
|
|
var manifest = require('./app/manifest.json')
|
2016-09-01 15:01:45 -07:00
|
|
|
var gulpif = require('gulp-if')
|
|
|
|
var replace = require('gulp-replace')
|
2016-03-02 23:06:43 -08:00
|
|
|
|
2016-09-02 14:00:36 -07:00
|
|
|
var production = gutil.env.production
|
2016-09-01 15:19:31 -07:00
|
|
|
|
2016-03-02 23:06:43 -08:00
|
|
|
// browser reload
|
|
|
|
|
|
|
|
gulp.task('dev:reload', function() {
|
|
|
|
livereload.listen({
|
|
|
|
port: 35729,
|
2016-07-26 15:15:40 -07:00
|
|
|
// basePath: './dist/firefox/'
|
2016-03-02 23:06:43 -08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// copy static
|
|
|
|
|
|
|
|
gulp.task('copy:locales', copyTask({
|
|
|
|
source: './app/_locales/',
|
2016-07-28 12:59:37 -07:00
|
|
|
destinations: [
|
|
|
|
'./dist/firefox/_locales',
|
|
|
|
'./dist/chrome/_locales',
|
2016-08-07 13:36:02 -07:00
|
|
|
'./dist/edge/_locales',
|
2016-07-28 12:59:37 -07:00
|
|
|
]
|
2016-03-02 23:06:43 -08:00
|
|
|
}))
|
|
|
|
gulp.task('copy:images', copyTask({
|
|
|
|
source: './app/images/',
|
2016-07-28 12:59:37 -07:00
|
|
|
destinations: [
|
|
|
|
'./dist/firefox/images',
|
|
|
|
'./dist/chrome/images',
|
2016-08-07 13:36:02 -07:00
|
|
|
'./dist/edge/images',
|
2016-07-28 12:59:37 -07:00
|
|
|
],
|
2016-03-02 23:06:43 -08:00
|
|
|
}))
|
2016-05-10 16:51:49 -07:00
|
|
|
gulp.task('copy:fonts', copyTask({
|
|
|
|
source: './app/fonts/',
|
2016-07-28 12:59:37 -07:00
|
|
|
destinations: [
|
|
|
|
'./dist/firefox/fonts',
|
|
|
|
'./dist/chrome/fonts',
|
2016-08-07 13:36:02 -07:00
|
|
|
'./dist/edge/fonts',
|
2016-07-28 12:59:37 -07:00
|
|
|
],
|
2016-05-10 16:51:49 -07:00
|
|
|
}))
|
2016-03-02 23:06:43 -08:00
|
|
|
gulp.task('copy:reload', copyTask({
|
2016-04-13 15:28:44 -07:00
|
|
|
source: './app/scripts/',
|
2016-07-28 12:59:37 -07:00
|
|
|
destinations: [
|
|
|
|
'./dist/firefox/scripts',
|
|
|
|
'./dist/chrome/scripts',
|
2016-08-07 13:36:02 -07:00
|
|
|
'./dist/edge/scripts',
|
2016-07-28 12:59:37 -07:00
|
|
|
],
|
2016-03-02 23:06:43 -08:00
|
|
|
pattern: '/chromereload.js',
|
|
|
|
}))
|
|
|
|
gulp.task('copy:root', copyTask({
|
|
|
|
source: './app/',
|
2016-07-28 12:59:37 -07:00
|
|
|
destinations: [
|
|
|
|
'./dist/firefox',
|
|
|
|
'./dist/chrome',
|
2016-08-07 13:36:02 -07:00
|
|
|
'./dist/edge',
|
2016-07-28 12:59:37 -07:00
|
|
|
],
|
2016-03-02 23:06:43 -08:00
|
|
|
pattern: '/*',
|
|
|
|
}))
|
2016-07-28 12:59:37 -07:00
|
|
|
|
2016-09-01 15:01:45 -07:00
|
|
|
gulp.task('manifest:chrome', function() {
|
|
|
|
return gulp.src('./dist/chrome/manifest.json')
|
2016-07-26 15:15:40 -07:00
|
|
|
.pipe(jsoneditor(function(json) {
|
|
|
|
delete json.applications
|
|
|
|
return json
|
|
|
|
}))
|
2016-07-26 16:48:48 -07:00
|
|
|
.pipe(gulp.dest('./dist/chrome', { overwrite: true }))
|
2016-07-26 15:15:40 -07:00
|
|
|
})
|
2016-07-28 12:59:37 -07:00
|
|
|
|
2016-09-01 15:19:31 -07:00
|
|
|
gulp.task('manifest:production', function() {
|
|
|
|
return gulp.src([
|
|
|
|
'./dist/firefox/manifest.json',
|
|
|
|
'./dist/chrome/manifest.json',
|
|
|
|
'./dist/edge/manifest.json',
|
|
|
|
],{base: './dist/'})
|
|
|
|
.pipe(gulpif(production,jsoneditor(function(json) {
|
|
|
|
json.background.scripts = ["scripts/background.js"]
|
|
|
|
return json
|
|
|
|
})))
|
|
|
|
.pipe(gulp.dest('./dist/', { overwrite: true }))
|
|
|
|
})
|
|
|
|
|
2016-09-02 14:00:36 -07:00
|
|
|
const staticFiles = [
|
|
|
|
'locales',
|
|
|
|
'images',
|
|
|
|
'fonts',
|
|
|
|
'root'
|
|
|
|
]
|
|
|
|
|
|
|
|
var copyStrings = staticFiles.map(staticFile => `copy:${staticFile}`)
|
|
|
|
|
|
|
|
if (!production) {
|
|
|
|
copyStrings.push('copy:`reload`')
|
|
|
|
}
|
|
|
|
|
|
|
|
gulp.task('copy', gulp.series(gulp.parallel(...copyStrings), 'manifest:production', 'manifest:chrome'))
|
2016-03-02 23:06:43 -08:00
|
|
|
gulp.task('copy:watch', function(){
|
2016-06-14 12:43:55 -07:00
|
|
|
gulp.watch(['./app/{_locales,images}/*', './app/scripts/chromereload.js', './app/*.{html,json}'], gulp.series('copy'))
|
2016-03-02 23:06:43 -08:00
|
|
|
})
|
|
|
|
|
2016-06-21 12:07:13 -07:00
|
|
|
// lint js
|
|
|
|
|
|
|
|
gulp.task('lint', function () {
|
2016-07-26 15:15:40 -07:00
|
|
|
// Ignoring node_modules, dist/firefox, and docs folders:
|
|
|
|
return gulp.src(['app/**/*.js', 'ui/**/*.js', '!node_modules/**', '!dist/firefox/**', '!docs/**', '!app/scripts/chromereload.js'])
|
2016-06-21 12:40:09 -07:00
|
|
|
.pipe(eslint(fs.readFileSync(path.join(__dirname, '.eslintrc'))))
|
2016-06-21 12:07:13 -07:00
|
|
|
// eslint.format() outputs the lint results to the console.
|
|
|
|
// Alternatively use eslint.formatEach() (see Docs).
|
|
|
|
.pipe(eslint.format())
|
|
|
|
// To have the process exit with an error code (1) on
|
|
|
|
// lint error, return the stream and pipe to failAfterError last.
|
|
|
|
.pipe(eslint.failAfterError())
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
gulp.task('default', ['lint'], function () {
|
|
|
|
// This will only run if the lint task is successful...
|
|
|
|
});
|
|
|
|
*/
|
2016-03-02 23:06:43 -08:00
|
|
|
|
|
|
|
// build js
|
|
|
|
|
2016-07-28 10:50:14 -07:00
|
|
|
const jsFiles = [
|
|
|
|
'inpage',
|
|
|
|
'contentscript',
|
|
|
|
'background',
|
|
|
|
'popup',
|
|
|
|
]
|
|
|
|
|
2016-09-02 14:00:36 -07:00
|
|
|
var jsDevStrings = jsFiles.map(jsFile => `dev:js:${jsFile}`)
|
|
|
|
var jsBuildStrings = jsFiles.map(jsFile => `build:js:${jsFile}`)
|
|
|
|
|
2016-07-28 10:50:14 -07:00
|
|
|
jsFiles.forEach((jsFile) => {
|
|
|
|
gulp.task(`dev:js:${jsFile}`, bundleTask({ watch: true, filename: `${jsFile}.js` }))
|
|
|
|
gulp.task(`build:js:${jsFile}`, bundleTask({ watch: false, filename: `${jsFile}.js` }))
|
|
|
|
})
|
|
|
|
|
2016-09-02 14:00:36 -07:00
|
|
|
gulp.task('dev:js', gulp.parallel(...jsDevStrings))
|
|
|
|
|
|
|
|
gulp.task('build:js', gulp.parallel(...jsBuildStrings))
|
2016-03-02 23:06:43 -08:00
|
|
|
|
|
|
|
|
2016-07-26 17:25:15 -07:00
|
|
|
// clean dist
|
2016-03-02 23:06:43 -08:00
|
|
|
|
|
|
|
|
|
|
|
gulp.task('clean', function clean() {
|
2016-07-26 15:15:40 -07:00
|
|
|
return del(['./dist/*'])
|
2016-03-02 23:06:43 -08:00
|
|
|
})
|
|
|
|
|
2016-07-26 15:15:40 -07:00
|
|
|
// zip tasks for distribution
|
|
|
|
gulp.task('zip:chrome', () => {
|
|
|
|
return gulp.src('dist/chrome/**')
|
2016-07-26 15:35:08 -07:00
|
|
|
.pipe(zip(`metamask-chrome-${manifest.version}.zip`))
|
|
|
|
.pipe(gulp.dest('builds'));
|
2016-08-07 13:36:02 -07:00
|
|
|
})
|
2016-07-26 15:15:40 -07:00
|
|
|
gulp.task('zip:firefox', () => {
|
|
|
|
return gulp.src('dist/firefox/**')
|
2016-07-26 15:35:08 -07:00
|
|
|
.pipe(zip(`metamask-firefox-${manifest.version}.zip`))
|
|
|
|
.pipe(gulp.dest('builds'));
|
2016-08-07 13:36:02 -07:00
|
|
|
})
|
|
|
|
gulp.task('zip:edge', () => {
|
|
|
|
return gulp.src('dist/edge/**')
|
|
|
|
.pipe(zip(`metamask-edge-${manifest.version}.zip`))
|
|
|
|
.pipe(gulp.dest('builds'));
|
|
|
|
})
|
|
|
|
gulp.task('zip', gulp.parallel('zip:chrome', 'zip:firefox', 'zip:edge'))
|
2016-03-02 23:06:43 -08:00
|
|
|
|
|
|
|
// high level tasks
|
|
|
|
|
2016-06-21 13:56:04 -07:00
|
|
|
gulp.task('dev', gulp.series('dev:js', 'copy', gulp.parallel('copy:watch', 'dev:reload')))
|
2016-09-02 14:00:36 -07:00
|
|
|
|
2016-07-28 12:59:37 -07:00
|
|
|
gulp.task('build', gulp.series('clean', gulp.parallel('build:js', 'copy')))
|
2016-07-26 15:15:40 -07:00
|
|
|
gulp.task('dist', gulp.series('build', 'zip'))
|
2016-03-02 23:06:43 -08:00
|
|
|
|
|
|
|
// task generators
|
|
|
|
|
|
|
|
function copyTask(opts){
|
|
|
|
var source = opts.source
|
|
|
|
var destination = opts.destination
|
2016-07-28 12:59:37 -07:00
|
|
|
var destinations = opts.destinations || [ destination ]
|
2016-03-02 23:06:43 -08:00
|
|
|
var pattern = opts.pattern || '/**/*'
|
|
|
|
|
|
|
|
return performCopy
|
|
|
|
|
|
|
|
function performCopy(){
|
2016-07-28 12:59:37 -07:00
|
|
|
let stream = gulp.src(source + pattern, { base: source })
|
|
|
|
destinations.forEach(function(destination) {
|
|
|
|
stream = stream.pipe(gulp.dest(destination))
|
|
|
|
})
|
2016-09-02 14:00:36 -07:00
|
|
|
stream.pipe(gulpif(production,livereload()))
|
2016-03-02 23:06:43 -08:00
|
|
|
|
2016-07-28 12:59:37 -07:00
|
|
|
return stream
|
2016-03-02 23:06:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bundleTask(opts) {
|
|
|
|
var browserifyOpts = assign({}, watchify.args, {
|
|
|
|
entries: ['./app/scripts/'+opts.filename],
|
|
|
|
debug: true,
|
2016-04-20 13:22:41 -07:00
|
|
|
plugin: 'browserify-derequire',
|
2016-03-02 23:06:43 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
var bundler = browserify(browserifyOpts)
|
2016-04-13 15:28:44 -07:00
|
|
|
bundler.transform('brfs')
|
2016-03-02 23:06:43 -08:00
|
|
|
if (opts.watch) {
|
|
|
|
bundler = watchify(bundler)
|
|
|
|
bundler.on('update', performBundle) // on any dep update, runs the bundler
|
|
|
|
}
|
|
|
|
|
|
|
|
bundler.on('log', gutil.log) // output build logs to terminal
|
|
|
|
|
|
|
|
return performBundle
|
|
|
|
|
|
|
|
function performBundle(){
|
|
|
|
return (
|
|
|
|
|
|
|
|
bundler.bundle()
|
|
|
|
// log errors if they happen
|
|
|
|
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
|
|
|
|
.pipe(source(opts.filename))
|
2016-07-25 16:34:29 -07:00
|
|
|
.pipe(brfs())
|
2016-03-02 23:06:43 -08:00
|
|
|
// optional, remove if you don't need to buffer file contents
|
|
|
|
.pipe(buffer())
|
|
|
|
// optional, remove if you dont want sourcemaps
|
|
|
|
.pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
|
2016-04-13 15:28:44 -07:00
|
|
|
// Add transformation tasks to the pipeline here.
|
2016-03-02 23:06:43 -08:00
|
|
|
.pipe(sourcemaps.write('./')) // writes .map file
|
2016-07-26 15:15:40 -07:00
|
|
|
.pipe(gulp.dest('./dist/firefox/scripts'))
|
2016-07-28 10:50:14 -07:00
|
|
|
.pipe(gulp.dest('./dist/chrome/scripts'))
|
2016-08-07 13:36:02 -07:00
|
|
|
.pipe(gulp.dest('./dist/edge/scripts'))
|
2016-09-02 14:00:36 -07:00
|
|
|
.pipe(gulpif(!production,livereload()))
|
2016-03-02 23:06:43 -08:00
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|