Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

149 Zeilen
3.8 KiB

  1. const fs = require('fs');
  2. const path = require('path');
  3. const chalk = require('chalk');
  4. const rollup = require('rollup');
  5. const { execSync } = require('child_process');
  6. const log = console.log; // eslint-disable-line
  7. const {
  8. get_build_json,
  9. get_app_path,
  10. apps_list,
  11. run_serially,
  12. assets_path,
  13. sites_path
  14. } = require('./rollup.utils');
  15. const {
  16. get_options_for
  17. } = require('./config');
  18. const build_for_app = process.argv[2] === '--app' ? process.argv[3] : null;
  19. show_production_message();
  20. ensure_js_css_dirs();
  21. concatenate_files();
  22. create_build_file();
  23. if (build_for_app) {
  24. build_assets_for_app(build_for_app)
  25. .then(() => {
  26. run_build_command_for_app(build_for_app);
  27. })
  28. } else {
  29. build_assets_for_all_apps()
  30. .then(() => {
  31. run_build_command_for_apps()
  32. });
  33. }
  34. function build_assets_for_all_apps() {
  35. return run_serially(
  36. apps_list.map(app => () => build_assets(app))
  37. );
  38. }
  39. function build_assets_for_app(app) {
  40. return build_assets(app)
  41. }
  42. function build_assets(app) {
  43. const options = get_options_for(app);
  44. if (!options.length) return Promise.resolve();
  45. log(chalk.yellow(`\nBuilding ${app} assets...\n`));
  46. const promises = options.map(({ inputOptions, outputOptions, output_file}) => {
  47. return build(inputOptions, outputOptions)
  48. .then(() => {
  49. log(`${chalk.green('✔')} Built ${output_file}`);
  50. });
  51. });
  52. const start = Date.now();
  53. return Promise.all(promises)
  54. .then(() => {
  55. const time = Date.now() - start;
  56. log(chalk.green(`✨ Done in ${time / 1000}s`));
  57. });
  58. }
  59. function build(inputOptions, outputOptions) {
  60. return rollup.rollup(inputOptions)
  61. .then(bundle => bundle.write(outputOptions))
  62. .catch(err => {
  63. log(chalk.red(err));
  64. // Kill process to fail in a CI environment
  65. if (process.env.CI) {
  66. process.kill(process.pid)
  67. }
  68. });
  69. }
  70. function concatenate_files() {
  71. // only concatenates files, not processed through rollup
  72. const files_to_concat = Object.keys(get_build_json('frappe'))
  73. .filter(filename => filename.startsWith('concat:'));
  74. files_to_concat.forEach(output_file => {
  75. const input_files = get_build_json('frappe')[output_file];
  76. const file_content = input_files.map(file_name => {
  77. let prefix = get_app_path('frappe');
  78. if (file_name.startsWith('node_modules/')) {
  79. prefix = path.resolve(get_app_path('frappe'), '..');
  80. }
  81. const full_path = path.resolve(prefix, file_name);
  82. return `/* ${file_name} */\n` + fs.readFileSync(full_path);
  83. }).join('\n\n');
  84. const output_file_path = output_file.slice('concat:'.length);
  85. const target_path = path.resolve(assets_path, output_file_path);
  86. fs.writeFileSync(target_path, file_content);
  87. log(`${chalk.green('✔')} Built ${output_file_path}`);
  88. });
  89. }
  90. function create_build_file() {
  91. const touch = require('touch');
  92. touch(path.join(sites_path, '.build'), { force: true });
  93. }
  94. function run_build_command_for_apps() {
  95. let cwd = process.cwd();
  96. apps_list.map(app => run_build_command_for_app(app))
  97. process.chdir(cwd);
  98. }
  99. function run_build_command_for_app(app) {
  100. if (app === 'frappe') return;
  101. let root_app_path = path.resolve(get_app_path(app), '..');
  102. let package_json = path.resolve(root_app_path, 'package.json');
  103. if (fs.existsSync(package_json)) {
  104. let package = require(package_json);
  105. if (package.scripts && package.scripts.build) {
  106. console.log('\nRunning build command for', chalk.bold(app));
  107. process.chdir(root_app_path);
  108. execSync('yarn build', { encoding: 'utf8', stdio: 'inherit' });
  109. }
  110. }
  111. }
  112. function ensure_js_css_dirs() {
  113. const paths = [
  114. path.resolve(assets_path, 'js'),
  115. path.resolve(assets_path, 'css')
  116. ];
  117. paths.forEach(path => {
  118. if (!fs.existsSync(path)) {
  119. fs.mkdirSync(path);
  120. }
  121. });
  122. }
  123. function show_production_message() {
  124. const production = process.env.FRAPPE_ENV === 'production';
  125. log(chalk.yellow(`${production ? 'Production' : 'Development'} mode`));
  126. }