Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

204 rader
4.9 KiB

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