Brak opisu

rollup.config.js 2.9KB

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