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

babel-plugin-extend-docs

v0.5.3

Published

Babel plugin which rewrites imports and templates according to a configuration

Downloads

22

Readme

Node Tools >> Babel Extend Docs >> Overview ||10

A plugin which rewrites imports and templates according to a configuration. This enables the reuse of existing documentation from source packages while still using your extensions code.

Installation

npm i -D babel-plugin-extend-docs

We want to only execute babel-plugin-extend-docs on the actual files we want to modify/extend.

You may also consider using babel overrides.

👉 web-dev-server.config.js

import path from 'path';
import { fromRollup } from '@web/dev-server-rollup';
import rollupBabel from '@rollup/plugin-babel';

const extendDocsConfig = {
  changes: [
    // possible changes as described below
  ],
};

// note that you need to use `.default` for babel
const babel = fromRollup(rollupBabel.default);

export default {
  nodeResolve: true,
  plugins: [
    babel({
      include: ['./glob/to/files/**/*.js'],
      plugins: [[path.resolve('./'), extendDocsConfig]],
    }),
  ],
};

Features

  • Renames named imports and all it's usage
  • Adjusts import paths
  • Replace tags in template literals

A Change

A change is what gets placed between in the extendDocsConfig within the changes array.

automating the generation of changes is optional but encouraged

It has the following possibilities:

changes: [
  {
    description: 'MyCounter', // not needed but can be added for easier reading of the config
    variable: {
      // see below
    },
    tag: {
      // see below
    },
  },
];

Paths

Both variable and tag are required to have a paths array which defines how to remap import paths. Generally it should be a single entry.

paths: [
  { from: 'source-pkg/counter', to: 'extension-pkg/counter' },
],

Replacement of tags

We have an existing demo code which we want to reuse.

import { LitElement, html } from '@lion/core';
import 'source-pkg/counter/define';
class MyApp extends LitElement {
  render() {
    return html`
      <h1>Example App</h1>
      <source-counter></source-counter>
    `;
  }
}
customElements.define('my-app', MyApp);

We created a "better" version of <source-counter> so we would like to use that in the demo. Our extension is called <extension-counter> and is available via extension-pkg/counter/define.

Within babel-plugin-extend-docs we can define to replace the tag + it's import.

tag: {
  from: 'source-counter',
  to: 'extension-counter',
  paths: [{ from: 'source-pkg/counter/define', to: 'extension-pkg/counter/define' }],
}

Result of Replacement of tags

import { LitElement, html } from '@lion/core';
import 'extension-pkg/counter/define';
class MyApp extends LitElement {
  render() {
    return html`
      <h1>Example App</h1>
      <extension-counter></extension-counter>
    `;
  }
}
customElements.define('my-app', MyApp);

Replacement of classes

We have an existing demo code which we want to reuse.

import { LitElement, html } from '@lion/core';
import { SourceCounter } from 'source-pkg/counter';
class TenCounter extends SourceCounter {
  inc() {
    this.count += 10;
  }
}
customElements.define('ten-counter', TenCounter);

class MyApp extends LitElement {
  render() {
    return html`
      <h1>Example App</h1>
      <ten-counter></ten-counter>
    `;
  }
}
customElements.define('my-app', MyApp);

We created a "better" version of SourceCounter so we would like that TenCounter now extends it instead.

Within babel-plugin-extend-docs we can define to replace the class + it's import.

variable: {
  from: 'SourceCounter',
  to: 'ExtensionCounter',
  paths: [
    { from: 'source-pkg/counter', to: 'extension-pkg/counter' },
  ],
},

Result of Replacement of classes

import { LitElement, html } from '@lion/core';
import { SourceCounter } from 'extension-pkg/counter';
class TenCounter extends SourceCounter {
  inc() {
    this.count += 10;
  }
}
customElements.define('ten-counter', TenCounter);
class MyApp extends LitElement {
  render() {
    return html`
      <h1>Example App</h1>
      <ten-counter></ten-counter>
    `;
  }
}
customElements.define('my-app', MyApp);

Full Demo & Api Example

You can run the example locally via npm run start or look at its source code. Note we are configuring babel via the server.config.mjs

👉 server.config.mjs

import path from 'path';
import { fromRollup } from '@web/dev-server-rollup';
import rollupBabel from '@rollup/plugin-babel';

const extendDocsConfig = {
  changes: [
    {
      name: 'SourceCounter',
      variable: {
        from: 'SourceCounter',
        to: 'ExtensionCounter',
        paths: [{ from: '#source/counter', to: '#extension/counter' }],
      },
      tag: {
        from: 'source-counter',
        to: 'extension-counter',
        paths: [{ from: '#source/counter/define', to: '#extension/counter/define' }],
      },
    },
  ],
};

// note that you need to use `.default` for babel
const babel = fromRollup(rollupBabel.default);

export default {
  nodeResolve: true,
  watch: true,
  open: 'demo/',
  plugins: [
    babel({
      include: ['./demo/**/*.demo.js'],
      plugins: [[path.resolve('./'), extendDocsConfig]],
    }),
  ],
};