Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to have self.__WB_MANIFEST available in another file #2401

Closed
novaknole opened this issue Mar 11, 2020 · 7 comments
Closed

how to have self.__WB_MANIFEST available in another file #2401

novaknole opened this issue Mar 11, 2020 · 7 comments

Comments

@novaknole
Copy link

novaknole commented Mar 11, 2020

Library Affected:
InjectManifest plugin

Browser & Platform:
Latest.

Issue or Feature Request Description:

@jeffposnick @philipwalton

I am using InjectManifest plugin. All seems great. I have the latest workbox plugin which means InjectManifest has childCompiler and all that stuff already implemented.

Now, what I need is I want self.__WB_MANIFEST to be also available in one of my other files such as app.js which is the entry point. So the flow is like this: app.js is the main entry in webpack, whatever that file imports gets built and in the plugins of webpack, as I said I also have InjectManifest plugin. What I need is that if in app.js, I write js console.log(self.__WB_MANIFEST), I want it to print a manifest too.

The reason is that I want to use that manifest for other situations as well in my src code. I found webpackCompilationPlugins this option, but I couldn't make it work as i don't have manifest in one of the plugins I might put in that option.

ONE POSSIBLE SOLUTION:

const manifest = []; // this code is somewhere at the top of the webpack.

manifestTransforms: [
     (manifestEntry) => {
            manifest = manifestEntry
     }
],
            
webpackCompilationPlugins:[
      {
            apply (compiler) {
                 compiler.hooks.emit.tapPromise('bla', compilation => {
                      console.log(manifest); // I have the manifest.
                   
                    // I can loop through the compilation.assets and get js source file of main.js and replace 
                    //self.__WB_MANIFEST  with the above console.log(manifest);
                 })
             },
       },
 ]

The actual thing is, hooks.emit.tap never runs. apply function runs, but not the hooks. and emit hook is the one i really need. I tried shouldEmit , but still no luck. Any idea? make hook works, but at that time, i don't have manifest. All the hooks are running including make, but after make hook, none of them really get run.

@jeffposnick
Copy link
Contributor

Does this example help?

#2383 (comment)

@novaknole
Copy link
Author

webpackCompilationPlugins:[
                {
                    apply (compiler) {
                        compiler.hooks.emit.tapPromise('ManifestWorkboxWebpackPlugin', async compilation => {
                            console.log("FIRST ONE");
                        })
                    
                    }
                },
            ]

console.log(FIRST ONE) never gets printed while building the project. That's the issue I guess before we move into another issue.

@jeffposnick
Copy link
Contributor

The fix that was just merged in #2400 may also be relevant to this use case.

I'd imagine we'll cut a v5.1.0 release within the week that includes it.

@novaknole
Copy link
Author

This might be it. what do you suggest me to do for now to figure this out ?

@novaknole
Copy link
Author

I added this code in inject-manifest.js in node_modules.

if (Array.isArray(this.config.webpackCompilationPlugins)) {
      console.log("this is array");
      for (const plugin of this.config.webpackCompilationPlugins) {
        console.log(plugin);
        plugin.apply(childCompiler);
      }
    }

but still, my emit hook not gets called. :(

@novaknole
Copy link
Author

I put the apply plugin out of the webpackCompilationPlugins option. as just a ordinary plugin and its hooks start working. I guess I will leave this like this. I am just curious if you can think of any other solution than that for just putting self.__WB_MANIFEST in my other file , app.js. Is my way a good way in your opinion from the perspective of workbox ?

@jeffposnick
Copy link
Contributor

Let's move this conversation over to #2383 (comment), which tracks an "official" solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants