Skip to content

feat: add srcset support (<img>) #147

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

Closed
wants to merge 7 commits into from
Closed

feat: add srcset support (<img>) #147

wants to merge 7 commits into from

Conversation

ushu
Copy link

@ushu ushu commented Oct 3, 2017

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior? (You can also link to an open issue here)

srcset attribute is not loaded from the HTML content

What is the new behavior?

srcset attribute (actually the images inside the srcset declaration) is loaded

Does this PR introduce a breaking change?

  • Yes
  • No

Other information:

I did this code before realising that an old PR (#55) has been addressing the same issue.
So the code ended up being quite different.

The added value of this version is that it supports multiple qualifiers for the same source such as "100w 2x", which are useful when coding for the mobile web.

Also I am not why this feature has been removed but since I have some time his week I can iterate on the PR with some guidance.

Regards !

@jsf-clabot
Copy link

jsf-clabot commented Oct 3, 2017

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Oct 3, 2017

Codecov Report

Merging #147 into master will increase coverage by 0.32%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #147      +/-   ##
==========================================
+ Coverage   96.96%   97.29%   +0.32%     
==========================================
  Files           2        2              
  Lines          99      111      +12     
  Branches       20       22       +2     
==========================================
+ Hits           96      108      +12     
  Misses          3        3
Impacted Files Coverage Δ
lib/attributesParser.js 100% <100%> (ø) ⬆️
index.js 96.62% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b13d4c...4371750. Read the comment docs.

@cupojoe
Copy link

cupojoe commented Oct 4, 2017

What is the status of this making it to release? @michael-ciniawsky

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.

What happens if one of these asset urls doesn't exist ?
This maybe better implemented as an opt-in ?

@@ -4,13 +4,36 @@
*/
var Parser = require("fastparse");

var srcsetQualifierRegexp = /(\s+(\d+w))?(\s+(\d+x))?\s*$/;
Copy link
Member

Choose a reason for hiding this comment

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

var SRCSET_REGEXP


// allow to load several urls in srcsets, separated by commas
var start = index + strUntilValue.length;
var subMatches = value.split(/,/);
Copy link
Member

Choose a reason for hiding this comment

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

\n

var length = subMatch.length;

// remove initial spacing
var initialSpace = /^\s+/.exec(subMatch);
Copy link
Member

Choose a reason for hiding this comment

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

- var initialSpace = /^\s+/.exec(subMatch);
- var spaceLength = initialSpace ? initialSpace[0].length : 0;
var space = {
  initial: /^\s+/.exec(subMatch),
  length: this.initial ? this.initial[0].length : 0
}

var spaceLength = initialSpace ? initialSpace[0].length : 0;

// remove srcset qualifiers (2x, 110w, etc.), if any
var qualifier = srcsetQualifierRegexp.exec(subMatch);
Copy link
Member

Choose a reason for hiding this comment

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

\n

@@ -4,13 +4,36 @@
*/
Copy link
Member

Choose a reason for hiding this comment

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

Please move this into its own folder && file => ./lib/attrs/srcset.js and require('./attrs/srcset.js') it

@michael-ciniawsky
Copy link
Member

@cupojoe It's in review and needs approval from 2+ maintainers, so no release date estimate can be made yet

@joshwiens
Copy link
Member

For reference, one of those should be @hemanth - This is one of the loaders that actually has an assigned lead maintainer

Copy link
Contributor

@hemanth hemanth left a comment

Choose a reason for hiding this comment

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

@michael-ciniawsky has covered most of it, apart of that LGTM.

Copy link
Member

@joshwiens joshwiens left a comment

Choose a reason for hiding this comment

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

Agreed, should be g2g with the changes @michael-ciniawsky has already suggested.

@codecov-io
Copy link

codecov-io commented Oct 10, 2017

Codecov Report

Merging #147 into master will increase coverage by 0.57%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #147      +/-   ##
==========================================
+ Coverage   96.96%   97.54%   +0.57%     
==========================================
  Files           2        3       +1     
  Lines          99      122      +23     
  Branches       20       22       +2     
==========================================
+ Hits           96      119      +23     
  Misses          3        3
Impacted Files Coverage Δ
lib/attrs/srcset.js 100% <100%> (ø)
lib/attributesParser.js 100% <100%> (ø) ⬆️
index.js 96.62% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b13d4c...4371750. Read the comment docs.

@ushu
Copy link
Author

ushu commented Oct 10, 2017

Hey there,

I moved the code into attrs/srcset.js as requested, and also updated naming and general structure following your guidance to make the code more readable.

@michael-ciniawsky about:

What happens if one of these asset urls doesn't exist ?
This maybe better implemented as an opt-in ?

I'm not really sure... the idea in the code was really to load images from the srcset as if they were included in a src attribute and so I don't think it should accept mentions to non-existing resources.

I know of other attempts to auto-generate these responsive assets on-the-fly, but in my case I work with designers that produce these images (sometimes slightly modified for smaller res) and it is helpful to have webpack check if they are present, as it does for src.

just my two cents though,
Kind Regards

@cupojoe
Copy link

cupojoe commented Oct 13, 2017

@michael-ciniawsky @ushu
Totally agree with @ushu. Assuming the build can generate the images is not a good idea. It should respect the existing sources to be more friendly towards integration with design.

@michael-ciniawsky
Copy link
Member

Sry but I have to declined this since v1.0.0 of the loader will be a complete overhaul. You can use posthtml-loader && write your own/a plugin to achieve this

@michael-ciniawsky michael-ciniawsky removed this from the 0.6.0 milestone Jan 4, 2018
ushu added a commit to ushu/html-srcsets-loader that referenced this pull request Jan 30, 2018
Since webpack-contrib/html-loader#147 was
rejected, I changed the package name to allow to import the code in new
projects easily.
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.

7 participants