Skip to content

Commit

Permalink
feat(grunt): use grunt-service-worker
Browse files Browse the repository at this point in the history
  • Loading branch information
sparanoid committed Jan 14, 2017
1 parent 8962aac commit 3bc06f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 50 deletions.
73 changes: 24 additions & 49 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"use strict"

path = require("path")
sw_precache = require("sw-precache")

module.exports = (grunt) ->

# Load all grunt tasks
Expand All @@ -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: "<!-- <%= config.pkg.name %> v<%= config.pkg.version %> | © <%= config.pkg.author %> | <%= config.pkg.license %> -->\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: "<!-- <%= config.pkg.name %> v<%= config.pkg.version %> | © <%= config.pkg.author %> | <%= config.pkg.license %> -->\n"

amsf:
base: ".amsf-cache"
branch: grunt.option("branch") or "release"
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -723,7 +698,7 @@ module.exports = (grunt) ->
"uncss_inline"
"cacheBust"
"concurrent:dist"
"sw_precache"
"service_worker"
"uglify:sw"
"cleanempty"
]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 3bc06f1

Please sign in to comment.