oliviertassinari / serviceworker-webpack-plugin

Simplifies creation of a service worker to serve your webpack bundles. :recycle:
https://oliviertassinari.github.io/serviceworker-webpack-plugin/
MIT License
458 stars 76 forks source link

Cannot get the plugin to load the sw.js file inside dist folder #84

Open titanve opened 6 years ago

titanve commented 6 years ago

Hello everyone!

Can you please help me telling me what is the difference between entry: " ..." and filename: " ..." ?

I've been trying since yesterday to config the plugin but it's been impossible.

My sw_cached_pages.js is in the same folder of the index.js and webpack puts the bundles inside /distfolder

What is the right config in order to load my service worker file and get the serviceWorkerOption variable?

My folder structure is: +- /spa --> |- /dist --> --> |- 1.bundle.js --> --> |- bundle.js --> --> |- sw.js <--- 'Generated by the plugin with the bundles list' --> |- index.js --> |- webpack.config.js --> |- sw_cached_pages.js

My plugin config:

new ServiceWorkerWebpackPlugin({
      // entry: path.join(__dirname, "/sw.js"),
      entry: "sw_cached_pages.js",
      // entry: "sw.js",
      filename: "sw_cached_pages.js",
      // excludes: ["**/.*", "**/*.map", "*.html"]
      excludes: ["**/.*", "**/*.map", "*.html"],
      publicPath: "/application/single_pages/visitas/"
    })

Thank you in advance!

Arseniy-II commented 6 years ago

Hello @titanve! entry: "./pass/to/your/sw/on/project/befor/compilation" - it is where webpack will try to find your file. in Your case it should be entry: path.join(__dirname, 'sw_cached_pages.js') (don't forget to const path = require('path'))

filename: 'name-of-the-file-after-webpack-compilation' - after webpack compilation it create file wit name you presented here.

You can check my answer on stack overflow for mor explanation on how to register sw.js correctly. :)