build-entry.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var Components = require('../../components.json');
  2. var fs = require('fs');
  3. var render = require('json-templater/string');
  4. var uppercamelcase = require('uppercamelcase');
  5. var path = require('path');
  6. var OUTPUT_PATH = path.join(__dirname, '../../src/index.js');
  7. var IMPORT_TEMPLATE = 'import {{name}} from \'../packages/{{package}}\';';
  8. var INSTALL_COMPONENT_TEMPLATE = ' Vue.component({{name}}.name, {{name}});';
  9. var MAIN_TEMPLATE = `/* Automatic generated by './build/bin/build-entry.js' */
  10. {{include}}
  11. import locale from 'element-ui/src/locale';
  12. const install = function(Vue, opts = {}) {
  13. /* istanbul ignore if */
  14. if (install.installed) return;
  15. locale.use(opts.locale);
  16. {{install}}
  17. Vue.use(Loading.directive);
  18. Vue.prototype.$loading = Loading.service;
  19. Vue.prototype.$msgbox = MessageBox;
  20. Vue.prototype.$alert = MessageBox.alert;
  21. Vue.prototype.$confirm = MessageBox.confirm;
  22. Vue.prototype.$prompt = MessageBox.prompt;
  23. Vue.prototype.$notify = Notification;
  24. Vue.prototype.$message = Message;
  25. };
  26. /* istanbul ignore if */
  27. if (typeof window !== 'undefined' && window.Vue) {
  28. install(window.Vue);
  29. };
  30. module.exports = {
  31. version: '{{version}}',
  32. locale: locale.use,
  33. install,
  34. Loading,
  35. {{list}}
  36. };
  37. `;
  38. delete Components.font;
  39. var ComponentNames = Object.keys(Components);
  40. var includeComponentTemplate = [];
  41. var installTemplate = [];
  42. var listTemplate = [];
  43. ComponentNames.forEach(name => {
  44. var componentName = uppercamelcase(name);
  45. includeComponentTemplate.push(render(IMPORT_TEMPLATE, {
  46. name: componentName,
  47. package: name
  48. }));
  49. if (['Loading', 'MessageBox', 'Notification', 'Message'].indexOf(componentName) === -1) {
  50. installTemplate.push(render(INSTALL_COMPONENT_TEMPLATE, {
  51. name: componentName,
  52. component: name
  53. }));
  54. }
  55. if (componentName !== 'Loading') listTemplate.push(` ${componentName}`);
  56. });
  57. var template = render(MAIN_TEMPLATE, {
  58. include: includeComponentTemplate.join('\n'),
  59. install: installTemplate.join('\n'),
  60. version: process.env.VERSION || require('../../package.json').version,
  61. list: listTemplate.join(',\n')
  62. });
  63. fs.writeFileSync(OUTPUT_PATH, template);
  64. console.log('[build entry] DONE:', OUTPUT_PATH);