Skip to content

Commit

Permalink
use existing manifest file (phamann#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 4, 2017
1 parent a3e7e0f commit 21cd3f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ function hasTemplate(dest) {
return /\[hash\]/.test(dest);
}

function generateManifest(input, output) {
return JSON.stringify({[input]: output});
function tryRequire(file) {
try {
return JSON.parse(fs.readFileSync(file, 'utf-8'));
} catch (err) {
if (err.code === 'ENOENT') return null;
throw err;
}
}

function formatFilename(dest, hash) {
Expand Down Expand Up @@ -84,9 +89,10 @@ export default function hash(opts = {}) {
}

if(options.manifest) {
const manifest = generateManifest(options.manifestKey || builtFile, fileName);
const manifest = tryRequire(options.manifest) || {};
manifest[options.manifestKey || builtFile] = fileName;
mkdirpath(options.manifest);
fs.writeFileSync(options.manifest, manifest, 'utf8');
fs.writeFileSync(options.manifest, JSON.stringify(manifest), 'utf8');
}

mkdirpath(fileName);
Expand Down
22 changes: 20 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ function rmDirectoryRecursive(path) {
}
};

function readJson(file) {
return JSON.parse(fs.readFileSync(file, 'utf-8'));
}

describe('rollup-plugin-hash', () => {

afterEach(() => {
Expand Down Expand Up @@ -111,7 +115,7 @@ describe('rollup-plugin-hash', () => {
const res = hashWithOptions({ dest: 'tmp/[hash].js', manifest: 'tmp/manifest.json' });
return res.then(() => {
const tmp = fs.readdirSync('tmp');
const manifest = require('./tmp/manifest.json');
const manifest = readJson('tmp/manifest.json');
expect(tmp).to.contain(results.manifest);
expect(manifest).to.be.an('object');
expect(manifest).to.have.property('tmp/index.js');
Expand All @@ -123,12 +127,26 @@ describe('rollup-plugin-hash', () => {
const res = hashWithOptions({ manifestKey: 'custom/dir/index.js', dest: 'tmp/[hash].js', manifest: 'tmp/manifestCustomInput.json' });
return res.then(() => {
const tmp = fs.readdirSync('tmp');
const manifest = require('./tmp/manifestCustomInput.json');
const manifest = readJson('tmp/manifestCustomInput.json');
expect(tmp).to.contain(results.manifestCustomInput);
expect(manifest).to.be.an('object');
expect(manifest).to.have.property('custom/dir/index.js');
expect(manifest['custom/dir/index.js']).to.equal('tmp/' + results.sha1);
});
});

it('should reuse an existing manifest file', () => {
fs.mkdirSync('tmp');
fs.writeFileSync('tmp/manifest.json', JSON.stringify({ existing: 'foo' }));

const res = hashWithOptions({ dest: 'tmp/[hash].js', manifest: 'tmp/manifest.json' });
return res.then(() => {
const manifest = readJson('tmp/manifest.json');
expect(manifest).to.have.property('tmp/index.js');
expect(manifest['tmp/index.js']).to.equal('tmp/' + results.sha1);
expect(manifest).to.have.property('tmp/index.js');
expect(manifest['existing']).to.equal('foo');
});
});

});

0 comments on commit 21cd3f4

Please sign in to comment.