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

refactor: use annotation comments for source maps (options.sourceMap) #219

Merged
merged 1 commit into from
Dec 5, 2017

Conversation

mgol
Copy link
Contributor

@mgol mgol commented Apr 21, 2017

This is a rebase of PR #165 with minor modifications (e.g. a complete removal of fixUrls.js.

Fixes broken relative and protocol-relative url() use in stylesheets when
source maps are enabled. Chrome >=57 and Firefox >=50.1 support loading source
maps when sourceMapURL directive is dynamically added to a new or existing style
element in the DOM. This means that style-loader can simply add the styles and
sourceMapURL directive as text inside a style element and the source map will be
loaded by the browser. Given the simpler insertion method, the relative and
protocol-relative urls in stylesheets will not break.

Fixes #121
Ref #93

What kind of change does this PR introduce?

Bugfix + breaking change.

Did you add tests for your changes?
Not yet.

If relevant, did you update the README?
Yes.

Summary

Currently if source maps are enabled style-loader uses blob URLs instead of <style> tags with inline CSS. This makes the CSS loading asynchronous and causes subsequent JavaScript to often see the state from before applying the CSS, breaking apps (see #121). Another issue is breaking relative URLs (see #93).

Blobs were previously used to make source maps work but recent versions of major browsers have no problems with source maps working for inline <style> tags if a proper source map comment is appended.

Does this PR introduce a breaking change?

Yes. The convertToAbsoluteUrls option is removed as it's no longer needed.

Other information
I can add some tests to verify the source map comment is appended and that it has correct mapping. Is anything else needed to land it?

@mgol
Copy link
Contributor Author

mgol commented Apr 21, 2017

I set @john-bai as the author of the commit as almost the whole code is his. I can add tests in a separate commit.

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Apr 21, 2017

@mgol You mentioned Chrome && Firefox. Do you know the status in Safari/Edge ? (General browser support/limitations) since this a breaking change we need to know, how to handle eventual support for folks unable to use this immediately for some reason, or maybe (hopefully) it's widely enough supported, so we can just recommend to modify their setup in one way or the other so it works for the majority 😛

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a comment

Choose a reason for hiding this comment

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

Just one small thing, thx 👍 in advance, this PR is a really nice and needed cleanup :)

package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "style-loader",
"version": "0.16.1",
"version": "0.16.2",
Copy link
Member

@michael-ciniawsky michael-ciniawsky Apr 21, 2017

Choose a reason for hiding this comment

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

Please don't 😛, we use standard-version for release management across webpack-contrib and this is the responsibility of the respective maintainer(s)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. A bad merge, I didn't intend to mess with this field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Yep no offense I thought so 😛

@michael-ciniawsky michael-ciniawsky added this to the 1.0.0-beta.0 milestone Apr 21, 2017
@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Apr 21, 2017

@mgol Test(s) would be highly appreciated, but you mentioned it's better do it in a separate PR which is ok as long as their will be one 😛 . Otherwise browser support and how strongly this affects current users is the elefant in the room :D

@mgol
Copy link
Contributor Author

mgol commented Apr 21, 2017

@michael-ciniawsky I did some testing:

  1. In Chrome 58 & Firefox 53 the approach from this PR works perfectly. One caveat is Chrome seems to apply the source map incorrectly if the inline style with a sourcemap is attached in the HTML statically instead of being created & appended via JS. This is not a problem for style-loader, though, as it will always do it via JS. I'll report a Chrome issue later.
  2. In Safari 10.1 the blob approach works while the <style>-based one doesn't apply source maps.
  3. In Edge 15 the <style>-based approach seems to work with some caveats (see the points below). The blob-based approach doesn't apply source maps. So this scenario is opposite to the Safari one. Caveats:
    3.1. It seems to point to a place near the beginning of the file instead of the correct location... but at least it gets the file right.
    3.2. It only works if you open DevTools after the page loads. Otherwise you need to reopen DevTools to see the mappings.
  4. In IE 11 source maps are never applied.

I still think the <style>-based approach is the way to go as the blob-based one causes very serious issues. The only issue is that will disable source map support in Safari. If we want to keep such support, this would require imtroducing yet another flag to let people opt into the blob approach. This would increase the complexity of style-loader code even more, though.

Let me know what you think.

EDIT: Reported issues:

  1. Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=714101
  2. WebKit: https://bugs.webkit.org/show_bug.cgi?id=171111

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Apr 21, 2017

First thx very much for investigating on this futher 👍 , personally I would prefer a "Best used with Chrome/Firefox' 😛 note/warning in the README instead of a bloated inconvenient 'ducktape' approach in style-loader, but that's not my call to make alone :).

@michael-ciniawsky
Copy link
Member

@mgol mind taking a look at #153 aswell 😛 ?

@mgol
Copy link
Contributor Author

mgol commented Apr 21, 2017

@michael-ciniawsky does #153 influence this PR as well?

@michael-ciniawsky
Copy link
Member

@mgol I'm not 💯 about that fact

@lydell
Copy link
Contributor

lydell commented May 5, 2017

I’m using this in a project, and it seems to work just fine. Sad to see that there are merge conflicts now, though!

@michael-ciniawsky
Copy link
Member

@mgol Could you rebase one more time and change the base branch next please? :)

Fixes broken relative and protocol-relative url() use in stylesheets when
source maps are enabled. Chrome >=57 and Firefox >=50.1 support loading source
maps when sourceMapURL directive is dynamically added to a new or existing style
element in the DOM. This means that style-loader can simply add the styles and
sourceMapURL directive as text inside a style element and the source map will be
loaded by the browser. Given the simpler insertion method, the relative and
protocol-relative urls in stylesheets will not break.
@mgol mgol changed the base branch from master to next November 9, 2017 14:32
@mgol
Copy link
Contributor Author

mgol commented Nov 9, 2017

@michael-ciniawsky Done.

@alexander-akait
Copy link
Member

@mgol maybe best create new PR?

@mgol
Copy link
Contributor Author

mgol commented Nov 9, 2017

@evilebottnawi Why? It's already rebased. Do you want to have clearer conversation or something?

Note that @john-bai is still marked as the author of the commit in this PR as I based my PR on his one.

@mgol
Copy link
Contributor Author

mgol commented Nov 9, 2017

Also, perhaps some source map tests would be useful to make sure this feature works correctly. Although we won't be able to unit-test that mappings work in the browser.

@michael-ciniawsky michael-ciniawsky requested review from alexander-akait and removed request for sontek, bebraw and ekulabuhov November 11, 2017 01:50
@teriu
Copy link

teriu commented Nov 24, 2017

Any progress on getting this PR merged in?

@michael-ciniawsky michael-ciniawsky changed the title refactor: use inline styles when sourcemap is enabled refactor: use annotation comments for source maps (options.sourceMap) Dec 5, 2017
@michael-ciniawsky michael-ciniawsky merged commit 45ba5c9 into webpack-contrib:next Dec 5, 2017
@mgol mgol deleted the always-inline-styles branch December 6, 2017 15:45
Kronuz added a commit to Kronuz/UniversalWebpackTarget that referenced this pull request May 23, 2018
Fixes #165
webpack-contrib/style-loader#165

Using #219
refactor: use annotation comments for source maps (`options.sourceMap`)
webpack-contrib/style-loader#219
@kevinzang
Copy link

kevinzang commented Aug 21, 2018

Did this change get released? I was including it via the commit 45ba5c9, but starting today (2018-08-20) yarn install failed because 45ba5c9 is no longer a valid commit (fatal: reference is not a tree). The merge commit (8a17918) seems to also not be valid.

@mdekalka
Copy link

Would like to add to @kevinzang comment that downloading the latest version 0.23.1 seems does not include this fix.

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

Successfully merging this pull request may close these issues.

9 participants