daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const docGen = require('react-docgen-typescript')
  2. const docGenLoader = require('react-docgen-typescript-loader/dist/generateDocgenCodeBlock.js')
  3. const ts = require('typescript')
  4.  
  5. class DocgenPlugin {
  6.   apply(compiler) {
  7.     // emit is asynchronous hook, tapping into it using tapAsync, you can use tapPromise/tap(synchronous) as well
  8.     compiler.hooks.emit.tapAsync('DocgenPlugin', (compilation, callback) => {
  9.       const modulesToProcess = []
  10.       compilation.modules.forEach(module => {
  11.         // Skip ignored / external modules
  12.         if (!module.built || module.external || !module.rawRequest) {
  13.           return
  14.         }
  15.         if (/@sensenet.+\/src.+.tsx/.test(module.id)) {
  16.           modulesToProcess.push(module)
  17.         }
  18.       })
  19.       const tsProgram = ts.createProgram(modulesToProcess.map(v => v.userRequest), {
  20.         jsx: ts.JsxEmit.React,
  21.         module: ts.ModuleKind.CommonJS,
  22.         target: ts.ScriptTarget.Latest,
  23.       })
  24.       processModule(modulesToProcess[0], tsProgram)
  25.       callback()
  26.     })
  27.   }
  28. }
  29.  
  30. function processModule(module, tsProgram) {
  31.   const componentDocs = docGen.withDefaultConfig().parseWithProgramProvider(module.userRequest, () => tsProgram)
  32.   if (!componentDocs.length) return
  33.   console.log(module.userRequest)
  34.   const source = docGenLoader.default({
  35.     filename: module.userRequest,
  36.     source: module.userRequest,
  37.     componentDocs,
  38.     docgenCollectionName: 'STORYBOOK_REACT_CLASSES',
  39.     setDisplayName: true,
  40.   })
  41.   module._source._value = source.substring(module.userRequest.length)
  42.   console.log(module)
  43. }
  44. module.exports = DocgenPlugin
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top