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.
 
 
 
 
 
 

153 rader
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 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 minimize_css = output_path.startsWith('css/') && production;
  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. }], 'sass'],
  87. include: [
  88. path.resolve(bench_path, '**/*.less'),
  89. path.resolve(bench_path, '**/*.scss'),
  90. path.resolve(bench_path, '**/*.css')
  91. ],
  92. minimize: minimize_css
  93. })
  94. ];
  95. return {
  96. inputOptions: {
  97. input: input_files,
  98. plugins: plugins,
  99. onwarn(warning) {
  100. // skip warnings
  101. if (['EMPTY_BUNDLE'].includes(warning.code)) return;
  102. // console.warn everything else
  103. log(chalk.yellow.underline(warning.code), ':', warning.message);
  104. }
  105. },
  106. outputOptions: {
  107. // this file is always empty, remove it later?
  108. file: path.resolve(assets_path, `css/rollup.manifest.css`),
  109. format: 'cjs'
  110. }
  111. };
  112. }
  113. function get_options_for(app) {
  114. const build_json = get_build_json(app);
  115. if (!build_json) return [];
  116. return Object.keys(build_json)
  117. .map(output_file => {
  118. if (output_file.endsWith('libs.min.js')) return null;
  119. const input_files = build_json[output_file]
  120. .map(input_file => {
  121. let prefix = get_app_path(app);
  122. if (input_file.startsWith('node_modules/')) {
  123. prefix = path.resolve(get_app_path(app), '..');
  124. }
  125. return path.resolve(prefix, input_file);
  126. });
  127. return Object.assign(
  128. get_rollup_options(output_file, input_files), {
  129. output_file
  130. });
  131. })
  132. .filter(Boolean);
  133. }
  134. module.exports = {
  135. get_options_for
  136. };