Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ const staticify = (root, options) => {

const fileName = path.basename(p);
const fileNameParts = fileName.split('.');
const fileNameHashPosition = fileNameParts.length - 2;
const fileNameHash = fileNameParts[fileNameHashPosition];
const re = new RegExp(`^[0-9a-f]{${HASH_LEN}}$`, 'i');
const reResult = re.exec(fileNameHash);

if (fileNameParts.length >= 3 &&
fileNameParts[fileNameParts.length - 2].length === HASH_LEN &&
re.exec(fileNameParts[fileNameParts.length - 2])[0] === fileNameParts[fileNameParts.length - 2]
if (fileNameParts.length >= 3 && fileNameHash.length === HASH_LEN &&
(reResult && reResult[0] === fileNameHash)
) {
const stripped = fileNameParts.slice(0, fileNameParts.length - 2);
const stripped = fileNameParts.slice(0, fileNameHashPosition);

stripped.push(fileNameParts[fileNameParts.length - 1]);

Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ describe('.stripVersion', () => {
staticify(ROOT).stripVersion(path.normalize('/script.js')).should.equal(path.normalize('/script.js'));
});

it('should not fail when the path contains a 7 character string that is not a hash', () => {
staticify(ROOT).stripVersion(path.normalize('/script.abcdefg.html')).should.equal(path.normalize('/script.abcdefg.html'));
});

it('should strip the (long) hash from a path when necessary', () => {
staticify(ROOT, {shortHash: false}).stripVersion(path.normalize('/script.4e2502b01a4c92b0a51b1a5a3271eab6.js')).should.equal(path.normalize('/script.js'));
staticify(ROOT, {shortHash: false}).stripVersion(path.normalize('/script.js')).should.equal(path.normalize('/script.js'));
});

it('should not fail when the path contains a 32 character string that is not a hash', () => {
staticify(ROOT).stripVersion(path.normalize('/script.abcdefgabcdefgabcdefgabcdefgabcd.html')).should.equal(path.normalize('/script.abcdefgabcdefgabcdefgabcdefgabcd.html'));
});
});

describe('.getVersionedPath', () => {
Expand Down