diff --git a/package.json b/package.json index 9a601bf..0854637 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 83768d4..14f65e8 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -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); diff --git a/test/.eslintrc b/test/.eslintrc index 58ae91d..7c8ef8f 100644 --- a/test/.eslintrc +++ b/test/.eslintrc @@ -7,6 +7,7 @@ "beforeEach": true, "afterEach": true, "before": true, - "after": true + "after": true, + "Buffer": true } } diff --git a/test/index.js b/test/index.js index c6f2cc5..4ffa10a 100644 --- a/test/index.js +++ b/test/index.js @@ -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(() => {