Nenhuma descrição

rollup.config.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import resolve from "@rollup/plugin-node-resolve";
  2. import commonjs from "@rollup/plugin-commonjs";
  3. import image from '@rollup/plugin-image';
  4. import alias from '@rollup/plugin-alias';
  5. import postcss from 'rollup-plugin-postcss-modules'
  6. import babel from "rollup-plugin-babel";
  7. import json from "rollup-plugin-json";
  8. import autoprefixer from 'autoprefixer';
  9. import postcssUrl from 'postcss-url';
  10. import peerDepsExternal from 'rollup-plugin-peer-deps-external';
  11. import visualizer from 'rollup-plugin-visualizer';
  12. import analyze from 'rollup-plugin-analyzer';
  13. // import url from '@rollup/plugin-url';
  14. // import less from "rollup-plugin-less";
  15. import nodeEval from 'node-eval';
  16. export function getModuleExports(moduleId) {
  17. const id = require.resolve(moduleId)
  18. const moduleOut = nodeEval(fs.readFileSync(id).toString(), id)
  19. let result = []
  20. const excludeExports = /^(default|__)/
  21. if (moduleOut && typeof moduleOut === 'object') {
  22. result = Object.keys(moduleOut)
  23. .filter(name => !excludeExports.test(name))
  24. }
  25. return result
  26. }
  27. export function getNamedExports(moduleIds) {
  28. const result = {}
  29. moduleIds.forEach( id => {
  30. result[id] = getModuleExports(id)
  31. })
  32. return result
  33. }
  34. const fs = require('fs');
  35. const path = require('path');
  36. const componentFold = 'src/components';
  37. const cModuleNames = fs.readdirSync(path.resolve(componentFold));
  38. const cModuleMap = cModuleNames.reduce((prev, name) => {
  39. prev[name] = `${componentFold}/${name}/index.ts`;
  40. return prev;
  41. }, {});
  42. const extensions = ['.js', '.jsx', '.ts', '.tsx'];
  43. export default {
  44. input: {
  45. '.': "src/lib-build.js",
  46. ...cModuleMap
  47. },
  48. output: [
  49. {
  50. dir: "lib",
  51. sourcemap: true,
  52. entryFileNames: '[name]/index.js',
  53. exports: 'named',
  54. // file: "lib/bundle.js",
  55. format: "cjs"
  56. },
  57. {
  58. dir: "eslib",
  59. sourcemap: true,
  60. entryFileNames: '[name]/index.js',
  61. exports: 'named',
  62. // file: "lib/bundle.js",
  63. format: "es"
  64. }
  65. ],
  66. plugins: [
  67. peerDepsExternal(),
  68. resolve({
  69. mainFields: ['jsnext'],
  70. preferBuiltins: true,
  71. browser: true,
  72. extensions,
  73. }),
  74. commonjs({
  75. include: 'node_modules/**',
  76. browser: true,
  77. namedExports: getNamedExports([
  78. 'prop-types',
  79. ])
  80. }),
  81. babel({
  82. exclude: "node_modules/**",
  83. runtimeHelpers: true,
  84. extensions,
  85. }),
  86. image(),
  87. json(),
  88. // url(),
  89. // less(),
  90. postcss({
  91. extensions: ['.css', '.less'],
  92. plugins: [
  93. autoprefixer(),
  94. postcssUrl({
  95. url: 'inline'
  96. }),
  97. ],
  98. modules: true,
  99. }),
  100. alias({
  101. entries: [
  102. { find: '@', replacement: 'src' },
  103. { find: '@components', replacement: 'src/components' },
  104. ]
  105. }),
  106. visualizer({
  107. sourcemap: true,
  108. open: true,
  109. }),
  110. analyze(),
  111. ],
  112. onwarn: function(warning) {
  113. // Skip certain warnings
  114. // should intercept ... but doesn't in some rollup versions
  115. if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
  116. // console.warn everything else
  117. console.warn( warning.message );
  118. }
  119. };