Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

refactor(index): remove unnecessary delete #140

Merged
merged 1 commit into from
Aug 16, 2018

Conversation

alexander-akait
Copy link
Member

This PR contains a:

  • bugfix
  • new feature
  • code refactor
  • test update
  • typo fix
  • metadata update

Motivation / Use-Case

we don't support webpack@3 in this package

Breaking Changes

no

Additional Info

delete is very expensive operation (perf)

/cc @insin can you test this branch to avoid we fix problem #138

@alexander-akait
Copy link
Member Author

@michael-ciniawsky don't merge before @insin until it tested

@michael-ciniawsky michael-ciniawsky changed the title refactor: remove unnecessary delete refactor(index): remove unnecessary delete Aug 15, 2018
@alexander-akait
Copy link
Member Author

@michael-ciniawsky looks we do breaking change here https://github.com/webpack-contrib/url-loader/blob/master/src/utils/normalizeFallback.js#L15 we don't pass limit, mimetype options to fallback loader, but here https://github.com/webpack-contrib/url-loader/blob/master/src/utils/normalizeFallback.js#L45 do. We need fix it.

We should pass all option(s) from url-loader to fallback loader or add fallbackOptions option for fallback options and don't mix options. Example i want use one name for file-loader and other name for responsive-loader. What do you think? Maybe better solve this right now and release major than to create problems in the future for us.

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Aug 15, 2018

Can't we just accept an {Object} for options.fallback ?

{
   ...
   use: { 
      loader: 'url-loader', 
      options: {
         fallback: { 
           loader: 'responsive-loader', 
           options: { ... } // We only pass these to the custom fallback
         }
      }
   }
}

and for the default (file-loader) we pass the options like we did before ? If the fallback is a {String} we don't pass any options relying on the custom fallback loader defaults ?

@alexander-akait
Copy link
Member Author

Good idea, let' do it, if no options defined we use url-options. Should options inherit? I.e. if i defined name for url loader, should we pass name to fallback? I think yes

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Aug 15, 2018

We can do that yes, but I think we should 'cherrypick'/limit them to only the useful/required ones (e.g options.name) and not to all by default for a custom fallback loader. For the file-loader (default) we pass all of the url-loader options like before

url-loader (Inline)

webpack.config.js

{
  loader: 'url-loader'
  options: {
     name: '...',
     limit: 10000
  }
}

file-loader (fallback [default])

webpack.config.js

{
  loader: 'url-loader'
  options: {
     name: '...',
     ...,
     limit: 10
  }
}

x-loader (fallback {String} [no fallback options])

webpack.config.js

{
  loader: 'url-loader'
  options: {
     name: '...',
     limit: 10,
     fallback: 'x-loader'
  }
}

x-loader (fallback {Object} [with fallback options])

webpack.config.js

{
  loader: 'url-loader'
  options: {
     name: '...',
     limit: 10,
     // fallback.options merged with e.g options.name if typeof options.fallback === 'object'
     // and called afterwards
     fallback: { loader: 'x-loader', options: { ...custom } }
  }
}

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Aug 15, 2018

if that works (didn't tried/tested yet :))

@alexander-akait
Copy link
Member Author

@michael-ciniawsky what about const fallbackOption = Object.assign({}, urlOptions, fallbackOptions)?

  1. If no fallback - we pass url-loader options to file-loader/x-loader.
  2. If fallback string - we parse query string, merge their with url-loader options and pass to file-loader/x-loader - const fallbackOption = Object.assign({}, urlOptions, fallbackOptions).
  3. If fallback object - we get fallback options and merge their with url-loader options and pass to file-loader/x-loader - const fallbackOption = Object.assign({}, urlOptions, fallbackOptions).

We should always pass all options, but fallback options always rewrite url-loader options (example name from fallback.options always rewrite name from url-loader, same for limit). Also we should pass all options, because fallback loader also can use name/limit options.

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Aug 15, 2018

Wouldn't that mean to just use fallback.call(this, src) (L62) (this === loaderContext) again, passing all options ?

{
  loader: 'url-loader'
  options: {
    name: '[name].[ext]',
    fallback: 'x-loader',
    'x-option1': '',
    'x-option2': ''
  }
}

@alexander-akait
Copy link
Member Author

alexander-akait commented Aug 15, 2018

@michael-ciniawsky yep, just merge options: Object.assign({}, urlOptions || {}, fallbackOptions || {}). Universal way to pass all options and prefer fallback options under url-loader options

@michael-ciniawsky
Copy link
Member

Yes, but if the custom fallback loader options are declared on the url-loader options we can even avoid the need to merge them since all options are already provided and added to the loaderContext as query (?) What was the reason to add the utils/normalizeFallback 'hack' ?

@alexander-akait
Copy link
Member Author

@michael-ciniawsky for backwards compatibility since we in previous versions sent all the options, we can add comments about remove this in 2 version

@michael-ciniawsky
Copy link
Member

What do you mean by removing this (?) in version 2.0.0 :). I'm a bit confused by the utils/normalizeFallback in general (Why do we need it ?), but might be missing something. If we pass all options it should be sufficient to 'revert' to fallback.call(this, src) in src/index.js and in your webpack.config.js one simply uses

webpack.config.js

{
  loader; 'url-loader',
  options: {
      limit: 1,
      name: '',
      fallback: 'x-loader',
      any: '',
      option: '',
      you: '',
      want: '',
  }
}

like it was before, but there must be a reason why utils/normalizeFallback was added in the first place, but I don't understand 😛

@alexander-akait
Copy link
Member Author

@michael-ciniawsky i will send new PR and we continue the discusion in it, this PR we can merge

@alexander-akait alexander-akait merged commit e4d10a2 into master Aug 16, 2018
@alexander-akait alexander-akait deleted the refactor-remove-unnecessary-delete branch August 16, 2018 10:27
@michael-ciniawsky
Copy link
Member

Does #139 💯 fixed it for now ? I can open an issue and we can discuss separately since people seem to have quite some issues with we better release a patch asap :)

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Aug 16, 2018

:D you're faster, yep agreed 👍

@michael-ciniawsky michael-ciniawsky removed this from the 1.1.2 milestone Aug 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants