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.

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