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.

87 lines
2.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. // Common configuration for webpacker loaded from config/webpacker.yml
  2. const { basename, dirname, extname, join, resolve } = require('path');
  3. const { env } = require('process');
  4. const { safeLoad } = require('js-yaml');
  5. const { lstatSync, readFileSync } = require('fs');
  6. const glob = require('glob');
  7. const configPath = resolve('config', 'webpacker.yml');
  8. const settings = safeLoad(readFileSync(configPath), 'utf8')[env.RAILS_ENV || env.NODE_ENV];
  9. const flavourFiles = glob.sync('app/javascript/flavours/*/theme.yml');
  10. const skinFiles = glob.sync('app/javascript/skins/*/*');
  11. const flavours = {};
  12. const core = function () {
  13. const coreFile = resolve('app', 'javascript', 'core', 'theme.yml');
  14. const data = safeLoad(readFileSync(coreFile), 'utf8');
  15. if (!data.pack_directory) {
  16. data.pack_directory = dirname(coreFile);
  17. }
  18. return data.pack ? data : {};
  19. }();
  20. for (let i = 0; i < flavourFiles.length; i++) {
  21. const flavourFile = flavourFiles[i];
  22. const data = safeLoad(readFileSync(flavourFile), 'utf8');
  23. data.name = basename(dirname(flavourFile));
  24. data.skin = {};
  25. if (!data.pack_directory) {
  26. data.pack_directory = dirname(flavourFile);
  27. }
  28. if (data.locales) {
  29. data.locales = join(dirname(flavourFile), data.locales);
  30. }
  31. if (data.pack && typeof data.pack === 'object') {
  32. flavours[data.name] = data;
  33. }
  34. }
  35. for (let i = 0; i < skinFiles.length; i++) {
  36. const skinFile = skinFiles[i];
  37. let skin = basename(skinFile);
  38. const name = basename(dirname(skinFile));
  39. if (!flavours[name]) {
  40. continue;
  41. }
  42. const data = flavours[name].skin;
  43. if (lstatSync(skinFile).isDirectory()) {
  44. data[skin] = {};
  45. const skinPacks = glob.sync(join(skinFile, '*.{css,scss}'));
  46. for (let j = 0; j < skinPacks.length; j++) {
  47. const pack = skinPacks[j];
  48. data[skin][basename(pack, extname(pack))] = pack;
  49. }
  50. } else if ((skin = skin.match(/^(.*)\.s?css$/i))) {
  51. data[skin[1]] = { common: skinFile };
  52. }
  53. }
  54. function removeOuterSlashes(string) {
  55. return string.replace(/^\/*/, '').replace(/\/*$/, '');
  56. }
  57. function formatPublicPath(host = '', path = '') {
  58. let formattedHost = removeOuterSlashes(host);
  59. if (formattedHost && !/^http/i.test(formattedHost)) {
  60. formattedHost = `//${formattedHost}`;
  61. }
  62. const formattedPath = removeOuterSlashes(path);
  63. return `${formattedHost}/${formattedPath}/`;
  64. }
  65. const output = {
  66. path: resolve('public', settings.public_output_path),
  67. publicPath: formatPublicPath(env.CDN_HOST, settings.public_output_path),
  68. };
  69. module.exports = {
  70. settings,
  71. core,
  72. flavours,
  73. env: {
  74. CDN_HOST: env.CDN_HOST,
  75. NODE_ENV: env.NODE_ENV,
  76. },
  77. output,
  78. };