├── .editorconfig ├── .gitignore ├── README.md ├── buildin └── __webpack_process.js ├── index.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![View project on npm](https://img.shields.io/npm/v/serviceworker-loader.svg?style=flat)](https://npmjs.org/package/serviceworker-loader) 2 | 3 | # ServiceWorker loader for Webpack 4 | 5 | ```bash 6 | $ npm install --save-dev serviceworker-loader 7 | ``` 8 | 9 | ## Usage 10 | 11 | [Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) 12 | 13 | ```javascript 14 | var registerServiceWorker = require("serviceworker!./sw.js"); 15 | 16 | registerServiceWorker({ scope: '/' }).then(success, error); 17 | ``` 18 | 19 | ## Credit 20 | 21 | This loader is based almost entirely on [worker-loader](https://github.com/webpack/worker-loader) by [@sokra](https://github.com/sokra). 22 | 23 | ## License 24 | 25 | MIT (http://markdalgleish.mit-license.org) 26 | -------------------------------------------------------------------------------- /buildin/__webpack_process.js: -------------------------------------------------------------------------------- 1 | exports = module.exports = new (require("events").EventEmitter); 2 | exports.title = "N/A"; 3 | exports.version = exports.arch = 4 | exports.execPath = "webpack"; 5 | exports.platform = "serviceworker"; 6 | exports.argv = ["webpack", "browser"]; 7 | exports.pid = 1; 8 | exports.nextTick = function (fn) { 9 | setTimeout(fn, 0); 10 | }; 11 | exports.cwd = function() { 12 | return "/app"; 13 | } 14 | exports.exit = exports.kill = 15 | exports.chdir = 16 | exports.umask = exports.dlopen = 17 | exports.uptime = exports.memoryUsage = 18 | exports.uvCounters = function() {}; 19 | exports.features = {}; 20 | exports.binding = function(str) { 21 | return {}; 22 | } 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var WebWorkerTemplatePlugin = require("webpack/lib/webworker/WebWorkerTemplatePlugin"); 2 | var SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin"); 3 | var path = require("path"); 4 | 5 | var loaderUtils = require("loader-utils"); 6 | module.exports = function() {}; 7 | module.exports.pitch = function(request) { 8 | if(!this.webpack) throw new Error("Only usable with webpack"); 9 | var callback = this.async(); 10 | var query = loaderUtils.parseQuery(this.query); 11 | var outputOptions = { 12 | filename: "[hash].serviceworker.js", 13 | chunkFilename: "[id].[hash].serviceworker.js", 14 | namedChunkFilename: null 15 | }; 16 | if(this.options && this.options.worker && this.options.worker.output) { 17 | for(var name in this.options.worker.output) { 18 | outputOptions[name] = this.options.worker.output[name]; 19 | } 20 | } 21 | var workerCompiler = this._compilation.createChildCompiler("serviceworker", outputOptions); 22 | workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions)); 23 | workerCompiler.apply(new SingleEntryPlugin(this.context, "!!" + request, "main")); 24 | if(this.options && this.options.worker && this.options.worker.plugins) { 25 | this.options.worker.plugins.forEach(function(plugin) { 26 | workerCompiler.apply(plugin); 27 | }); 28 | } 29 | var subCache = "subcache " + __dirname + " " + request; 30 | workerCompiler.plugin("compilation", function(compilation) { 31 | if(compilation.cache) { 32 | if(!compilation.cache[subCache]) 33 | compilation.cache[subCache] = {}; 34 | compilation.cache = compilation.cache[subCache]; 35 | } 36 | }); 37 | workerCompiler.runAsChild(function(err, entries, compilation) { 38 | if(err) return callback(err); 39 | var workerFile = entries[0].files[0]; 40 | return callback(null, "module.exports = function(options) {\n\treturn navigator.serviceWorker.register(__webpack_public_path__ + " + JSON.stringify(workerFile) + ", options);\n};"); 41 | }); 42 | } 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serviceworker-loader", 3 | "version": "0.1.0", 4 | "author": { 5 | "name": "Mark Dalgleish", 6 | "url": "http://markdalgleish.com" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/markdalgleish/serviceworker-loader.git" 11 | }, 12 | "description": "serviceworker loader module for webpack", 13 | "peerDependencies": { 14 | "webpack": ">=0.9 <2" 15 | }, 16 | "dependencies": { 17 | "loader-utils": "0.2.x" 18 | }, 19 | "licenses": [ 20 | { 21 | "type": "MIT", 22 | "url": "http://markdalgleish.mit-license.org" 23 | } 24 | ] 25 | } 26 | --------------------------------------------------------------------------------