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

path.extname for extension checks will break with webpack loaders #17

Closed
Pomax opened this issue Jan 20, 2016 · 2 comments
Closed

path.extname for extension checks will break with webpack loaders #17

Pomax opened this issue Jan 20, 2016 · 2 comments

Comments

@Pomax
Copy link

Pomax commented Jan 20, 2016

The use of path.extname to extract the extension of a require (done here) is actually an inherent bug, as path does the extension extract based on the assumption that the entire string is a pure filename, whereas node requires don't have to conform to these. Webpack loaders take advantage of this, so you can do something like this:

require("file!basefile.txt?name=monkey.d.luffy");

Which will copy the "basefile.txt" resource as the file "monkey.d.luffy" somewhere else. In strings like these, path cannot extract the extension, as it assumes the strings is a true file path rather than a URI, and so it will find .luffy as extension.

It would probably worth to at the very least strip off URI arguments, using something like

function shouldCompile(filename, exts, matcher, ignoreNodeModules) {
  if (typeof filename !== 'string') return false;
  // strip URI arguments if necessary (used by webpack etc.)
  var qpos = filename.indexOf('?');
  if (qpos > -1) filename = filename.substring(0,qpos);
  if (exts.indexOf(path.extname(filename)) === -1) return false;
  filename = path.resolve(filename);
  if (ignoreNodeModules && nodeModulesRegex.test(filename)) return false;
  if (matcher && typeof matcher === 'function') return !!matcher(filename);
  return true;
}```
@ariporad
Copy link
Collaborator

@Pomax: Ok, give me a sec

@danez
Copy link
Owner

danez commented Sep 3, 2017

This seems fixed in #19

@danez danez closed this as completed Sep 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants