From 816b668df63c93821a4578ba3f44f36749c9ff2d Mon Sep 17 00:00:00 2001 From: Etienne Date: Tue, 23 May 2017 23:24:00 -0400 Subject: [PATCH 1/4] Add bundle name placeholder * src/index.js: Add `[name]` to be replaced to the bundle filename. * test/index.js: Add test to check `[name]` is replaced correctly. --- src/index.js | 7 ++++--- test/index.js | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 0c45bc6..2e6c0af 100644 --- a/src/index.js +++ b/src/index.js @@ -33,8 +33,9 @@ function generateManifest(input, output) { `; } -function formatFilename(dest, hash) { - return dest.replace('[hash]', hash); +function formatFilename(dest, hash, name) { + return dest.replace('[hash]', hash) + .replace('[name]', name); } function mkdirpath (dest) { @@ -79,7 +80,7 @@ export default function hash(opts = {}) { } const hash = hasha(data.code, options); - const fileName = formatFilename(options.dest, hash); + const fileName = formatFilename(options.dest, hash, path.parse(bundle.dest).name); if(options.replace) { fs.unlinkSync(bundle.dest); diff --git a/test/index.js b/test/index.js index b0090ef..b3cb1f0 100644 --- a/test/index.js +++ b/test/index.js @@ -73,6 +73,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 support alternative hashing algorithms if configured', () => { const res = hashWithOptions({ dest: 'tmp/[hash].js', algorithm: 'md5' }); return res.then(() => { From fba1969003113c271158951778130208057788af Mon Sep 17 00:00:00 2001 From: Joe Nields Date: Thu, 17 May 2018 11:55:32 -0700 Subject: [PATCH 2/4] update to remove reference to deprecated dest field --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b5fb29d..14f65e8 100644 --- a/src/index.js +++ b/src/index.js @@ -87,7 +87,7 @@ export default function hash(opts = {}) { } const hash = hasha(data.code, options); - const fileName = formatFilename(destinationOption, hash, path.parse(bundle.dest).name); + const fileName = formatFilename(destinationOption, hash, path.parse(builtFile).name); if(options.replace) { fs.unlinkSync(builtFile); From c3b7911f854ab67279b475322242f8cfe11fb330 Mon Sep 17 00:00:00 2001 From: Joe Nields Date: Thu, 17 May 2018 11:55:51 -0700 Subject: [PATCH 3/4] fix eslint --- test/.eslintrc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 } } From ab4f2f183c5aa23115d3b0e01b237e2fa015514b Mon Sep 17 00:00:00 2001 From: Joe Nields Date: Thu, 17 May 2018 13:05:45 -0700 Subject: [PATCH 4/4] rename prepublish -> prepare --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",