Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

192 righe
4.6 KiB

  1. const path = require('path');
  2. const fs = require('fs');
  3. const {
  4. get_build_json_path,
  5. get_app_path,
  6. apps_list,
  7. assets_path,
  8. get_public_path,
  9. bench_path
  10. } = require('./rollup.utils');
  11. const less = require('rollup-plugin-less');
  12. const multi_entry = require('rollup-plugin-multi-entry');
  13. const commonjs = require('rollup-plugin-commonjs');
  14. const node_resolve = require('rollup-plugin-node-resolve');
  15. const buble = require('rollup-plugin-buble');
  16. const uglify = require('rollup-plugin-uglify');
  17. const frappe_html = require('./frappe-html-plugin');
  18. const production = process.env.FRAPPE_ENV === 'production';
  19. ensure_js_css_dirs();
  20. build_libs();
  21. function get_app_config(app) {
  22. const build_map = get_build_json(app);
  23. if (!build_map) return [];
  24. const js_config = Object.keys(build_map)
  25. .filter(output_file =>
  26. output_file.endsWith('.js') &&
  27. // libs is built separately (to be deprecated)
  28. !output_file.endsWith('libs.min.js')
  29. )
  30. .map(output_file => {
  31. const input_files = build_map[output_file].map(
  32. // make paths absolute
  33. input_path => path.resolve(get_app_path(app), input_path)
  34. );
  35. return get_js_config(output_file, input_files);
  36. });
  37. const less_config = Object.keys(build_map)
  38. .filter(output_file =>
  39. output_file.endsWith('.css')
  40. )
  41. .map(output_file => {
  42. const input_files = build_map[output_file].map(
  43. input_path => path.resolve(get_app_path(app), input_path)
  44. );
  45. return get_css_config(output_file, input_files);
  46. });
  47. return [].concat(js_config, less_config);
  48. }
  49. function get_js_config(output_file, input_files) {
  50. const css_output_file = path.resolve(assets_path, 'css', path.basename(output_file).split('.js')[0] + '.css');
  51. const plugins = [
  52. // enables array of inputs
  53. multi_entry(),
  54. // .html -> .js
  55. frappe_html(),
  56. // less -> css
  57. less({
  58. output: css_output_file,
  59. option: {
  60. // so that other .less files can import variables.less from frappe directly
  61. paths: [path.resolve(get_public_path('frappe'), 'less'), path.resolve(get_app_path('frappe'), '..')],
  62. compress: production
  63. },
  64. // include: [path.resolve(bench_path, '**/*.less'), path.resolve(bench_path, '**/*.css')],
  65. exclude: []
  66. }),
  67. // ES6 -> ES5
  68. buble({
  69. objectAssign: 'Object.assign',
  70. transforms: {
  71. dangerousForOf: true
  72. },
  73. exclude: [path.resolve(bench_path, '**/*.css'), path.resolve(bench_path, '**/*.less')]
  74. }),
  75. commonjs(),
  76. node_resolve(),
  77. production && uglify()
  78. ];
  79. return {
  80. input: input_files,
  81. plugins: plugins,
  82. output: {
  83. file: path.resolve(assets_path, output_file),
  84. format: 'iife',
  85. name: 'Rollup',
  86. // globals: {
  87. // 'sortablejs': 'window.Sortable',
  88. // 'clusterize.js': 'window.Clusterize'
  89. // },
  90. sourcemap: true
  91. },
  92. context: 'window',
  93. external: ['jquery']
  94. };
  95. }
  96. function get_css_config(output_file, input_files) {
  97. const plugins = [
  98. // enables array of inputs
  99. multi_entry(),
  100. // less -> css
  101. less({
  102. output: path.resolve(assets_path, output_file),
  103. option: {
  104. // so that other .less files can import variables.less from frappe directly
  105. paths: [path.resolve(get_public_path('frappe'), 'less')],
  106. compress: production
  107. },
  108. include: [path.resolve(bench_path, '**/*.less'), path.resolve(bench_path, '**/*.css')]
  109. })
  110. ];
  111. return {
  112. input: input_files,
  113. plugins: plugins,
  114. output: {
  115. // this file is always empty, remove it later?
  116. file: path.resolve(assets_path, `css/rollup.manifest.css`),
  117. format: 'cjs',
  118. }
  119. };
  120. }
  121. function ensure_js_css_dirs() {
  122. const paths = [
  123. path.resolve(assets_path, 'js'),
  124. path.resolve(assets_path, 'css')
  125. ];
  126. paths.forEach(path => {
  127. if (!fs.existsSync(path)) {
  128. fs.mkdirSync(path);
  129. }
  130. });
  131. // clear files in css folder
  132. const css_path = path.resolve(assets_path, 'css');
  133. const files = fs.readdirSync(css_path);
  134. files.forEach(file => {
  135. fs.unlinkSync(path.resolve(css_path, file));
  136. });
  137. }
  138. function build_libs() {
  139. const libs_path = 'js/libs.min.js';
  140. const input_files = get_build_json('frappe')[libs_path];
  141. const libs_content = input_files.map(file_name => {
  142. const full_path = path.resolve(get_app_path('frappe'), file_name);
  143. return `/* ${file_name} */\n` + fs.readFileSync(full_path);
  144. }).join('\n\n');
  145. const target_path = path.resolve(assets_path, libs_path);
  146. fs.writeFileSync(target_path, libs_content);
  147. console.log('✨ Built libs.min.js'); // eslint-disable-line
  148. }
  149. function get_all_apps_config() {
  150. let configs = [];
  151. apps_list.forEach(app => {
  152. configs = configs.concat(get_app_config(app));
  153. });
  154. return configs;
  155. }
  156. function get_build_json(app) {
  157. try {
  158. return require(get_build_json_path(app));
  159. } catch (e) {
  160. // build.json does not exist
  161. return null;
  162. }
  163. }
  164. module.exports = get_all_apps_config();