Нет описания

rollup.config.js 4.9KB

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