Urthen / case-sensitive-paths-webpack-plugin

Enforces case sensitive paths in Webpack requires.
MIT License
428 stars 45 forks source link

check for windows-style root paths #2

Closed aggieben closed 8 years ago

aggieben commented 8 years ago

Windows root paths don't look like '/', but rather 'C:\'. This change will allow the function to end it's recursion correctly on Windows. Addresses #1

aggieben commented 8 years ago

just to clarify: I did this in collusion with @morethanfire

aggieben commented 8 years ago

Also, another possible way to do this that might be easier to understand at first glance is something like this:

    function fileExistsWithCaseSync(filepath) {
        var dir = path.dirname(filepath);
        var paths = path.parse(dir);
        //if ((dir === '/' || (os.platform() === 'win32' && dir.split(':')[1] === '\\')) || dir === '.') return;
        if ((paths.dir === paths.root) || dir === '.') return;
        var filenames = fs.readdirSync(dir);
        if (filenames.indexOf(path.basename(filepath)) === - 1) {

            // This could easily be accomplished with various requires, but I want to keep this module thin.
            var lowercasedFilenames = [];
            for (var i = 0; i < filenames.length; i++) {
                lowercasedFilenames.push(filenames[i].toLowerCase());
            }
            var index = lowercasedFilenames.indexOf(path.basename(filepath).toLowerCase());
            return path.join(dir, filenames[index]);
        }
        return fileExistsWithCaseSync(dir);
    }
Urthen commented 8 years ago

Good catch! I don't have a windows dev box so I wasn't able to test that.

I'll double-check this new version and release a patch.