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

feat(index): add data-url attribute #317

Closed
wants to merge 5 commits into from

Conversation

bukuta
Copy link

@bukuta bukuta commented Apr 20, 2018

What kind of change does this PR introduce?

Feature

Did you add tests for your changes?
Yes

If relevant, did you update the README?
Yes

Summary

Add the support of dynamic attributes with placeholder as file-loader does, supply some useful informations when debugging.

It may fix the problem

Does this PR introduce a breaking change?

No breaking change

Other information
None

@jsf-clabot
Copy link

jsf-clabot commented Apr 20, 2018

CLA assistant check
All committers have signed the CLA.

index.js Outdated
@@ -12,7 +13,16 @@ module.exports = function () {};
module.exports.pitch = function (request) {
if (this.cacheable) this.cacheable();

var options = loaderUtils.getOptions(this) || {};
var options = clone(loaderUtils.getOptions(this) || {});
Copy link
Member

Choose a reason for hiding this comment

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

Don't use clone, it is decrease perf and bad practice

Copy link
Author

Choose a reason for hiding this comment

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

Ok.
clone was used because the recommend in loader-utils.
Now it is removed, and the logic is splited

@alexander-akait
Copy link
Member

Also please describe use case. Thanks!

@codecov
Copy link

codecov bot commented Apr 20, 2018

Codecov Report

Merging #317 into master will increase coverage by 0.22%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #317      +/-   ##
==========================================
+ Coverage   98.43%   98.66%   +0.22%     
==========================================
  Files           4        4              
  Lines          64       75      +11     
  Branches       21       24       +3     
==========================================
+ Hits           63       74      +11     
  Misses          1        1
Impacted Files Coverage Δ
index.js 100% <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 e4fa68a...974cabd. Read the comment docs.

@bukuta
Copy link
Author

bukuta commented Apr 23, 2018

@evilebottnawi

Some times we need some ways to find out

  • whether or not the styles processed with the right loaders and injected to dom
  • the order of the styles

The differences can be distinct.

image
vs
image

@alexander-akait
Copy link
Member

@bukuta Please accept CLA

@bukuta
Copy link
Author

bukuta commented Apr 23, 2018

@evilebottnawi OK

package.json Outdated
@@ -15,6 +15,7 @@
"options.json"
],
"dependencies": {
"clone": "^1.0.4",
Copy link
Member

Choose a reason for hiding this comment

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

Please remove this

Copy link
Author

Choose a reason for hiding this comment

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

Removed

index.js Outdated
@@ -13,6 +13,16 @@ module.exports.pitch = function (request) {
if (this.cacheable) this.cacheable();

var options = loaderUtils.getOptions(this) || {};
var context = options.context || this.rootContext || (this.options && this.options.context);
var attrs = {};
for(var key in options.attrs||{}){
Copy link
Member

Choose a reason for hiding this comment

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

space between ||

Copy link
Author

Choose a reason for hiding this comment

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

Formatted

index.js Outdated
var context = options.context || this.rootContext || (this.options && this.options.context);
var attrs = {};
for(var key in options.attrs||{}){
var name = loaderUtils.interpolateName(this,options.attrs[key],{
Copy link
Member

Choose a reason for hiding this comment

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

Can we add simple regex to detect [here]? To avoid perf problem?

Copy link
Author

Choose a reason for hiding this comment

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

I add a flag and a regexp

@@ -83,6 +96,8 @@ module.exports.pitch = function (request) {
"",
"var options = " + JSON.stringify(options),
"",
dynamicAttributesMatched ? "options.attrs = " + JSON.stringify(attrs) : "",
Copy link
Member

Choose a reason for hiding this comment

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

Just check Object.keys(attrs).length > 0

Copy link
Author

Choose a reason for hiding this comment

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

The attrs object contains all attributes plain and dynamic. and ony dynamic attributes detected, the override is need.

// Run
let expected = [
existingStyle,
`<style x-from="style.css" type="text/css">${requiredCss}</style>`
Copy link
Member

Choose a reason for hiding this comment

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

Can we add tests around nested (i.e. with context option) to ensure path is right

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

All good, just add one tests 👍

README.md Outdated
@@ -210,7 +210,7 @@ import style from './file.css'
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { attrs: { id: 'id' } } }
{ loader: 'style-loader', options: { attrs: { id: 'id' , 'x-from': '[path][name].[ext]'} } }

Choose a reason for hiding this comment

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

What about a data attribute instead of a non-standard one?

Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

@bukuta let's use by default data-from in tests and README and we can merge this

README.md Outdated
@@ -210,7 +210,7 @@ import style from './file.css'
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { attrs: { id: 'id' } } }
{ loader: 'style-loader', options: { attrs: { id: 'id' , 'x-from': '[path][name].[ext]'} } }
Copy link
Member

Choose a reason for hiding this comment

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

@bukuta let's use by default data-from in tests and README and we can merge this

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Looks very good. Thanks!

@@ -210,7 +210,7 @@ import style from './file.css'
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { attrs: { id: 'id' } } }
{ loader: 'style-loader', options: { attrs: { id: 'id' , 'data-from': '[path][name].[ext]'} } }
Copy link
Member

Choose a reason for hiding this comment

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

data-from => data-url

@@ -14,6 +14,19 @@ module.exports.pitch = function (request) {

var options = loaderUtils.getOptions(this) || {};

var attrs = {};
Copy link
Member

Choose a reason for hiding this comment

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

Use the request or this.resourcePath and shorten it instead of interpolating, we should already have the url here

var attrs = {
   'data-url': request || this.resourcePath
}

@michael-ciniawsky michael-ciniawsky changed the title feat: support dynamic attrs feat(index): add data-url attribute May 5, 2018
Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

@bukuta can you describe real use case? Why just don't use source maps?

@michael-ciniawsky
Copy link
Member

Closing due to inactivity feel free to reopen once the requested chances are addressed :)

@adriengcql
Copy link

Looks like it's almost done, can we reopen?

@m-orsh
Copy link

m-orsh commented Jan 9, 2020

Lets say I have 2 standalone apps. Each one needs to check whether the css libraries it needs have been loaded prior to loading them. If I load app 1, and this loads some css libs that app 2 also will use, without a method of identifying the css I have imported, how can I identify if the css has already been loaded? I think the only good way is with this feature.
example:
-app 1 loads bootstrap.css
-app 2 now checks whether it should load the same bootstrap.css
-there is no way of telling bootstrap.css has been loaded since it was loaded in a <style> tag and there is no good way of labelling which style tag contains bootstrap.css

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.

Add ID's to <style> tags
7 participants