All files / src/server/assets index.js

100% Statements 12/12
100% Branches 2/2
100% Functions 1/1
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36    2x 4x 4x 4x 4x     3x             3x 4x 4x 3x         1x             4x        
import fs from 'fs';
 
const getAssetsArray = () => {
  const assets = [];
  const assetsManifestEnv = 'SPARTACUS_ASSETS_MANIFEST_PATH';
  try {
    const assetManifest = JSON.parse(
      fs.readFileSync(process.env[assetsManifestEnv]),
    );
    const assetsManifestKeys = Object.keys(assetManifest);
 
    /**
     * Loops through the asset manifest, extracts the JS URL out of each entry and
     * injects them into the assets array, which is passed to render.
     * Loops backwards as the client bundle is output first, but needs to be last.
     */
    for (let i = assetsManifestKeys.length - 1; i >= 0; i -= 1) {
      const key = assetsManifestKeys[i];
      if (key.length > 0) {
        assets.push(assetManifest[key].js);
      }
    }
  } catch (error) {
    /* eslint-disable no-console */
    console.log(
      `Error parsing assets manifest. ${assetsManifestEnv} = ${
        process.env[assetsManifestEnv]
      }`,
    );
    /* eslint-enable no-console */
  }
  return assets;
};
 
export default getAssetsArray;