1.0.2 • Published 2 years ago

extract-inline-css v1.0.2

Weekly downloads
6
License
MIT
Repository
github
Last release
2 years ago

extract-inline-css

Travis (.org)

Extract and replace inline CSS with classnames.

Installation

$ yarn add -D extract-inline-css

Usage

import extract from 'extract-inline-css';

extract('./index.html', {
  dist: './dist'
});

This will generate extracted.css and result.html files inside dist/ directory.

If you want to get results in string format, set out: 'object' option:

import extract from 'extract-inline-css';

const { css, html } = extract('./index.html', {
  out: 'object'
});

Input:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        background: #bada55;
      }
    </style>
  </head>
  <body>
    <h1 style="font-size: 22px; line-height: 1.2;">Hello world!</h1>
  </body>
</html>

Output:

result.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1 class="h1_g4q7h2">Hello world!</h1>
  </body>
</html>

extracted.css

body {
  background: #bada55;
}

.h1_g4q7h2 {
  font-size: 22px;
  line-height: 1.2;
}

Options

OptionTypeDefaultDescription
cssFilenamestringextracted.cssFilename of the resulting CSS file
diststring.Output directory path
extractGlobalStylesbooleantrueExtract CSS from <style> tags
formatCssbooleantrueBeautify CSS output
formatHtmlbooleanfalseBeautify HTML output
htmlFilenamestringoutput.htmlFilename of the resulting HTML file
keepStyleAttributebooleanfalseDo not strip 'style' attributes from HTML tags
keepStyleTagsbooleanfalseDo not strip <style> tags
out'file' | 'object'fileOutput format

License

MIT