No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

210 líneas
5.2 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 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 ? require('autoprefixer')() : null,
  109. starts_with_css && production ? require('cssnano')({ preset: 'default' }) : null
  110. ].filter(Boolean),
  111. extract: output_path,
  112. loaders: [less_loader],
  113. use: [
  114. ['less', {
  115. // import other less/css files starting from these folders
  116. paths: [
  117. path.resolve(get_public_path('frappe'), 'less')
  118. ]
  119. }],
  120. ['sass', {
  121. ...get_options_for_scss(),
  122. outFile: output_path,
  123. sourceMapContents: true
  124. }]
  125. ],
  126. include: [
  127. path.resolve(bench_path, '**/*.less'),
  128. path.resolve(bench_path, '**/*.scss'),
  129. path.resolve(bench_path, '**/*.css')
  130. ],
  131. sourceMap: starts_with_css && !production
  132. })
  133. ];
  134. return {
  135. inputOptions: {
  136. input: input_files,
  137. plugins: plugins,
  138. onwarn(warning) {
  139. // skip warnings
  140. if (['EMPTY_BUNDLE'].includes(warning.code)) return;
  141. // console.warn everything else
  142. log(chalk.yellow.underline(warning.code), ':', warning.message);
  143. log(warning);
  144. }
  145. },
  146. outputOptions: {
  147. // this file is always empty, remove it later?
  148. file: path.resolve(assets_path, `css/rollup.manifest.css`),
  149. format: 'cjs'
  150. }
  151. };
  152. }
  153. function get_options_for(app) {
  154. const build_json = get_build_json(app);
  155. if (!build_json) return [];
  156. return Object.keys(build_json)
  157. .map(output_file => {
  158. if (output_file.startsWith('concat:')) return null;
  159. const input_files = build_json[output_file]
  160. .map(input_file => {
  161. let prefix = get_app_path(app);
  162. if (input_file.startsWith('node_modules/')) {
  163. prefix = path.resolve(get_app_path(app), '..');
  164. }
  165. return path.resolve(prefix, input_file);
  166. });
  167. return Object.assign(
  168. get_rollup_options(output_file, input_files), {
  169. output_file
  170. });
  171. })
  172. .filter(Boolean);
  173. }
  174. function ignore_css() {
  175. return {
  176. name: 'ignore-css',
  177. transform(code, id) {
  178. if (!['.css', '.scss', '.sass', '.less'].some(ext => id.endsWith(ext))) {
  179. return null;
  180. }
  181. return `
  182. // ignored ${id}
  183. `;
  184. }
  185. };
  186. };
  187. module.exports = {
  188. get_options_for
  189. };