From 60c6b8bc17e8a95d238870cab436191d370684fe Mon Sep 17 00:00:00 2001 From: Brenden Palmer Date: Thu, 27 Jan 2022 12:19:52 -0500 Subject: [PATCH] Only add to manifest when necessary --- lib/asset-manifest-generator.js | 25 +++++++++++++++---------- node-tests/fixtures/manifests/txt.json | 3 --- node-tests/manifest-generator-test.js | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/asset-manifest-generator.js b/lib/asset-manifest-generator.js index f528c3e..2c3f6c8 100644 --- a/lib/asset-manifest-generator.js +++ b/lib/asset-manifest-generator.js @@ -7,6 +7,19 @@ var fs = require('fs-extra'); var DEFAULT_SUPPORTED_TYPES = [ 'js', 'css' ]; +/** + * Adds a bundle if it doesn't already exist + * + * @name addBundleToManifest + * @param {Object} manifest The existing manifest object + * @param {string} bundleName The bundle to add + */ +function addBundleToManifest(manifest, bundleName) { + manifest.bundles[bundleName] = manifest.bundles[bundleName] || { + assets: [] + }; +} + /** * A Broccoli plugin to generate an asset manifest from a given input tree. * You can also provide a second tree that is a tree containing an existing @@ -83,19 +96,10 @@ AssetManifestGenerator.prototype.build = function() { return manifest; } - if (bundleInfo.isNewBundle) { - if (manifest.bundles[bundleName]) { - throw new Error('Attempting to add bundle "' + bundleName + '" to manifest but a bundle with that name already exists.'); - } - - manifest.bundles[bundleName] = { - assets: [] - }; - } - // If the asset is named "dependencies.manifest.json" then we should read // the json in and set it as "dependencies" on the corresponding bundle. else if (assetName === 'dependencies.manifest.json') { + addBundleToManifest(manifest, bundleName); var dependencies = fs.readJsonSync(path.join(inputPath, entry)); manifest.bundles[bundleName].dependencies = dependencies; } @@ -109,6 +113,7 @@ AssetManifestGenerator.prototype.build = function() { let contents = fs.readFileSync(fullPath, 'utf-8'); if (contents.trim() !== '') { + addBundleToManifest(manifest, bundleName); manifest.bundles[bundleName].assets.push({ uri: generateURI(path.posix.join(prepend, entry)), type: assetType diff --git a/node-tests/fixtures/manifests/txt.json b/node-tests/fixtures/manifests/txt.json index f0f8873..9b6fe4e 100644 --- a/node-tests/fixtures/manifests/txt.json +++ b/node-tests/fixtures/manifests/txt.json @@ -13,9 +13,6 @@ "assets": [ { "uri": "/bundles/chat/assets/secret.txt", "type": "txt" } ] - }, - "shared": { - "assets": [] } } } diff --git a/node-tests/manifest-generator-test.js b/node-tests/manifest-generator-test.js index 27d8fb9..39abfd0 100644 --- a/node-tests/manifest-generator-test.js +++ b/node-tests/manifest-generator-test.js @@ -151,7 +151,7 @@ describe('manifest-generator', function() { yield output.build(); var manifest = fs.readJsonSync(output.path('asset-manifest.json')); - assert.deepEqual(manifest.bundles.chat.assets, []); + assert.strictEqual(manifest.bundles.chat, undefined); })); }); })