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.
 
 
 
 
 
 

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