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

children chunks are not able to access $jscomp polyfills because code is wrapped in IIFE wrapper #123

Open
vishwanathd11 opened this issue Mar 5, 2020 · 5 comments

Comments

@vishwanathd11
Copy link

vishwanathd11 commented Mar 5, 2020

Migrating from https://github.com/roman01la/webpack-closure-compiler to this plugin.

In standard mode output wrapper is hardcoded like this outputWrapper: '(function(){%s}).call(this || window)',
Not sure how but this affects when there are dynamically loaded chunks are present. All polyfills are injected only in the main chunks and child chunks don't get the polyfills added to them. This causes $jscomp is not defined error in application.

I have already tried below compiler options without any success.
force_inject_library: 'es6_runtime'
isolation_mode: 'NONE'

I tried outputWrapper: '' in the code and that works because now code is not wrapped in IIFE anymore. $jscomp object created is available in the global scope and other chunks can see it.

Since there is no way of sending outputWrapper from outside, how can we work around this. I think bug is somewhere else; because older plugin used to add polyfills in children chunks as well which is not happening with this plugin.

I can create reproducible small project over the weekend if needed.

@vishwanathd11 vishwanathd11 changed the title children chunks dont get injected with polyfills when outputWrapper is not blank children chunks are not able to see $jscomp polyfills because code is wrapped in IIFE wrapper Mar 5, 2020
@vishwanathd11 vishwanathd11 reopened this Mar 6, 2020
@ChadKillingsworth
Copy link
Member

Child chunks are definitely not supposed to get polyfills. However this seems to point that your entry chunk isn't getting executed first.

@vishwanatharondekar
Copy link

vishwanatharondekar commented Jul 7, 2020

I created this long back. Not sure if the problem is present anymore. Entry chunk was getting executed, problem was code getting wrapped in outputWrapper causing $jscomp variable being local and not available to child chunks.

Specifically following had worked.
I tried outputWrapper: '' in the code of closure-webpack-plugin; that works because now code is not wrapped in IIFE anymore. $jscomp object created is available in the global scope and other chunks can see it.

@vishwanathd11
Copy link
Author

Can confirm that this is still there. Is there some config I am missing which can fix this?

@vishwanathd11 vishwanathd11 changed the title children chunks are not able to see $jscomp polyfills because code is wrapped in IIFE wrapper children chunks are not able to access $jscomp polyfills because code is wrapped in IIFE wrapper Jul 16, 2020
@Abdillah
Copy link

This is still exists with v2.5.0 and 20200830 closure compiler.

However this seems to point that your entry chunk isn't getting executed first.

Isn't the $jscomp defined inside the closure? It is inaccessible from the child chunks at all.

@ian-craig
Copy link
Contributor

ian-craig commented Sep 22, 2021

I'm also hitting this when there is an async function in a child chunk and out_language < 2017.

With Closure in debug mode, the exception is: ReferenceError: $$jscomp$asyncExecutePromiseGeneratorFunction$$ is not defined

The definition for that jscomp function is in the entry chunk as

(function(){ // Start of chunk outputWrapper
...
function $$jscomp$asyncExecutePromiseGeneratorFunction$$($generatorFunction$$) {
  return $$jscomp$asyncExecutePromiseGenerator$$($generatorFunction$$());
}
...
).call(this || window) // End of chunk outputWrapper

Because Closure isn't aware of this chunk outputWrapper, it's not aware that other chunks don't have access to function $$jscomp$asyncExecutePromiseGeneratorFunction$$

The outputWrapper: '' hack that @vishwanathd11 suggested was also my solution.

@ChadKillingsworth what was the original intent of this wrapper? Just to prevent polluting global scope? My only concern with dropping it is we also have assume_function_wrapper: true, but I think the fact that Webpack wraps each module in its own scope should cover this (for simple mode). If that's the case, it may be safer to not have this wrapper.

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

5 participants