Browse Source

Add js uglify

Gerald 9 years ago
parent
commit
e35aa4ca18
4 changed files with 45 additions and 31 deletions
  1. 36 21
      gulpfile.js
  2. 4 3
      package.json
  3. 0 0
      src/background/app.js
  4. 5 7
      src/background/sync/index.js

+ 36 - 21
gulpfile.js

@@ -6,9 +6,11 @@ const cssnano = require('gulp-cssnano');
 const gulpFilter = require('gulp-filter');
 const order = require('gulp-order');
 const eslint = require('gulp-eslint');
+const uglify = require('gulp-uglify');
 const templateCache = require('./scripts/templateCache');
 const i18n = require('./scripts/i18n');
 const pkg = require('./package.json');
+const isProd = process.env.NODE_ENV === 'production';
 
 const paths = {
   cache: 'src/cache.js',
@@ -50,39 +52,48 @@ gulp.task('eslint', () => (
   .pipe(eslint.format())
 ));
 
-gulp.task('templates', () => (
-  merge2([
-    gulp.src(paths.cache),
+gulp.task('templates', () => {
+  var stream = merge2([
     gulp.src(paths.templates).pipe(templateCache()),
-  ]).pipe(concat('cache.js'))
-  .pipe(gulp.dest('dist'))
-));
+    gulp.src(paths.cache),
+  ])
+  .pipe(concat('cache.js'));
+  if (isProd) stream = stream.pipe(uglify());
+  return stream.pipe(gulp.dest('dist'));
+});
 
-gulp.task('js-bg', () => (
-  gulp.src(paths.jsBg)
-  .pipe(concat('background/app.js'))
-  .pipe(gulp.dest('dist'))
-));
+gulp.task('js-bg', () => {
+  var stream = gulp.src(paths.jsBg)
+  .pipe(order([
+    '**/utils.js',
+    '!**/app.js',
+  ]))
+  .pipe(concat('background/app.js'));
+  if (isProd) stream = stream.pipe(uglify());
+  return stream.pipe(gulp.dest('dist'));
+});
 
-gulp.task('js-options', () => (
-  gulp.src(paths.jsOptions)
+gulp.task('js-options', () => {
+  var stream = gulp.src(paths.jsOptions)
   .pipe(order([
     '**/tab-*.js',
     '!**/app.js',
   ]))
-  .pipe(concat('options/app.js'))
-  .pipe(gulp.dest('dist'))
-));
+  .pipe(concat('options/app.js'));
+  if (isProd) stream = stream.pipe(uglify());
+  return stream.pipe(gulp.dest('dist'));
+});
 
-gulp.task('js-popup', () => (
-  gulp.src(paths.jsPopup)
+gulp.task('js-popup', () => {
+  var stream = gulp.src(paths.jsPopup)
   .pipe(order([
     '**/base.js',
     '!**/app.js',
   ]))
-  .pipe(concat('popup/app.js'))
-  .pipe(gulp.dest('dist'))
-))
+  .pipe(concat('popup/app.js'));
+  if (isProd) stream = stream.pipe(uglify());
+  return stream.pipe(gulp.dest('dist'))
+});
 
 gulp.task('manifest', () => (
   gulp.src(paths.manifest, {base: 'src'})
@@ -91,8 +102,12 @@ gulp.task('manifest', () => (
 ));
 
 gulp.task('copy-files', () => {
+  const jsFilter = gulpFilter(['**/*.js'], {restore: true});
   const cssFilter = gulpFilter(['**/*.css'], {restore: true});
   return gulp.src(paths.copy)
+  .pipe(jsFilter)
+  .pipe(uglify())
+  .pipe(jsFilter.restore)
   .pipe(cssFilter)
   .pipe(cssnano({
     zindex: false,

+ 4 - 3
package.json

@@ -3,11 +3,11 @@
   "version": "2.2.6",
   "scripts": {
     "clean": "node -e \"require('del')(['dist'])\"",
-    "dev": "gulp build && gulp watch",
+    "predev": "gulp build",
+    "dev": "gulp watch",
     "i18n": "gulp i18n",
     "eslint": "gulp eslint",
-    "update": "node scripts/updateLib",
-    "version": "gulp build"
+    "update": "node scripts/updateLib"
   },
   "description": "Violentmonkey",
   "devDependencies": {
@@ -22,6 +22,7 @@
     "gulp-filter": "^4.0.0",
     "gulp-order": "^1.1.1",
     "gulp-replace": "^0.5.4",
+    "gulp-uglify": "^1.5.3",
     "gulp-util": "^3.0.7",
     "html-minifier": "^2.1.0",
     "js-yaml": "^3.5.5",

+ 0 - 0
src/background/main.js → src/background/app.js


+ 5 - 7
src/background/sync/index.js

@@ -1,4 +1,4 @@
-/* eslint no-console: 0 */
+/* eslint-disable no-console */
 var sync = function () {
   var services = [];
   var servicesReady = [];
@@ -481,12 +481,10 @@ var sync = function () {
     },
   });
 
-  setTimeout(function () {
-    _.tabs.update(function (tab) {
-      tab.url && services.some(function (service) {
-        return service.checkAuthenticate && service.checkAuthenticate(tab.url);
-      }) && _.tabs.remove(tab.id);
-    });
+  _.tabs.update(function (tab) {
+    tab.url && services.some(function (service) {
+      return service.checkAuthenticate && service.checkAuthenticate(tab.url);
+    }) && _.tabs.remove(tab.id);
   });
 
   return {