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.
 
 
 
 
 
 

121 lines
3.0 KiB

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