nifty-wallet/gulpfile.js

516 lines
13 KiB
JavaScript
Raw Normal View History

2018-03-28 20:13:47 -07:00
const watchify = require('watchify')
const browserify = require('browserify')
const disc = require('disc')
const gulp = require('gulp')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const gutil = require('gulp-util')
const watch = require('gulp-watch')
const sourcemaps = require('gulp-sourcemaps')
const jsoneditor = require('gulp-json-editor')
const zip = require('gulp-zip')
const assign = require('lodash.assign')
const livereload = require('gulp-livereload')
const del = require('del')
const eslint = require('gulp-eslint')
const fs = require('fs')
const path = require('path')
const manifest = require('./app/manifest.json')
const gulpif = require('gulp-if')
const replace = require('gulp-replace')
const mkdirp = require('mkdirp')
const asyncEach = require('async/each')
const exec = require('child_process').exec
const sass = require('gulp-sass')
const autoprefixer = require('gulp-autoprefixer')
const gulpStylelint = require('gulp-stylelint')
const stylefmt = require('gulp-stylefmt')
const uglify = require('gulp-uglify-es').default
const babel = require('gulp-babel')
const debug = require('gulp-debug')
const disableDebugTools = gutil.env.disableDebugTools
const debugMode = gutil.env.debug
2018-03-28 19:56:44 -07:00
const browserPlatforms = [
'firefox',
'chrome',
'edge',
'opera',
2018-03-28 19:56:44 -07:00
]
const commonPlatforms = [
// browser webapp
'mascara',
2018-03-28 19:56:44 -07:00
// browser extensions
...browserPlatforms
]
2016-03-02 23:06:43 -08:00
// browser reload
gulp.task('dev:reload', function() {
livereload.listen({
port: 35729,
})
})
2018-03-28 19:56:44 -07:00
// copy universal
2016-03-02 23:06:43 -08:00
const copyTaskNames = []
const copyDevTaskNames = []
createCopyTasks('locales', {
2016-03-02 23:06:43 -08:00
source: './app/_locales/',
destinations: commonPlatforms.map(platform => `./dist/${platform}/_locales`),
})
createCopyTasks('images', {
2016-03-02 23:06:43 -08:00
source: './app/images/',
destinations: commonPlatforms.map(platform => `./dist/${platform}/images`),
})
createCopyTasks('contractImages', {
source: `${require.resolve('eth-contract-metadata')}/images/`,
destinations: commonPlatforms.map(platform => `./dist/${platform}/images/contract`),
})
createCopyTasks('fonts', {
source: './app/fonts/',
destinations: commonPlatforms.map(platform => `./dist/${platform}/fonts`),
})
createCopyTasks('reload', {
devOnly: true,
source: './app/scripts/',
2016-03-02 23:06:43 -08:00
pattern: '/chromereload.js',
2018-03-28 20:54:59 -07:00
destinations: commonPlatforms.map(platform => `./dist/${platform}`),
})
createCopyTasks('html', {
source: './app/',
2018-03-28 20:54:59 -07:00
pattern: '/*.html',
destinations: commonPlatforms.map(platform => `./dist/${platform}`),
})
2018-03-28 19:56:44 -07:00
// copy extension
createCopyTasks('manifest', {
2018-03-28 19:56:44 -07:00
source: './app/',
pattern: '/*.json',
destinations: browserPlatforms.map(platform => `./dist/${platform}`),
})
2018-03-28 20:54:59 -07:00
// copy mascara
createCopyTasks('html:mascara', {
2018-03-28 20:54:59 -07:00
source: './mascara/',
pattern: 'proxy/index.html',
destinations: [`./dist/mascara/`],
})
function createCopyTasks(label, opts) {
if (!opts.devOnly) {
const copyTaskName = `copy:${label}`
copyTask(copyTaskName, opts)
copyTaskNames.push(copyTaskName)
}
const copyDevTaskName = `copy:dev:${label}`
copyTask(copyDevTaskName, Object.assign({ devMode: true }, opts))
copyDevTaskNames.push(copyDevTaskName)
}
2018-03-28 19:56:44 -07:00
// manifest tinkering
2016-09-01 15:01:45 -07:00
gulp.task('manifest:chrome', function() {
return gulp.src('./dist/chrome/manifest.json')
.pipe(jsoneditor(function(json) {
delete json.applications
return json
}))
2016-07-26 16:48:48 -07:00
.pipe(gulp.dest('./dist/chrome', { overwrite: true }))
})
gulp.task('manifest:opera', function() {
return gulp.src('./dist/opera/manifest.json')
.pipe(jsoneditor(function(json) {
json.permissions = [
"storage",
"tabs",
"clipboardWrite",
"clipboardRead",
"http://localhost:8545/"
]
return json
}))
.pipe(gulp.dest('./dist/opera', { overwrite: true }))
})
gulp.task('manifest:production', function() {
return gulp.src([
'./dist/firefox/manifest.json',
'./dist/chrome/manifest.json',
'./dist/edge/manifest.json',
'./dist/opera/manifest.json',
],{base: './dist/'})
// Exclude chromereload script in production:
2018-03-28 19:56:44 -07:00
.pipe(gulpif(!debugMode,jsoneditor(function(json) {
json.background.scripts = json.background.scripts.filter((script) => {
return !script.includes('chromereload')
})
return json
})))
.pipe(gulp.dest('./dist/', { overwrite: true }))
})
2018-03-28 19:56:44 -07:00
gulp.task('copy',
gulp.series(
gulp.parallel(...copyTaskNames),
'manifest:production',
'manifest:chrome',
'manifest:opera'
)
)
gulp.task('copy:dev',
gulp.series(
gulp.parallel(...copyDevTaskNames),
'manifest:chrome',
'manifest:opera'
)
)
2016-03-02 23:06:43 -08:00
2016-06-21 12:07:13 -07:00
// lint js
gulp.task('lint', function () {
// Ignoring node_modules, dist/firefox, and docs folders:
2018-01-17 15:45:36 -08:00
return gulp.src(['app/**/*.js', '!app/scripts/vendor/**/*.js', 'ui/**/*.js', 'mascara/src/*.js', 'mascara/server/*.js', '!node_modules/**', '!dist/firefox/**', '!docs/**', '!app/scripts/chromereload.js', '!mascara/test/jquery-3.1.0.min.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('lint:fix', function () {
return gulp.src(['app/**/*.js', 'ui/**/*.js', 'mascara/src/*.js', 'mascara/server/*.js', '!node_modules/**', '!dist/firefox/**', '!docs/**', '!app/scripts/chromereload.js', '!mascara/test/jquery-3.1.0.min.js'])
.pipe(eslint(Object.assign(fs.readFileSync(path.join(__dirname, '.eslintrc')), {fix: true})))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
});
// scss compilation and autoprefixing tasks
gulp.task('build:scss', function () {
return gulp.src('ui/app/css/index.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(autoprefixer())
.pipe(gulp.dest('ui/app/css/output'))
2017-08-06 13:44:50 -07:00
})
gulp.task('watch:scss', function() {
2017-08-06 13:44:50 -07:00
gulp.watch(['ui/app/css/**/*.scss'], gulp.series(['build:scss']))
})
gulp.task('lint-scss', function() {
return gulp
.src('ui/app/css/itcss/**/*.scss')
.pipe(gulpStylelint({
reporters: [
{ formatter: 'string', console: true }
],
fix: true,
}));
});
gulp.task('fmt-scss', function () {
return gulp.src('ui/app/css/itcss/**/*.scss')
.pipe(stylefmt())
.pipe(gulp.dest('ui/app/css/itcss'));
});
2018-03-28 20:41:56 -07:00
// build js
const buildJsFiles = [
'inpage',
'contentscript',
'background',
'ui',
]
2017-01-10 13:46:15 -08:00
// bundle tasks
2018-03-28 20:41:56 -07:00
createTasksForBuildJsExtension({ buildJsFiles, taskPrefix: 'dev:js', devMode: true })
createTasksForBuildJsExtension({ buildJsFiles, taskPrefix: 'build:js:extension' })
createTasksForBuildJsMascara({ taskPrefix: 'build:js:mascara' })
2018-03-28 20:41:56 -07:00
function createTasksForBuildJsExtension({ buildJsFiles, taskPrefix, devMode, bundleTaskOpts = {} }) {
// inpage must be built before all other scripts:
const rootDir = './app/scripts'
2018-03-28 20:41:56 -07:00
const nonInpageFiles = buildJsFiles.filter(file => file !== 'inpage')
const buildPhase1 = ['inpage']
const buildPhase2 = nonInpageFiles
const destinations = browserPlatforms.map(platform => `./dist/${platform}`)
2018-03-28 20:41:56 -07:00
bundleTaskOpts = Object.assign({
buildSourceMaps: true,
sourceMapDir: devMode ? './' : '../sourcemaps',
2018-03-28 20:41:56 -07:00
minifyBuild: !devMode,
buildWithFullPaths: devMode,
watch: devMode,
2018-03-28 20:41:56 -07:00
}, bundleTaskOpts)
createTasksForBuildJs({ rootDir, taskPrefix, bundleTaskOpts, destinations, buildPhase1, buildPhase2 })
}
2017-01-10 13:46:15 -08:00
2018-03-28 20:41:56 -07:00
function createTasksForBuildJsMascara({ taskPrefix, devMode, bundleTaskOpts = {} }) {
// inpage must be built before all other scripts:
const rootDir = './mascara/src/'
2018-03-28 20:41:56 -07:00
const buildPhase1 = ['ui', 'proxy', 'background']
2018-03-28 19:56:44 -07:00
const destinations = ['./dist/mascara']
2018-03-28 20:41:56 -07:00
bundleTaskOpts = Object.assign({
buildSourceMaps: true,
sourceMapDir: './',
minifyBuild: !devMode,
buildWithFullPaths: devMode,
watch: devMode,
2018-03-28 20:41:56 -07:00
}, bundleTaskOpts)
createTasksForBuildJs({ rootDir, taskPrefix, bundleTaskOpts, destinations, buildPhase1 })
}
2018-03-28 20:41:56 -07:00
function createTasksForBuildJs({ rootDir, taskPrefix, bundleTaskOpts, destinations, buildPhase1 = [], buildPhase2 = [] }) {
// bundle task for each file
2018-03-28 20:41:56 -07:00
const jsFiles = [].concat(buildPhase1, buildPhase2)
jsFiles.forEach((jsFile) => {
gulp.task(`${taskPrefix}:${jsFile}`, bundleTask(Object.assign({
label: jsFile,
filename: `${jsFile}.js`,
filepath: `${rootDir}/${jsFile}.js`,
destinations,
}, bundleTaskOpts)))
})
// compose into larger task
const subtasks = []
subtasks.push(gulp.parallel(buildPhase1.map(file => `${taskPrefix}:${file}`)))
if (buildPhase2.length) subtasks.push(gulp.parallel(buildPhase2.map(file => `${taskPrefix}:${file}`)))
2017-10-12 11:03:42 -07:00
2018-03-29 17:37:49 -07:00
gulp.task(taskPrefix, gulp.series(subtasks))
}
2016-03-02 23:06:43 -08:00
2017-01-10 13:46:15 -08:00
// disc bundle analyzer tasks
2018-03-28 20:41:56 -07:00
buildJsFiles.forEach((jsFile) => {
gulp.task(`disc:${jsFile}`, discTask({ label: jsFile, filename: `${jsFile}.js` }))
2017-01-10 13:46:15 -08:00
})
2018-03-28 20:41:56 -07:00
gulp.task('disc', gulp.parallel(buildJsFiles.map(jsFile => `disc:${jsFile}`)))
2017-01-10 13:46:15 -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() {
return del(['./dist/*'])
2016-03-02 23:06:43 -08:00
})
// zip tasks for distribution
2017-01-10 13:08:13 -08:00
gulp.task('zip:chrome', zipTask('chrome'))
gulp.task('zip:firefox', zipTask('firefox'))
gulp.task('zip:edge', zipTask('edge'))
gulp.task('zip:opera', zipTask('opera'))
gulp.task('zip', gulp.parallel('zip:chrome', 'zip:firefox', 'zip:edge', 'zip:opera'))
2016-03-02 23:06:43 -08:00
2018-03-28 20:13:47 -07:00
// set env for production
gulp.task('apply-prod-environment', function(done) {
process.env.NODE_ENV = 'production'
done()
});
2016-03-02 23:06:43 -08:00
// high level tasks
gulp.task('dev',
gulp.series(
'build:scss',
'copy',
gulp.parallel(
'dev:js',
'watch:scss',
'copy:dev',
'dev:reload'
)
)
)
gulp.task('build',
gulp.series(
'clean',
'build:scss',
gulp.parallel(
'build:js:extension',
'build:js:mascara',
'copy'
)
)
)
gulp.task('dist',
gulp.series(
'apply-prod-environment',
'build',
'zip'
)
)
2016-03-02 23:06:43 -08:00
// task generators
function copyTask(taskName, opts){
2018-03-28 20:13:47 -07:00
const source = opts.source
const destination = opts.destination
const destinations = opts.destinations || [ destination ]
const pattern = opts.pattern || '/**/*'
const devMode = opts.devMode
2016-03-02 23:06:43 -08:00
return gulp.task(taskName, performCopy)
2016-03-02 23:06:43 -08:00
function performCopy(){
// stream from source
let stream
if (devMode) {
stream = watch(source + pattern, { base: source })
} else {
stream = gulp.src(source + pattern, { base: source })
}
// copy to destinations
destinations.forEach(function(destination) {
stream = stream.pipe(gulp.dest(destination))
})
// trigger reload
if (devMode) {
stream.pipe(livereload())
}
2016-03-02 23:06:43 -08:00
return stream
2016-03-02 23:06:43 -08:00
}
}
2017-01-10 13:08:13 -08:00
function zipTask(target) {
return () => {
return gulp.src(`dist/${target}/**`)
.pipe(zip(`metamask-${target}-${manifest.version}.zip`))
2017-08-06 13:44:50 -07:00
.pipe(gulp.dest('builds'))
2017-01-10 13:08:13 -08:00
}
}
function generateBundler(opts, performBundle) {
const browserifyOpts = assign({}, watchify.args, {
entries: [opts.filepath],
2016-04-20 13:22:41 -07:00
plugin: 'browserify-derequire',
2018-03-28 20:41:56 -07:00
debug: opts.buildSourceMaps,
fullPaths: opts.buildWithFullPaths,
2016-03-02 23:06:43 -08:00
})
let bundler = browserify(browserifyOpts)
2017-01-10 13:46:15 -08:00
if (opts.watch) {
bundler = watchify(bundler)
// on any file update, re-runs the bundler
2017-01-10 13:56:31 -08:00
bundler.on('update', performBundle)
2017-01-10 13:46:15 -08:00
}
return bundler
}
function discTask(opts) {
2018-03-28 20:41:56 -07:00
opts = Object.assign({
buildWithFullPaths: true,
}, opts)
const bundler = generateBundler(opts, performBundle)
2017-01-10 13:56:31 -08:00
// output build logs to terminal
bundler.on('log', gutil.log)
2017-01-10 13:46:15 -08:00
return performBundle
function performBundle(){
// start "disc" build
2018-03-28 20:41:56 -07:00
const discDir = path.join(__dirname, 'disc')
2017-01-10 13:46:15 -08:00
mkdirp.sync(discDir)
2018-03-28 20:41:56 -07:00
const discPath = path.join(discDir, `${opts.label}.html`)
2017-01-10 13:46:15 -08:00
return (
bundler.bundle()
.pipe(disc())
.pipe(fs.createWriteStream(discPath))
)
}
}
function bundleTask(opts) {
const bundler = generateBundler(opts, performBundle)
2017-01-10 13:56:31 -08:00
// output build logs to terminal
bundler.on('log', gutil.log)
2016-03-02 23:06:43 -08:00
return performBundle
function performBundle(){
let buildStream = bundler.bundle()
// handle errors
buildStream.on('error', (err) => {
beep()
if (opts.watch) {
console.warn(err.stack)
} else {
throw err
}
})
2017-08-08 17:46:31 -07:00
// process bundles
buildStream = buildStream
2017-01-10 13:56:31 -08:00
// convert bundle stream to gulp vinyl stream
2016-03-02 23:06:43 -08:00
.pipe(source(opts.filename))
2017-01-10 13:56:31 -08:00
// inject variables into bundle
2018-03-28 19:56:44 -07:00
.pipe(replace('\'GULP_METAMASK_DEBUG\'', debugMode))
2017-01-10 13:56:31 -08:00
// buffer file contents (?)
2016-03-02 23:06:43 -08:00
.pipe(buffer())
2018-03-28 20:41:56 -07:00
// Initialize Source Maps
if (opts.buildSourceMaps) {
buildStream = buildStream
// loads map from browserify file
.pipe(sourcemaps.init({ loadMaps: true }))
}
// Minification
if (opts.minifyBuild) {
buildStream = buildStream
.pipe(uglify({
mangle: {
reserved: [ 'MetamaskInpageProvider' ]
},
}))
}
// Finalize Source Maps (writes .map file)
if (opts.buildSourceMaps) {
buildStream = buildStream
.pipe(sourcemaps.write(opts.sourceMapDir))
}
// write completed bundles
opts.destinations.forEach((dest) => {
buildStream = buildStream.pipe(gulp.dest(dest))
})
// finally, trigger live reload
buildStream = buildStream
2018-03-28 19:56:44 -07:00
.pipe(gulpif(debugMode, livereload()))
2016-03-02 23:06:43 -08:00
return buildStream
2016-03-02 23:06:43 -08:00
}
}
2017-08-08 17:46:31 -07:00
function beep () {
process.stdout.write('\x07')
}