rollup.config.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { readFileSync, writeFileSync } from 'fs';
  2. import resolve from '@rollup/plugin-node-resolve';
  3. import buble from '@rollup/plugin-buble';
  4. import { terser } from 'rollup-plugin-terser';
  5. import replace from '@rollup/plugin-replace';
  6. const names = ['iconify', 'iconify.without-api'];
  7. const global = 'Iconify';
  8. // Wrapper to export module as global and as ES module
  9. const header = `/**
  10. * (c) Iconify
  11. *
  12. * For the full copyright and license information, please view the license.txt or license.gpl.txt
  13. * files at https://github.com/iconify/iconify
  14. *
  15. * Licensed under MIT.
  16. *
  17. * @license MIT
  18. * @version __iconify_version__
  19. */`;
  20. const defaultFooter = `
  21. // Export to window or web worker
  22. try {
  23. if (self.Iconify === void 0) {
  24. self.Iconify = Iconify;
  25. }
  26. } catch (err) {
  27. }`;
  28. const iifeFooter = `
  29. // Export as ES module
  30. if (typeof exports === 'object') {
  31. try {
  32. exports.__esModule = true;
  33. exports.default = Iconify;
  34. for (var key in Iconify) {
  35. exports[key] = Iconify[key];
  36. }
  37. } catch (err) {
  38. }
  39. }
  40. ${defaultFooter}`;
  41. // Get replacements
  42. const replacements = {
  43. preventAssignment: true,
  44. };
  45. const packageJSON = JSON.parse(readFileSync('package.json', 'utf8'));
  46. replacements['__iconify_version__'] = packageJSON.version;
  47. // Update README.md
  48. let readme = readFileSync('README.md', 'utf8');
  49. const oldReadme = readme;
  50. const replaceCodeLink = (search) => {
  51. let start = 0;
  52. let pos;
  53. while ((pos = readme.indexOf(search, start)) !== -1) {
  54. start = pos + search.length;
  55. let pos2 = readme.indexOf('/', start);
  56. if (pos2 === -1) {
  57. return;
  58. }
  59. readme =
  60. readme.slice(0, start) + packageJSON.version + readme.slice(pos2);
  61. }
  62. };
  63. replaceCodeLink('/code.iconify.design/3/');
  64. replaceCodeLink('/@iconify/iconify@');
  65. if (readme !== oldReadme) {
  66. console.log('Updatead README');
  67. writeFileSync('README.md', readme, 'utf8');
  68. }
  69. // Export configuration
  70. const config = [];
  71. names.forEach((name) => {
  72. // Full and minified
  73. [false, true].forEach((minify) => {
  74. // Parse all formats
  75. ['js', 'cjs', 'mjs'].forEach((ext) => {
  76. if (minify && ext !== 'js') {
  77. // Minify only .js files
  78. return;
  79. }
  80. // Get export format and footer
  81. let format = ext;
  82. let footer = defaultFooter;
  83. switch (ext) {
  84. case 'js':
  85. format = 'iife';
  86. footer = iifeFooter;
  87. break;
  88. case 'mjs':
  89. format = 'es';
  90. break;
  91. }
  92. const item = {
  93. input: `lib/${name}.js`,
  94. output: [
  95. {
  96. file: `dist/${name}${minify ? '.min' : ''}.${ext}`,
  97. format,
  98. exports: 'named',
  99. name: global,
  100. banner: header,
  101. footer,
  102. },
  103. ],
  104. plugins: [
  105. resolve({
  106. browser: true,
  107. }),
  108. replace(replacements),
  109. ],
  110. };
  111. if (ext === 'js') {
  112. // Support old browsers only in .js files.
  113. // Other files are for modern browsers that don't need it or
  114. // for bundlers that should handle old browser support themselves.
  115. item.plugins.push(
  116. buble({
  117. objectAssign: 'Object.assign',
  118. })
  119. );
  120. }
  121. if (minify) {
  122. item.plugins.push(terser());
  123. }
  124. config.push(item);
  125. });
  126. });
  127. });
  128. export default config;