Skip to content

Instantly share code, notes, and snippets.

@alexkirsz
Last active February 23, 2017 13:38
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexkirsz/333579c2a5b4db644bd5 to your computer and use it in GitHub Desktop.
Save alexkirsz/333579c2a5b4db644bd5 to your computer and use it in GitHub Desktop.
Force case sensitivity in webpack
/*
The MIT License (MIT)
Copyright (c) 2015 Alexandre Kirszenberg
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
module.exports = function ForceCaseSensitivityPlugin() {
this.plugin('normal-module-factory', function(nmf) {
nmf.plugin('after-resolve', function(data, done) {
var parentDir = path.dirname(data.resource);
var resourceName = path.basename(data.resource);
fs.readdir(parentDir, function(err, files) {
if (err) {
done(err);
}
if (files.indexOf(resourceName) === -1) {
var realName = _.find(files, function(filename) {
return filename.toLowerCase() === resourceName.toLowerCase()
});
done(new Error('ForceCaseSensitivityPlugin: `' + resourceName + '` does not match the corresponding file on disk `' + realName + '`'));
return;
}
done(null, data);
});
});
});
};
@fairchild
Copy link

for anyone else that may stumble here debugging case issues in webpack build, this gist has been put into an npm package here, https://github.com/dcousineau/force-case-sensitivity-webpack-plugin/blob/master/index.js

@olslash
Copy link

olslash commented Dec 9, 2015

Dumb but necessary: I'd like to use this at work -- would you mind really quickly telling me what license you want this used under?

@alexkirsz
Copy link
Author

Added MIT License.

@Urthen
Copy link

Urthen commented Mar 4, 2016

This (and the NPM package based on it) worked well until a developer accidentally mis-cased a directory name, not the file name.

I modified your code to recursively check path cases as well, and released that as its own package if you're interested. I added your license. Let me know if there's any changes to attribution you'd like.

Thanks!

@21paradox
Copy link

I've also create a snipped for checking file/folder caseSenstive async.(use glob)

var glob = require('glob');
var url = require('url');

var ForceCaseSensitivityPlugin = function() {

  var context = this.context;

  this.plugin('normal-module-factory', function(nmf) {

    nmf.plugin('after-resolve', function(data, done) {

      var filePathToMatch = data.resource;
      var stripPath = url.parse(filePathToMatch).pathname;

      glob(stripPath, {
          cwd: context,
          nosort: true,
          nocase: true
      }, function(err, matched) {

          if (err) {
              done(err);
          } else {

              if (matched.length === 0) {
                  done(new Error(path.relative(context, stripPath) + ' fileNot Matched'));

              } else {

                  if (stripPath === matched[0]) {
                      done(null, data);
                  } else {
                      done(new Error(path.relative(context, stripPath) + ' fileNot Matched'));
                  }
              }
          }
      });
    });
  });
};

@punmechanic
Copy link

The conversation in this gist is why I love open source.

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