Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

149 рядки
3.7 KiB

  1. const path = require('path');
  2. const chalk = require('chalk');
  3. const log = console.log; // eslint-disable-line
  4. const multi_entry = require('rollup-plugin-multi-entry');
  5. const commonjs = require('rollup-plugin-commonjs');
  6. const node_resolve = require('rollup-plugin-node-resolve');
  7. const postcss = require('rollup-plugin-postcss');
  8. const buble = require('rollup-plugin-buble');
  9. const uglify = require('rollup-plugin-uglify');
  10. const frappe_html = require('./frappe-html-plugin');
  11. const production = process.env.FRAPPE_ENV === 'production';
  12. const {
  13. assets_path,
  14. bench_path,
  15. get_public_path,
  16. get_app_path,
  17. get_build_json
  18. } = require('./rollup.utils');
  19. function get_rollup_options(output_file, input_files) {
  20. if (output_file.endsWith('.js')) {
  21. return get_rollup_options_for_js(output_file, input_files);
  22. } else if(output_file.endsWith('.css')) {
  23. return get_rollup_options_for_css(output_file, input_files);
  24. }
  25. }
  26. function get_rollup_options_for_js(output_file, input_files) {
  27. const css_output_file = path.resolve(assets_path, 'css', path.basename(output_file).split('.js')[0] + '.css');
  28. const plugins = [
  29. // enables array of inputs
  30. multi_entry(),
  31. // .html -> .js
  32. frappe_html(),
  33. // ES6 -> ES5
  34. buble({
  35. objectAssign: 'Object.assign',
  36. transforms: {
  37. dangerousForOf: true
  38. },
  39. exclude: [path.resolve(bench_path, '**/*.css'), path.resolve(bench_path, '**/*.less')]
  40. }),
  41. commonjs(),
  42. node_resolve(),
  43. production && uglify()
  44. ];
  45. return {
  46. inputOptions: {
  47. input: input_files,
  48. plugins: plugins,
  49. context: 'window',
  50. external: ['jquery'],
  51. onwarn({ code, message, loc, frame }) {
  52. // skip warnings
  53. if (['EVAL', 'SOURCEMAP_BROKEN', 'NAMESPACE_CONFLICT'].includes(code)) return;
  54. if (loc) {
  55. log(`${loc.file} (${loc.line}:${loc.column}) ${message}`);
  56. if (frame) log(frame);
  57. } else {
  58. log(chalk.yellow.underline(code), ':', message);
  59. }
  60. }
  61. },
  62. outputOptions: {
  63. file: path.resolve(assets_path, output_file),
  64. format: 'iife',
  65. name: 'Rollup',
  66. globals: {
  67. 'jquery': 'window.jQuery'
  68. },
  69. sourcemap: true
  70. }
  71. };
  72. }
  73. function get_rollup_options_for_css(output_file, input_files) {
  74. const output_path = path.resolve(assets_path, output_file);
  75. const plugins = [
  76. // enables array of inputs
  77. multi_entry(),
  78. // less -> css
  79. postcss({
  80. extract: output_path,
  81. use: [['less', {
  82. // import other less/css files starting from these folders
  83. paths: [
  84. path.resolve(get_public_path('frappe'), 'less')
  85. ]
  86. }]],
  87. include: [path.resolve(bench_path, '**/*.less'), path.resolve(bench_path, '**/*.css')],
  88. minimize: production
  89. })
  90. ];
  91. return {
  92. inputOptions: {
  93. input: input_files,
  94. plugins: plugins,
  95. onwarn(warning) {
  96. // skip warnings
  97. if (['EMPTY_BUNDLE'].includes(warning.code)) return;
  98. // console.warn everything else
  99. log(chalk.yellow.underline(warning.code), ':', warning.message);
  100. }
  101. },
  102. outputOptions: {
  103. // this file is always empty, remove it later?
  104. file: path.resolve(assets_path, `css/rollup.manifest.css`),
  105. format: 'cjs'
  106. }
  107. };
  108. }
  109. function get_options_for(app) {
  110. const build_json = get_build_json(app);
  111. if (!build_json) return [];
  112. return Object.keys(build_json)
  113. .map(output_file => {
  114. if (output_file.endsWith('libs.min.js')) return null;
  115. const input_files = build_json[output_file]
  116. .map(input_file => {
  117. let prefix = get_app_path(app);
  118. if (input_file.startsWith('node_modules/')) {
  119. prefix = path.resolve(get_app_path(app), '..');
  120. }
  121. return path.resolve(prefix, input_file);
  122. });
  123. return Object.assign(
  124. get_rollup_options(output_file, input_files), {
  125. output_file
  126. });
  127. })
  128. .filter(Boolean);
  129. }
  130. module.exports = {
  131. get_options_for
  132. };