configuration.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Common configuration for webpacker loaded from config/webpacker.yml
  2. const { resolve } = require('path');
  3. const { env } = require('process');
  4. const { safeLoad } = require('js-yaml');
  5. const { readFileSync } = require('fs');
  6. const configPath = resolve('config', 'webpacker.yml');
  7. const settings = safeLoad(readFileSync(configPath), 'utf8')[env.RAILS_ENV || env.NODE_ENV];
  8. const themePath = resolve('config', 'themes.yml');
  9. const themes = safeLoad(readFileSync(themePath), 'utf8');
  10. function removeOuterSlashes(string) {
  11. return string.replace(/^\/*/, '').replace(/\/*$/, '');
  12. }
  13. function formatPublicPath(host = '', path = '') {
  14. let formattedHost = removeOuterSlashes(host);
  15. if (formattedHost && !/^http/i.test(formattedHost)) {
  16. formattedHost = `//${formattedHost}`;
  17. }
  18. const formattedPath = removeOuterSlashes(path);
  19. return `${formattedHost}/${formattedPath}/`;
  20. }
  21. const output = {
  22. path: resolve('public', settings.public_output_path),
  23. publicPath: formatPublicPath(env.CDN_HOST, settings.public_output_path),
  24. };
  25. module.exports = {
  26. settings,
  27. themes,
  28. env: {
  29. CDN_HOST: env.CDN_HOST,
  30. NODE_ENV: env.NODE_ENV,
  31. },
  32. output,
  33. };