How to add custom code to the generated working file

I use Webpack and swPrecache. swPrecache creates the service-worker.js file for me, and now I want to add my own code to the generated file.

I created a Webpack plugin that adds the converted Babel code to the service-worker.js file, for example:

swPrecache.write(filePath, options, (function(err) { // If we want to append an additional file. if (options.appendFile) { var sync = fs.readFile(options.appendFile, (error, code) => { if (error) { throw error; } let transformed = babel.transform(code, options.babelConfig || {}); fs.appendFile(filePath, transformed.code); }); } callback(err); })); 

This, however, does not include code to make require() work in the file I am adding. I believe that I need to use Webpack to create the service-worker.js file, but I'm not sure how to do this, perhaps through a custom entry point?

+5
source share
1 answer

To include your custom script in the generated service, it is recommended that you use the importScripts option in the sw-precache config.You can be found in more detail here .

+1
source

All Articles