Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const path = require('path');
  2. const fs = require('fs');
  3. const chalk = require('chalk');
  4. const log = console.log; // eslint-disable-line
  5. const multi_entry = require('rollup-plugin-multi-entry');
  6. const commonjs = require('rollup-plugin-commonjs');
  7. const node_resolve = require('rollup-plugin-node-resolve');
  8. const postcss = require('rollup-plugin-postcss');
  9. const buble = require('rollup-plugin-buble');
  10. const { terser } = require('rollup-plugin-terser');
  11. const vue = require('rollup-plugin-vue');
  12. const frappe_html = require('./frappe-html-plugin');
  13. const less_loader = require('./less-loader');
  14. const production = process.env.FRAPPE_ENV === 'production';
  15. const {
  16. apps_list,
  17. assets_path,
  18. bench_path,
  19. get_public_path,
  20. get_app_path,
  21. get_build_json,
  22. get_options_for_scss
  23. } = require('./rollup.utils');
  24. function get_rollup_options(output_file, input_files) {
  25. if (output_file.endsWith('.js')) {
  26. return get_rollup_options_for_js(output_file, input_files);
  27. } else if(output_file.endsWith('.css')) {
  28. return get_rollup_options_for_css(output_file, input_files);
  29. }
  30. }
  31. function get_rollup_options_for_js(output_file, input_files) {
  32. const node_resolve_paths = [].concat(
  33. // node_modules of apps directly importable
  34. apps_list.map(app => path.resolve(get_app_path(app), '../node_modules')).filter(fs.existsSync),
  35. // import js file of any app if you provide the full path
  36. apps_list.map(app => path.resolve(get_app_path(app), '..')).filter(fs.existsSync)
  37. );
  38. const plugins = [
  39. // enables array of inputs
  40. multi_entry(),
  41. // .html -> .js
  42. frappe_html(),
  43. // ignore css imports
  44. ignore_css(),
  45. // .vue -> .js
  46. vue.default(),
  47. // ES6 -> ES5
  48. buble({
  49. objectAssign: 'Object.assign',
  50. transforms: {
  51. dangerousForOf: true,
  52. classes: false
  53. },
  54. exclude: [path.resolve(bench_path, '**/*.css'), path.resolve(bench_path, '**/*.less')]
  55. }),
  56. commonjs(),
  57. node_resolve({
  58. customResolveOptions: {
  59. paths: node_resolve_paths
  60. }
  61. }),
  62. production && terser()
  63. ];
  64. return {
  65. inputOptions: {
  66. input: input_files,
  67. plugins: plugins,
  68. context: 'window',
  69. external: ['jquery'],
  70. onwarn({ code, message, loc, frame }) {
  71. // skip warnings
  72. if (['EVAL', 'SOURCEMAP_BROKEN', 'NAMESPACE_CONFLICT'].includes(code)) return;
  73. if ('UNRESOLVED_IMPORT' === code) {
  74. log(chalk.yellow.underline(code), ':', message);
  75. const command = chalk.yellow('bench setup requirements');
  76. log(`Cannot find some dependencies. You may have to run "${command}" to install them.`);
  77. log();
  78. return;
  79. }
  80. if (loc) {
  81. log(`${loc.file} (${loc.line}:${loc.column}) ${message}`);
  82. if (frame) log(frame);
  83. } else {
  84. log(chalk.yellow.underline(code), ':', message);
  85. }
  86. }
  87. },
  88. outputOptions: {
  89. file: path.resolve(assets_path, output_file),
  90. format: 'iife',
  91. name: 'Rollup',
  92. globals: {
  93. 'jquery': 'window.jQuery'
  94. },
  95. sourcemap: true
  96. }
  97. };
  98. }
  99. function get_rollup_options_for_css(output_file, input_files) {
  100. const output_path = path.resolve(assets_path, output_file);
  101. const starts_with_css = output_file.startsWith('css/');
  102. const plugins = [
  103. // enables array of inputs
  104. multi_entry(),
  105. // less -> css
  106. postcss({
  107. plugins: [
  108. starts_with_css && production ? require('cssnano')({ preset: 'default' }) : null
  109. ].filter(Boolean),
  110. extract: output_path,
  111. loaders: [less_loader],
  112. use: [
  113. ['less', {
  114. // import other less/css files starting from these folders
  115. paths: [
  116. path.resolve(get_public_path('frappe'), 'less')
  117. ]
  118. }],
  119. ['sass', {
  120. ...get_options_for_scss(),
  121. outFile: output_path,
  122. sourceMapContents: true
  123. }]
  124. ],
  125. include: [
  126. path.resolve(bench_path, '**/*.less'),
  127. path.resolve(bench_path, '**/*.scss'),
  128. path.resolve(bench_path, '**/*.css')
  129. ],
  130. sourceMap: starts_with_css && !production
  131. })
  132. ];
  133. return {
  134. inputOptions: {
  135. input: input_files,
  136. plugins: plugins,
  137. onwarn(warning) {
  138. // skip warnings
  139. if (['EMPTY_BUNDLE'].includes(warning.code)) return;
  140. // console.warn everything else
  141. log(chalk.yellow.underline(warning.code), ':', warning.message);
  142. log(warning);
  143. }
  144. },
  145. outputOptions: {
  146. // this file is always empty, remove it later?
  147. file: path.resolve(assets_path, `css/rollup.manifest.css`),
  148. format: 'cjs'
  149. }
  150. };
  151. }
  152. function get_options_for(app) {
  153. const build_json = get_build_json(app);
  154. if (!build_json) return [];
  155. return Object.keys(build_json)
  156. .map(output_file => {
  157. if (output_file.startsWith('concat:')) return null;
  158. const input_files = build_json[output_file]
  159. .map(input_file => {
  160. let prefix = get_app_path(app);
  161. if (input_file.startsWith('node_modules/')) {
  162. prefix = path.resolve(get_app_path(app), '..');
  163. }
  164. return path.resolve(prefix, input_file);
  165. });
  166. return Object.assign(
  167. get_rollup_options(output_file, input_files), {
  168. output_file
  169. });
  170. })
  171. .filter(Boolean);
  172. }
  173. function ignore_css() {
  174. return {
  175. name: 'ignore-css',
  176. transform(code, id) {
  177. if (!['.css', '.scss', '.sass', '.less'].some(ext => id.endsWith(ext))) {
  178. return null;
  179. }
  180. return `
  181. // ignored ${id}
  182. `;
  183. }
  184. };
  185. };
  186. module.exports = {
  187. get_options_for
  188. };