Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

199 wiersze
4.8 KiB

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