routes-loader.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * React Static Boilerplate
  3. * Copyright (c) Konstantin Tarkus | MIT License
  4. */
  5. 'use strict'; // eslint-disable-line strict
  6. var glob = require('glob');
  7. var { join } = require('path');
  8. module.exports = function() {
  9. this.cacheable();
  10. var callback = this.async();
  11. var target = this.target;
  12. var source = ['export default {'];
  13. glob('**/*.{js,jsx}', { cwd: join(__dirname, '../content') }, function(err, files) {
  14. if (err) {
  15. callback(err);
  16. return;
  17. }
  18. files.forEach(function(file) {
  19. var path = '/' + file;
  20. if (path === '/index.js' || path === '/index.jsx') {
  21. path = '/';
  22. } else if (path.endsWith('/index.js')) {
  23. path = path.substr(0, path.length - 9);
  24. } else if (path.endsWith('/index.jsx')) {
  25. path = path.substr(0, path.length - 10);
  26. } else if (path.endsWith('.js')) {
  27. path = path.substr(0, path.length - 3);
  28. } else if (path.endsWith('.jsx')) {
  29. path = path.substr(0, path.length - 4);
  30. }
  31. if (target === 'node' || path === '/404' || path === '/500') {
  32. source.push(' \'' + path + '\': () => require(\'../content/' + file + '\'),');
  33. } else {
  34. source.push(
  35. ' \'' + path + '\': async () => new Promise(resolve => { ' +
  36. 'require.ensure([\'../content/' + file + '\'], require => resolve(require(\'../content/' + file + '\'))); }),');
  37. }
  38. });
  39. source.push('};');
  40. source = source.join('\n');
  41. callback(null, source);
  42. });
  43. };