25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

240 satır
5.9 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(file, app="frappe") {
  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 === file) {
  159. if (output_file.startsWith('concat:')) return null;
  160. const input_files = build_json[output_file]
  161. .map(input_file => {
  162. let prefix = get_app_path(app);
  163. if (input_file.startsWith('node_modules/')) {
  164. prefix = path.resolve(get_app_path(app), '..');
  165. }
  166. return path.resolve(prefix, input_file);
  167. });
  168. return Object.assign(
  169. get_rollup_options(output_file, input_files), {
  170. output_file
  171. });
  172. }
  173. })
  174. .filter(Boolean);
  175. }
  176. function get_options_for(app) {
  177. const build_json = get_build_json(app);
  178. if (!build_json) return [];
  179. return Object.keys(build_json)
  180. .map(output_file => {
  181. if (output_file.startsWith('concat:')) return null;
  182. let files = build_json[output_file];
  183. if (typeof files === 'string') {
  184. files = [files];
  185. }
  186. const input_files = files
  187. .map(input_file => {
  188. let prefix = get_app_path(app);
  189. if (input_file.startsWith('node_modules/')) {
  190. prefix = path.resolve(get_app_path(app), '..');
  191. }
  192. return path.resolve(prefix, input_file);
  193. });
  194. return Object.assign(
  195. get_rollup_options(output_file, input_files), {
  196. output_file
  197. });
  198. })
  199. .filter(Boolean);
  200. }
  201. function ignore_css() {
  202. return {
  203. name: 'ignore-css',
  204. transform(code, id) {
  205. if (!['.css', '.scss', '.sass', '.less'].some(ext => id.endsWith(ext))) {
  206. return null;
  207. }
  208. return `
  209. // ignored ${id}
  210. `;
  211. }
  212. };
  213. };
  214. module.exports = {
  215. get_options_for,
  216. get_options
  217. };