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

web-components-codemods

v1.2.3

Published

Codemods for Web Components

Downloads

10

Readme

Web Components Codemods

Build Status Commitizen friendly semantic-release codecov Dependency status

NPM

Codemods for Web Components.
Breaking changes? Don't panic :)

Table of contents

Usage

The available codemods can be run in two ways: by using the included CLI or running the transform scripts directly with jscodeshift.

Using the included CLI

Install this package globally:

npm i -g web-components-codemods

Run the command in the directory you want to run a transform (the directory can be changed later):

kodemod

The command will prompt you for the transform to run and all of its options. kodemod CLI screenshot

Alternatively, you can run a specific transform by running kodemod <transform>.

Example:

kodemod replace-attrs

Available transform commands (same as transform scripts):

Using jscodeshift

Install jscodeshift globally:

npm i -g jscodeshift

Clone or download this repository:

npx degit kcmr/web-components-codemods

Run jscodeshift passing the transform script with the -t option:

jscodeshift target-dir/*.js -t web-components-codemods/<transform-script>.js

Available codemods

Replace attrs

Replaces attributes in the specified tag inside a template literal tagged html (LitElement or lit-html).

Script: transforms/replace-attrs.js

Options

| Name | Default | Type | Description | | ------------ | ----------- | --------- | -------------------------------------------------------- | | --tag | undefined | String | Tag name where the attributes will be replaced | | --attrs | undefined | String | Stringified object with {'old-attr': 'new-attr'} pairs | | --tabWidth | 4 | Number | Number of spaces used per tab | | --useTabs | false | Boolean | Use tabs instead of spaces |

Example input:

class MyComponent extends LitElement {
  render() {
    return html`
      <some-component
        attr-one="value"
        attr-two="${expression}"
        .someProp="${expression}"
      >
      </some-component>
    `;
  }
}

Command with options:

jscodeshift input.js -t replace-attrs.js --tag=some-component --attrs='{"attr-one": "foo", ".someProp": ".newProp"}'

Output:

class MyComponent extends LitElement {
  render() {
    return html`
      <some-component
-        attr-one="value"
+        foo="value"
        attr-two="${expression}"
-        .someProp="${expression}"
+        .newProp="${expression}"
      >
      </some-component>
    `;
  }
}

Replace block scope by IIFE

Replaces brackets used as scope in a file by an IIFE.

Script: transforms/block-scope-to-iife.js

Options: no options.

Example input:

{
  const { Element } = Polymer;
}

Command with options:

jscodeshift input.js -t block-scope-to-iife.js

Output:

-{
+(function() {
  const { Element } = Polymer;
+})();
-}

Rename tag

Renames a tag name inside template literals and strings.

Script: transforms/rename-tag.js

Options

| Name | Default | Type | Description | | ------------ | ----------- | --------- | ----------------------------- | | --oldTag | undefined | String | Tag name to replace | | --newTag | undefined | String | New tag name | | --tabWidth | 2 | Number | Number of spaces used per tab | | --useTabs | false | Boolean | Use tabs instead of spaces |

Example input:

const tpl = `
  <some-tag>
    <some-tag-two></some-tag-two>
  </some-tag>
`;
customElements.define('some-tag', SomeTag);

Command with options:

jscodeshift input.js -t rename-tag.js --oldTag=some-tag --newTag=new-tag

Output:

const tpl = `
-  <some-tag>
+  <new-tag>
    <some-tag-two></some-tag-two>
-  </some-tag>
+  </new-tag>
`;
-customElements.define('some-tag', SomeTag);
+customElements.define('new-tag', SomeTag);

LitElement to Lit imports

Updates the imports from lit-element to lit according to the upgrade guide of Lit 2.0

Script: transforms/lit-element-to-lit-imports.js

Options:

| Name | Default | Type | Description | | --------- | -------- | -------- | -------------------------------- | | --quote | single | String | Type of quote (single or double) |

Example input:

import { css } from 'lit-element';
import { LitElement, html, property as foo, customElement } from 'lit-element';
import { repeat } from 'lit-html/directives/repeat.js';
import { ifDefined } from 'lit-html/directives/if-defined';

Command with options:

jscodeshift input.js -t lit-element-to-lit-imports.js

Output:

-import { css } from 'lit-element';
+import { css } from 'lit';
-import { LitElement, html, property as foo, customElement } from 'lit-element';
+import { LitElement, html } from 'lit';
+import { property as foo, customElement } from 'lit/decorators.js';
-import { repeat } from 'lit-html/directives/repeat.js';
+import { repeat } from 'lit/directives/repeat.js';
-import { ifDefined } from 'lit-html/directives/if-defined';
+import { ifDefined } from 'lit/directives/if-defined';

Acknowledgments

Inspiration

Resources

License

This project is licensed under the MIT License.