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

System.js build: Fix leaking variables, improve resource resolution logic #1788

Merged
merged 4 commits into from
Sep 24, 2019

Conversation

mofux
Copy link
Contributor

@mofux mofux commented Aug 5, 2019

@@ -67,5 +71,6 @@ if (win.__stencil_cssshim) {
} else {
start();
}
}).call(window);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need call() here?
can't it be })()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I've tried initially, but it turned out that the function scope was not bound to window when using })(). This did break the embedded ES6Promise polyfill unfortunately. Using .call(window) therefore explicitly sets the function scope to the window object, making the ES6Promise polyfill work again.

@mofux
Copy link
Contributor Author

mofux commented Aug 6, 2019

I've updated the script resolution logic to first look for a script with the data-stencil-namespace property set to the namespace property configured in the stencil.config.ts. This is the most reliable way to tell the loader which script it should use to resolve the resourcesUrl from.

@manucorporat
Copy link
Contributor

@mofux I think it's called [data-namespace], not [data-stencil-namespace], no?
would you mind creating a different PR with the fix for the defer though?

Btw, would you mind adding the explanation of call(window) as comment?

Awesome job figuring all this out!

@mofux
Copy link
Contributor Author

mofux commented Aug 7, 2019

@manucorporat Sure, I'll split out the defer fix to a different PR.

The [data-stencil-namespace] attribute was introduced by myself and shouldn't be referred to anywhere outside the system.js loader itself... Do you want me to change it to [data-namespace] instead? I think data-stencil-namespace is a better name because it is scoped to stencil.

@mofux
Copy link
Contributor Author

mofux commented Aug 7, 2019

Making two PRs out of this at the same time seems... complicated 🤔 The changes of one PR would conflict with the other. I could make one PR (defer) based on the other (var leak), but then the second PR would be identical to this one 😅

I guess it's best to remove the var leak stuff from this PR, then get this one merged, and then add another PR for the var leak based on the updated master branch?

I'd like this PR to become the one that fixes the defer stuff, because the comments and referenced issues mostly talk about that topic.

@manucorporat
Copy link
Contributor

So far, I can't merge the current PR since it would break existing applications. Also what's the point of adding defer? just curious

…rces-url if data-stencil-namespace is not specified
@mofux
Copy link
Contributor Author

mofux commented Aug 9, 2019

So far, I can't merge the current PR since it would break existing applications

The changes should be fully backwards-compatible now. There is no need to specify the data-stencil-namespace attribute with the script tag, but if you do, chances that the correct script is picked are much higher 😅 If not specified it will simply continue with the old lookup logic instead.

Also what's the point of adding defer? just curious

You can use the defer or async attributes to delay execution of the script. IMO this is very useful when combining it with prerendered components because it can speed up the time to the first meaningful draw (the JS parsing can run after the page has already painted).

@manucorporat
Copy link
Contributor

@mofux I know... but modern browser (all but IE11) will use type="module" which will not even run all this code. I guess why try to optimize IE11.

ok, now you fixed it: 0bc27ec

:)

@mofux
Copy link
Contributor Author

mofux commented Aug 9, 2019

but modern browser (all but IE11) will use type="module" which will not even run all this code. I guess why try to optimize IE11.

Well, we had to opt out of the type="module" script because of two reasons:

  • It doesn't contain all the polyfills. We had a problem with Firefox ESR 60 that has no native support for customElements, so the module script failed on it.
  • The CMS of my customer doesn't support the ES6 modules (it does some magic around the scripts for it's live editing features)

IMO it is a wrong assumption to assume that the nomodule script is only used in IE11. It works perfectly fine in all other browsers as well, and there may be many reasons to prefer the nomodule script over the module script.

@mofux
Copy link
Contributor Author

mofux commented Aug 20, 2019

@manucorporat I just noticed commit 3a44def. Will this solve the leaking variables problems, or is this addressing a different problem?

@XianHain
Copy link

XianHain commented Aug 23, 2019

The changes should be fully backwards-compatible now. There is no need to specify the data-stencil-namespace attribute with the script tag, but if you do, chances that the correct script is picked are much higher 😅 If not specified it will simply continue with the old lookup logic instead.

Is there a way to check to see if the namespace is a part of the script URL? I'm seeing an issue where this code is intermittently changing the URL of a third-party script that exists on the page outside of the Stencil Web Component and changing the filename to corePath.

For example, it's changing https://foo.com/analytics.js to https://foo.com/p-80f7b62d.system.js, breaking both the web component and the third-party script.

I've opened issue #1827 with more details.

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