Created
November 5, 2017 20:23
-
-
Save zak905/8ce23504526ec09df1789f0f93afd13a to your computer and use it in GitHub Desktop.
Script to automatically generate service worker after a GWT build and before packaging the app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "gwt-pwa", | |
"version": "0.0.1", | |
"description": "generates service worker for GWT app", | |
"main": "serviceWorkerBuilder.js", | |
"dependencies": { | |
"handlebars": "^4.0.11" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"generate": "node serviceWorkerBuilder.js" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+https://github.com/zak905/gwt-pwa-demo.git" | |
}, | |
"author": "zakaria amine", | |
"license": "ISC", | |
"bugs": { | |
"url": "https://github.com/zak905/gwt-pwa-demo/issues" | |
}, | |
"homepage": "https://github.com/zak905/gwt-pwa-demo#readme" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plugin> | |
<groupId>com.github.eirslett</groupId> | |
<artifactId>frontend-maven-plugin</artifactId> | |
<version>1.6</version> | |
<executions> | |
<execution> | |
<id>npm install</id> | |
<goals> | |
<goal>npm</goal> | |
</goals> | |
<phase>generate-resources</phase> | |
<configuration> | |
<arguments>install</arguments> | |
</configuration> | |
</execution> | |
<execution> | |
<id>npm generate</id> | |
<goals> | |
<goal>npm</goal> | |
</goals> | |
<phase>prepare-package</phase> | |
<configuration> | |
<arguments>run-script generate ${project.build.finalName}</arguments> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const args = process.argv; | |
const arguments = args.slice(2); | |
const buildFolderName = arguments[0]; | |
const gwtModuleName = arguments[1]; | |
if (typeof buildFolderName === 'undefined' || !buildFolderName){ | |
console.log("please provide the name of the build folder"); | |
return; | |
} | |
const rootFolder = 'target/' + buildFolderName; | |
const fs = require('fs'); | |
var Handlebars = require('handlebars'); | |
var filesToCache = []; | |
browseAllFilesInDirectory(rootFolder); | |
function browseAllFilesInDirectory(folder) { | |
const filesRegExp = /\.(html|css|js|gif|png|jpeg)$/i; | |
const exceptions = ["WEB-INF", "META-INF", "bower_components", "clear.cache.gif"]; | |
fs.readdirSync(folder).forEach(fileName => { | |
const resource = folder + "/" + fileName; | |
if (exceptions.indexOf(fileName) < 0) { | |
if (fileName.match(filesRegExp)) { | |
filesToCache.push(resource.replace(rootFolder+"/", "")); | |
} else if (fs.lstatSync(resource).isDirectory()) { | |
browseAllFilesInDirectory(resource); | |
} | |
} | |
}); | |
} | |
var swData = { | |
cacheName: gwtModuleName || "cache_"+new Date().getTime().toString(), | |
filesToCache: filesToCache, | |
}; | |
fs.readFile("sw_template.js", "utf8", (error, data) => { | |
if (error) { | |
console.log("Unable to read template file"); | |
} | |
var template = Handlebars.compile(data); | |
var serviceWorkerJs = template(swData); | |
fs.writeFile(rootFolder+"/sw.js", serviceWorkerJs, (error) => { | |
console.log("successfully generated service worker sw.js in " + rootFolder); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cacheName = '{{cacheName}}'; | |
var filesToCache = [ {{#each filesToCache}} | |
'{{.}}'{{#unless @last}},{{/unless}} | |
{{/each}} | |
]; | |
self.addEventListener('install', function(e) { | |
console.log('[ServiceWorker] Install'); | |
e.waitUntil( | |
caches.open(cacheName).then(function(cache) { | |
console.log('[ServiceWorker] Caching app shell'); | |
return cache.addAll(filesToCache); | |
}) | |
); | |
}); | |
self.addEventListener('activate', function(e) { | |
console.log('[ServiceWorker] Activate'); | |
e.waitUntil( | |
caches.keys().then(function(keyList) { | |
return Promise.all(keyList.map(function(key) { | |
console.log('[ServiceWorker] Removing old cache', key); | |
if (key !== cacheName) { | |
return caches.delete(key); | |
} | |
})); | |
}) | |
); | |
}); | |
self.addEventListener('fetch', function(e) { | |
console.log('[ServiceWorker] Fetch', e.request.url); | |
e.respondWith( | |
caches.match(e.request).then(function(response) { | |
return response || fetch(e.request); | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey kevzlou7979,
sorry for the late reply. The error seems familiar. What maven goal are you running? It works for me using
mvn package
. Also, the frontend-maven-plugin does not look for node path in your system, but inside /nodes_modules, so in case you tried to avoid theinstall-node-and-npm
, this may cause it. I did not find enough flexibility in thefrontend-maven-plugin
to allow using the already installed npm and node. If you find a better way, please let me know. As for the code, I think this might be some misinterpretation from IntelliJ, the code works well if you trynode serviceWorkerBuilder.js
. Here is the full demo project: https://github.com/gwidgets/gwt-pwa-demo