From 3bc06f1b264ed99d0a26b24f7460b54e2d4aafde Mon Sep 17 00:00:00 2001 From: Tunghsiao Liu Date: Sat, 14 Jan 2017 19:13:15 +0800 Subject: [PATCH] feat(grunt): use `grunt-service-worker` --- Gruntfile.coffee | 73 ++++++++++++++++-------------------------------- package.json | 2 +- 2 files changed, 25 insertions(+), 50 deletions(-) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index af7aea2f7..c90c242de 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -1,8 +1,5 @@ "use strict" -path = require("path") -sw_precache = require("sw-precache") - module.exports = (grunt) -> # Load all grunt tasks @@ -19,39 +16,19 @@ module.exports = (grunt) -> # Get deploy target, see `_deploy.yml` for more info deploy_env = grunt.option("env") or "default" - baseConfig = - cfg: grunt.file.readYAML("_config.yml") - pkg: grunt.file.readJSON("package.json") - amsf: grunt.file.readYAML("_amsf.yml") - deploy: grunt.file.readYAML("_deploy.yml") - app: "<%= config.cfg.source %>" - dist: "<%= config.cfg.destination %>" - base: "<%= config.cfg.base %>" - assets: "<%= config.cfg.assets %>" - banner: "\n" - - # TODO: Wrap it into a separate Grunt plugin? - writeServiceWorkerFile = (rootDir, handleFetch, callback) -> - config = - cacheId: baseConfig.pkg.name - handleFetch: handleFetch - logger: grunt.log.writeln - staticFileGlobs: [ - rootDir + "/**/**.css" - rootDir + "/**/**.html" - rootDir + "/**/**.jpg" - rootDir + "/**/**.png" - rootDir + "/**/**.svg" - rootDir + "/**/img/**.*" - rootDir + "/**/js/**.js" - ] - stripPrefix: rootDir - verbose: true - sw_precache.write path.join(baseConfig.cfg.destination + baseConfig.cfg.base, "service-worker.js"), config, callback - # Project configurations grunt.initConfig - config: baseConfig + config: + cfg: grunt.file.readYAML("_config.yml") + pkg: grunt.file.readJSON("package.json") + amsf: grunt.file.readYAML("_amsf.yml") + deploy: grunt.file.readYAML("_deploy.yml") + app: "<%= config.cfg.source %>" + dist: "<%= config.cfg.destination %>" + base: "<%= config.cfg.base %>" + assets: "<%= config.cfg.assets %>" + banner: "\n" + amsf: base: ".amsf-cache" branch: grunt.option("branch") or "release" @@ -66,11 +43,6 @@ module.exports = (grunt) -> new_name: grunt.option("theme") or "<%= amsf.theme.current %>" new_url: grunt.option("url") or "<%= amsf.theme.current_url %>" - sw_precache: - dist: - handleFetch: true - rootDir: "<%= config.dist %>" - coffeelint: options: indentation: 2 @@ -318,6 +290,18 @@ module.exports = (grunt) -> src: "**/*.html" ] + service_worker: + dist: + options: + cacheId: "<%= config.pkg.name %>" + baseDir: "<%= config.dist %>" + workerDir: "<%= config.dist %><%= config.base %>" + staticFileGlobs: [ + "**/*.{css,html,jpg,gif,png,svg}" + "**/img/**.*" + "**/js/**.js" + ] + usebanner: options: position: "bottom" @@ -608,15 +592,6 @@ module.exports = (grunt) -> push: false # Custom tasks - grunt.registerMultiTask "sw_precache", -> - done = @async() - handleFetch = @data.handleFetch - rootDir = @data.rootDir - writeServiceWorkerFile rootDir, handleFetch, (error) -> - if error - grunt.fail.warn error - done() - grunt.registerTask "amsf-func-mkdir", "Initialize AMSF working directory", -> grunt.file.mkdir '.amsf-cache' @@ -723,7 +698,7 @@ module.exports = (grunt) -> "uncss_inline" "cacheBust" "concurrent:dist" - "sw_precache" + "service_worker" "uglify:sw" "cleanempty" ] diff --git a/package.json b/package.json index e2fa0dd36..7b1fab34c 100644 --- a/package.json +++ b/package.json @@ -51,12 +51,12 @@ "grunt-minjson": "^0.4.0", "grunt-newer": "^1.2.0", "grunt-postcss": "^0.8.0", + "grunt-service-worker": "^0.1.1", "grunt-shell": "^2.1.0", "grunt-text-replace": "^0.4.0", "grunt-uncss-inline": "^1.0.5", "grunt-xmlmin": "^0.1.8", "jit-grunt": "^0.10.0", - "sw-precache": "^4.3.0", "time-grunt": "^1.4.0" } }