Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"pretest": "npm run build",
"test": "mocha",
"build": "rollup -c",
"prepublish": "npm run lint && npm test && npm run build"
"prepare": "npm run lint && npm test && npm run build"
},
"devDependencies": {
"buble": "^0.15.2",
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ function generateManifest(input, output) {
return JSON.stringify({[input]: output});
}

function formatFilename(dest, hash) {
function formatFilename(dest, hash, name) {
const match = pattern.exec(dest);
const length = match && match[1];
let hashResult = hash;
if (length) {
hashResult = hash.substr(0, +length);
}
return dest.replace(pattern, hashResult);
return dest
.replace(pattern, hashResult)
.replace('[name]', name);
}

function mkdirpath (dest) {
Expand Down Expand Up @@ -85,7 +87,7 @@ export default function hash(opts = {}) {
}

const hash = hasha(data.code, options);
const fileName = formatFilename(destinationOption, hash);
const fileName = formatFilename(destinationOption, hash, path.parse(builtFile).name);

if(options.replace) {
fs.unlinkSync(builtFile);
Expand Down
3 changes: 2 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"beforeEach": true,
"afterEach": true,
"before": true,
"after": true
"after": true,
"Buffer": true
}
}
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ describe('rollup-plugin-hash', () => {
});
});

it('should replace dynamic dest filename template with hash and filename of bundle', () => {
const res = hashWithOptions({ dest: 'tmp/[name]-[hash].js' });
return res.then(() => {
const tmp = fs.readdirSync('tmp');
expect(tmp).to.contain(`index-${results.sha1}`);
});
});

it('should create a sourcemap, if applicable', () => {
const res = hashWithOptions({ dest: 'tmp/[hash].js' }, { sourcemap: true });
return res.then(() => {
Expand Down