No Description

rollup.config.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 { DEFAULT_EXTENSIONS } from '@babel/core';
  8. import json from "rollup-plugin-json";
  9. import autoprefixer from 'autoprefixer';
  10. import postcssUrl from 'postcss-url';
  11. import peerDepsExternal from 'rollup-plugin-peer-deps-external';
  12. import visualizer from 'rollup-plugin-visualizer';
  13. import analyze from 'rollup-plugin-analyzer';
  14. import ts from "@wessberg/rollup-plugin-ts";
  15. // import typescript from 'rollup-plugin-typescript2';
  16. // import sourcemaps from 'rollup-plugin-sourcemaps';
  17. // import url from '@rollup/plugin-url';
  18. // import less from "rollup-plugin-less";
  19. import nodeEval from 'node-eval';
  20. export function getModuleExports(moduleId) {
  21. const id = require.resolve(moduleId)
  22. const moduleOut = nodeEval(fs.readFileSync(id).toString(), id)
  23. let result = []
  24. const excludeExports = /^(default|__)/
  25. if (moduleOut && typeof moduleOut === 'object') {
  26. result = Object.keys(moduleOut)
  27. .filter(name => !excludeExports.test(name))
  28. }
  29. return result
  30. }
  31. export function getNamedExports(moduleIds) {
  32. const result = {}
  33. moduleIds.forEach( id => {
  34. result[id] = getModuleExports(id)
  35. })
  36. return result
  37. }
  38. const fs = require('fs');
  39. const path = require('path');
  40. const componentsFold = 'src/components';
  41. const cModuleNames = fs.readdirSync(path.resolve(componentsFold));
  42. const cModuleMap = cModuleNames.reduce((prev, name) => {
  43. const modulePath = `${componentsFold}/${name}`;
  44. prev[name] = `${modulePath}/index.ts`;
  45. const child = fs.readdirSync(path.resolve(modulePath));
  46. child.forEach(c => {
  47. const childFoldPath = `${modulePath}/${c}`;
  48. const isDir = fs.statSync(childFoldPath).isDirectory();
  49. if (isDir) {
  50. if (fs.existsSync(`${childFoldPath}/index.tsx`)) {
  51. prev[c] = `${childFoldPath}/index.tsx`;
  52. }
  53. if (c === 'Common' && fs.existsSync(childFoldPath) && fs.statSync(childFoldPath).isDirectory()) {
  54. const commonComponentsArr = fs.readdirSync(childFoldPath);
  55. commonComponentsArr.forEach(commonComponentFold => {
  56. const commonComponentFoldPath = `${childFoldPath}/${commonComponentFold}`;
  57. const isCommonComponentDir = fs.statSync(commonComponentFoldPath).isDirectory();
  58. if (isCommonComponentDir && fs.existsSync(`${commonComponentFoldPath}/index.tsx`)) {
  59. prev[`${commonComponentFold}`] = `${commonComponentFoldPath}/index.tsx`;
  60. }
  61. });
  62. }
  63. }
  64. });
  65. return prev;
  66. }, {});
  67. const extensions = [ ...DEFAULT_EXTENSIONS, '.js', '.jsx', '.ts', '.tsx'];
  68. export default {
  69. input: {
  70. '.': "src/lib-build.ts",
  71. ...cModuleMap
  72. },
  73. output: [
  74. {
  75. dir: "lib",
  76. sourcemap: true,
  77. entryFileNames: '[name]/index.js',
  78. exports: 'named',
  79. // file: "lib/bundle.js",
  80. format: "cjs"
  81. },
  82. {
  83. dir: "eslib",
  84. sourcemap: true,
  85. entryFileNames: '[name]/index.js',
  86. exports: 'named',
  87. // file: "lib/bundle.js",
  88. format: "es"
  89. }
  90. ],
  91. plugins: [
  92. peerDepsExternal(),
  93. resolve({
  94. mainFields: ['jsnext'],
  95. preferBuiltins: true,
  96. browser: true,
  97. extensions,
  98. }),
  99. image(),
  100. json(),
  101. // sourcemaps(),
  102. commonjs({
  103. include: /node_modules/,
  104. browser: true,
  105. namedExports: {
  106. ...getNamedExports([
  107. 'prop-types',
  108. 'draft-js',
  109. ]),
  110. 'braft-extensions/dist/emoticon': ['defaultEmoticons']
  111. },
  112. }),
  113. // url(),
  114. // less(),
  115. postcss({
  116. extensions: ['.css', '.less'],
  117. plugins: [
  118. autoprefixer(),
  119. postcssUrl({
  120. url: 'inline'
  121. }),
  122. ],
  123. modules: true,
  124. }),
  125. ts({
  126. exclude: "node_modules/**",
  127. transpiler: "babel",
  128. hook: {
  129. outputPath: (path, kind) => {
  130. return path
  131. }
  132. }
  133. }),
  134. // typescript({
  135. // include: ["*.js+(|x)", "**/*.js+(|x)", "*.ts+(|x)", "**/*.ts+(|x)"],
  136. // verbosity: 3,
  137. // clean: true,
  138. // typescript: require('typescript'),
  139. // // useTsconfigDeclarationDir: true,
  140. // rollupCommonJSResolveHack: true,
  141. // objectHashIgnoreUnknownHack: true,
  142. // }),
  143. babel({
  144. exclude: "node_modules/**",
  145. runtimeHelpers: true,
  146. extensions,
  147. }),
  148. alias({
  149. entries: [
  150. { find: '@', replacement: 'src' },
  151. { find: '@components', replacement: 'src/components' },
  152. ],
  153. }),
  154. visualizer({
  155. sourcemap: true,
  156. open: true,
  157. }),
  158. analyze(),
  159. ],
  160. onwarn: function(warning) {
  161. // Skip certain warnings
  162. // should intercept ... but doesn't in some rollup versions
  163. if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
  164. // console.warn everything else
  165. console.warn( warning.message );
  166. }
  167. };