Remove Script Type JavaScript Rehype Plugin

4.0.1 · active · verified Sun Apr 19

This `rehype` plugin, `rehype-remove-script-type-javascript`, is designed to optimize HTML documents by automatically removing redundant `type` and `language` attributes from `<script>` elements that inherently contain JavaScript. It intelligently identifies and targets scripts with types like `text/javascript` or `language` attributes, leaving module scripts (`type="module"`) and other non-JavaScript script types untouched. The package's current stable version is `4.0.1`. It operates within the `unified` ecosystem, processing `hast` syntax trees. While `rehype-remove-script-type-javascript` has its own versioning, it is part of the `rehype-minify` monorepo, which recently underwent a significant `7.0.0` major release, introducing ecosystem-wide changes such as a strict Node.js 16+ requirement and exclusive ESM distribution. This plugin helps improve page load performance by reducing the byte size of HTML, as these attributes are often unnecessary in modern browsers where JavaScript is the default script type. It offers a focused, declarative approach to HTML optimization within a `unified` pipeline.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to integrate `rehype-remove-script-type-javascript` into a `unified` processing pipeline to remove redundant script type attributes from an HTML file.

import rehypeParse from 'rehype-parse';
import rehypeRemoveScriptTypeJavaScript from 'rehype-remove-script-type-javascript';
import rehypeStringify from 'rehype-stringify';
import {unified} from 'unified';
import {VFile} from 'vfile';

async function processExampleHtml() {
  const htmlInput = `<!doctype html><html><head></head><body>
    <script type="text/javascript">console.log('Legacy JS');</script>
    <script language="javascript">alert('Another legacy JS');</script>
    <script type="module">import './modern.js';</script>
    <script type="application/json">{"data": "json-script"}</script>
    <script src="external.js" type="text/javascript"></script>
  </body></html>`;

  const inputVFile = new VFile({ path: 'input.html', value: htmlInput });

  const outputVFile = await unified()
    .use(rehypeParse, { fragment: false }) // Process as a full document
    .use(rehypeRemoveScriptTypeJavaScript)
    .use(rehypeStringify)
    .process(inputVFile);

  console.log('Original HTML:\n', htmlInput);
  console.log('\nProcessed HTML:\n', String(outputVFile));
}

processExampleHtml();

view raw JSON →