generate_asset_map.js 742 B

1234567891011121314151617181920212223242526272829
  1. /*
  2. This code is included by both the server and frontend via
  3. common/assets.js
  4. When included from the server the export will be the function.
  5. When included from the frontend (via webpack) the export will
  6. be an object mapping file names to hashed file names. Example:
  7. "send_logo.svg": "send_logo.5fcfdf0e.svg"
  8. */
  9. const fs = require('fs');
  10. const path = require('path');
  11. function kv(f) {
  12. return `"${f}": require('../assets/${f}')`;
  13. }
  14. module.exports = function() {
  15. const files = fs.readdirSync(path.join(__dirname, '..', 'assets'));
  16. const code = `module.exports = {
  17. ${files.map(kv).join(',\n')}
  18. };`;
  19. return {
  20. code,
  21. dependencies: files.map(f => require.resolve('../assets/' + f)),
  22. cacheable: true
  23. };
  24. };