Skip to content

Commit

Permalink
[#54] Unescape # in paths
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaff-weldon authored and Michael Pratt committed Feb 22, 2021
1 parent 1d11967 commit 7d05a0a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,17 @@ CaseSensitivePathsPlugin.prototype.apply = function(compiler) {
});
};

const cleanupPath = (resourcePath) => {
// Trim ? off, since some loaders add that to the resource they're attemping to load
return resourcePath.split('?')[0]
// replace escaped \0# with # see: https://github.com/webpack/enhanced-resolve#escaping
.replace('\u0000#', '#');
}

const onAfterResolve = (data, done) => {
this.primeCache(() => {
// Trim ? off, since some loaders add that to the resource they're attemping to load
let pathName = (data.createData || data).resource.split('?')[0];

let pathName = cleanupPath((data.createData || data).resource);
pathName = pathName.normalize ? pathName.normalize('NFC') : pathName;

checkFile(pathName, data, done);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/name-with-hash/#/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'foo';
1 change: 1 addition & 0 deletions test/fixtures/name-with-hash/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const testModule = require('./#/foo');
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ describe('CaseSensitivePathsPlugin', () => {
});
}

it('should handle paths with # correctly', (done) => {
const compiler = webpackCompilerAtDir('name-with-hash');

// if not done properly an exception will be thrown: Uncaught TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes
return compiler.run((err, stats) => {
if (err) done(err);
assert.strictEqual(stats.hasErrors(), false);
assert.strictEqual(stats.hasWarnings(), false);
done();
});
});

// For future reference: This test is somewhat of a race condition, these values seem to work well.
// If this test fails, sometimes just re-running will make it succeed.
it('should handle the deletion of a folder', (done) => {
Expand Down

0 comments on commit 7d05a0a

Please sign in to comment.