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

@joshcena/docusaurus-plugin-utils

v0.1.3

Published

A utility to build and pack docusaurus plugins

Downloads

14

Readme

docusaurus-plugin-utils

A utility to build and pack docusaurus plugins, particularly useful for TypeScript plugin authors

Motivation

As more of the official packages are migrated to TS, I start to notice duplicated logic in the packing process. Internally, a package would go through at most three steps:

  1. Compilation with tsc;
  2. Untyped files are copied to the lib folder with a node script;
  3. The theme components are transpiled with only types stripped, and then formatted, so that it's usable for JS swizzling.

Not following this long process of compiling -> copying static assets -> double transpilation can cause some troubles.

The first one is less of a concern, although more likely to occur: when the theme folder contains .css files, they are not copied to the dist folder by tsc, you would either need to transpile by babel with allExtensions: true, or you would need to use the copyUntypedFiles script (which we use a lot—duplicated in every package). Neither is very convenient for a TS plugin developer. However, the plugin author cannot get away without using one of the two techniques, because missing .css files would make the code not run at all.

The second one can be more problematic. The TypeScript theme components are transpiled twice, once only with the types stripped and once to actual commonjs code. Now imagine the following case:

  1. The developer of a plugin writes the plugin in TS;
  2. Unaware of the double-transpile workflow, the plugin is only built with tsc and distributed;
  3. A user uses the plugin, and decided to swizzle a theme component, but uses the JS version;
  4. The user will receive an unreadable component.

This can go away unnoticed because for normal users who don't swizzle, the plugin runs correctly. The above case is very rare as of now, but can be likely in the future with a better plugin ecosystem. Also, many official plugins have their own theme components, and most of them are currently kept as JS to bypass this issue—still not optimal for TS-perfectionists like me. If we can expose a utility to transpile the theme components to human-readable JS automatically, things would be a lot easier for the plugin authors.

And that's how this package comes to existence.

CLI

docusaurus-plugin build

Builds your plugin and transpiles theme components to human-readable JS for swizzling.

Options:

  • --source-dir: the source directory of your TypeScript plugin. Defaults to src.
  • --target-dir: the target directory of transpilation output. Defaults to lib.
  • --theme-dir: the directory of your theme components. Defaults to src/theme. If the directory is not present, we assume that no theme components are present.
  • --theme-target-dir: the directory to output the human-readable JS components. Defaults to lib/js-theme.
  • --ignore: a list of patterns to be ignored—no compilation output will be emitted. Defaults to **/__tests__/**. Note: .d.ts files will always be ignored, regardless of options used.

Usage:

yarn docusaurus-plugin build --theme-dir theme

Note: this command assumes you have Prettier installed—you should really consider about the readability of your JS components, because Babel output isn't really well-formatted.

Note: I haven't incorporated typechecking in this command (because the TS compiler API is so mysteriously hard). You would need to typecheck yourself with tsc --noEmit.

docusaurus-plugin watch

Starts watch-mode compilation.

Options:

  • --source-dir: the source directory of your TypeScript plugin. Defaults to src.
  • --target-dir: the target directory of transpilation output. Defaults to lib.
  • --ignore: a list of patterns to be ignored—no compilation output will be emitted. Defaults to **/__tests__/**. Note: .d.ts files will always be ignored, regardless of options used.

Usage:

yarn docusaurus-plugin watch

Note: running watch and then terminating it is not equivalent to running build—we don't specially transpile & format theme components due to performance concerns.