npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tinydoc-plugin-react

v5.0.0

Published

Document React components and preview them in Tinydoc.

Downloads

13

Readme

tinydoc-plugin-react

This plugin extends tinydoc's JS plugin with support for React components both during the analysis phase and in the UI.

A demo can be seen here. Make sure you try out the real-time editor by clicking on the Try it! link for component modules.

Features

UI stuff:

  • a real-time editor for previewing components and trying them out
  • display pre-defined examples of your components that will be viewable at run-time in the UI by others, highlighting how to use the component and how it will look like.

Core/analysis stuff:

  • all React.createClass components will be marked as modules
  • in-depth analysis of propTypes
  • understands statics so that tinydoc will be able to properly differentiate between instance and static methods in the UI

Installation

npm install tinydoc tinydoc-plugin-js tinydoc-plugin-react

Usage

This plugin expects to be installed onto a tinydoc JS plugin so that it provides its sources with the React support:

// @file tinydoc.conf.js

var jsPlugin = require('tinydoc-plugins-js')({
  routeName: 'js',
  source: [ 'lib/components/**/*.js' ]
});

var reactPlugin = require('tinydoc-plugin-react')({
  routeName: 'js' // <- this must match the JS plugin's
});

config.plugins.push(jsPlugin);
config.plugins.push(reactPlugin);

This gives you the flexibility to, for example, run this plugin only on a subset of JS files (your component files) and leave the rest of the codebase.

Unfortunately, a bit more setup is required to make the examples show up. Please refer to the Config config page for more on tuning the plugin.

There are examples under doc/examples that show how to use a transformer if you need one (like webpack, browserify, etc.)

Compiling your component files

If you need to pre-process your sources or produce a built file so that your components can be renderable at run-time, define the compile hook to do the work necessary. See the examples below for guidance.

Example: using Webpack as a compiler

We'll write a lodash (or Handlebars, or whatever) template file that will require the component files and export them to the global with the correct names.

The template receives a single parameter as described above:

  • components - Array<{ name: String, filePath: String }>

The template might look something like this:

window.React = require('react');

<% _.forEach(components, function(component) { %>
  window['<%- component.name %>'] = require('<%- component.filePath %>');
<% }); %>

Now for configuring the plugin, we'll need to run webpack using its node api and give the absolute file path of the compiled bundle back to the plugin so it knows what to include at run-time.

var _ = require('lodash');
var webpack = require('webpack');

// our template file from above
var entryFileTemplate = _.template(
  fs.readFileSync(path.resolve(__dirname,  'reactPlugin.tmpl.js'), 'utf-8')
);

var reactPlugin = require('tinydoc-plugin-react')({
  compile: function(compiler, components, done) {
    var entryFileContents = entryFileTemplate({
      components: components
    });

    // write it a temp file and we'll feed it to webpack as an entry
    var entryFilePath = compiler.utils.writeTmpFile(entryFileContents);

    // this is where webpack will write its stuff
    var outputDir = compiler.utils.getTmpDir();
    var outputFileName = 'my-react-components.js';

    var webpackConfig = {
      entry: entryFilePath,
      output: {
        path: outputDir,
        filename: outputFileName,
      }
    };

    webpack(webpackConfig, function(fatalError, stats) {
      if (fatalError) {
        return done(fatalError);
      }

      var jsonStats = stats.toJson();

      if (jsonStats.errors.length > 0) {
        return done(jsonStats.errors);
      }

      done(null, path.join(outputDir, outputFileName));
    });
  }
});