You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

  1. // Common configuration for webpacker loaded from config/webpacker.yml
  2. const { dirname, join, resolve } = require('path');
  3. const { env } = require('process');
  4. const { safeLoad } = require('js-yaml');
  5. const { readFileSync } = require('fs');
  6. const glob = require('glob');
  7. const configPath = resolve('config', 'webpacker.yml');
  8. const loadersDir = join(__dirname, 'loaders');
  9. const settings = safeLoad(readFileSync(configPath), 'utf8')[env.NODE_ENV];
  10. const themeFiles = glob.sync('app/javascript/themes/*/theme.yml');
  11. const themes = {};
  12. for (let i = 0; i < themeFiles.length; i++) {
  13. const themeFile = themeFiles[i];
  14. const data = safeLoad(readFileSync(themeFile), 'utf8');
  15. if (!data.pack_directory) {
  16. data.pack_directory = dirname(themeFile);
  17. }
  18. if (data.name && data.pack) {
  19. themes[data.name] = data;
  20. }
  21. }
  22. function removeOuterSlashes(string) {
  23. return string.replace(/^\/*/, '').replace(/\/*$/, '');
  24. }
  25. function formatPublicPath(host = '', path = '') {
  26. let formattedHost = removeOuterSlashes(host);
  27. if (formattedHost && !/^http/i.test(formattedHost)) {
  28. formattedHost = `//${formattedHost}`;
  29. }
  30. const formattedPath = removeOuterSlashes(path);
  31. return `${formattedHost}/${formattedPath}/`;
  32. }
  33. const output = {
  34. path: resolve('public', settings.public_output_path),
  35. publicPath: formatPublicPath(env.ASSET_HOST, settings.public_output_path),
  36. };
  37. module.exports = {
  38. settings,
  39. themes,
  40. env,
  41. loadersDir,
  42. output,
  43. };