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

eleventy-plugin-styles

v1.5.4

Published

Plugin for handling stylesheets from HTML.

Downloads

12

Readme

eleventy-plugin-styles 🍭

code style: prettier

Compiles and normalizes styles of your site 💪

Intention

Why should we use third-party tools like gulp, webpack or whatever you know for processing stylesheets if we can delegate it to Eleventy? Cool, right 😋!

Get started

What this plugin can do:

  1. Automatically reaches your styles, even from node_modules!

    In order to import style from package, simply write @(import|use) 'package_name/path/to/style'; 👐

  2. Compiles less, sass and scss.

    Plugin uses new sass package and less preprocessor, that allow you to use your favourite language for the CSS.

  3. Normalizes compiled CSS with PostCSS, autoprefixer, gets rid of unused style rules with PurgeCSS and minifies CSS with cssnano. And you can add much more plugins!

  4. Sets correct paths between HTML and CSS.

  5. Separates critical styles and uncritical ones. Thanks to critical package.

Installation

At first do:

npm i -D eleventy-plugin-styles

and then you can include it into .eleventy.js:

const { styles } = require('eleventy-plugin-styles');

module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		/* Optional options. */
	});
};

Options

Plugin can accept the following options:

interface StylesPluginOptions {
	/**
	 * Options that will be passed to [critical](https://github.com/addyosmani/critical)
	 * package.
	 */
	readonly criticalOptions?: CriticalOptions | PluginState.Off;
	/**
	 * Path to directory with all styles.
	 * Should be relative to _current working directory_.
	 */
	readonly inputDirectory?: string;
	/**
	 * Directory inside _output_ folder to be used as
	 * warehouse for all compiled styles. Will be
	 * prepended to public style urls in HTML.
	 */
	readonly publicDirectory?: string;
	/**
	 * Options that can be passed to [`sass`](https://www.npmjs.com/package/sass)
	 * module.
	 */
	readonly sassOptions?: SassCompilerOptions | PluginState.Off;
	/**
	 * Options that can be passed to [`less`](https://www.npmjs.com/package/less)
	 * module.
	 */
	readonly lessOptions?: Less.Options | PluginState.Off;
	/** Options to be passed to [`PurgeCSS`](https://purgecss.com/). */
	readonly purgeCSSOptions?: PurgeCSSOptions | PluginState.Off;
	/** Options to be passed to [`CSSNano`](https://cssnano.co/). */
	readonly cssnanoOptions?: CssNanoOptions | PluginState.Off;
	/** Array of plugins that can be passed to [`PostCSS`](https://postcss.org). */
	readonly postcssPlugins?: ReadonlyArray<AcceptedPlugin>;
	/**
	 * Indicates whether should Eleventy watch on files
	 * under _inputDirectory_ or not.
	 */
	readonly addWatchTarget?: boolean;
}

Explanation

inputDirectory

Plugin extracts links to stylesheet files from HTML. Therefore, your templates should have links to source style files.

For example:

<!-- Note that we wrote `styles.scss` 👇 -->
<link rel="stylesheet" href="style.scss" />

Plugin recognizes followed extensions: css, less, sass and scss. In future may be added much more if you will need it 🤓

After links extraction plugin will search for these files inside inputDirectory from options. So given above example:

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		inputDirectory: 'src/styles',
	});
};

Plugin will assume that path style file is src/styles/style.scss 🎉 And after all procedures will put compiled file to _site/style-[hash].css and link in HTML will be changed to:

<!-- If HTML file is in the same directory if style.css -->
<link rel="stylesheet" href="/style-[hash].css" />

_site is used just for example. Actually name of the directory will be up to you - plugin will know about it.

You can write relative path to styles if you prefer such style. For example, if your template path is src/templates/template.11ty.js and path to style file is src/styles/style.scss, then:

<link rel="stylesheet" href="../styles/style.scss" />

If path starts with leading slash (/), then it will be removed.

publicDirectory

If you want to customize output path of compiled style inside output directory, then you can provide publicDirectory option.

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		inputDirectory: 'src/styles',
		publicDirectory: 'styles',
	});
};

Given above example, stylesheet file will be placed into _site/styles directory, and its public path will be styles/style-[hash].css.

Pretty convenient, yes? 🙂

addWatchTarget

By default, Eleventy will watch for changes inside inputDirectory. You have an opportunity to disable it:

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		// Now Eleventy will not trigger rebuild process
		// if any style changes.
		addWatchTarget: false,
	});
};

sassOptions

Plugin supports sass preprocessor.

If you want to customize its behavior then options need to be passed to plugin.

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		sassOptions: {
			/* Some useful options. */
		},
	});
};

Plugin uses this preprocessor as the default language for CSS. You can disable it by providing off value instead of options.

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		sassOptions: 'off',
	});
};

Avoid changing file property, because provided stylesheet will be inserted into all your HTML pages, instead of referenced ones. Bad situation ☹️. Also if you want to override includePaths property, then make sure you add node_modules to array (this is a default value).

lessOptions

Plugin supports less preprocessor.

If you want to customize its behavior then options need to be passed to plugin.

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		lessOptions: {
			/* Some useful options. */
		},
	});
};

Or disable it by providing the off value.

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		lessOptions: 'off',
	});
};

If you want to use this preprocessor, then you should disable sass preprocessor at first.

purgeCSSOptions

PurgeCSS is included as a plugin to PostCSS.

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		purgeCSSOptions: {
			/* Some useful options. */
		},
	});
};

You can disable the plugin by passing 'off' as an option, like this:

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		purgeCSSOptions: 'off',
	});
};

Avoid overriding content property and css, because they are used internally and that may cause unexpected results.

cssnanoOptions

cssnano is included as a plugin to PostCSS.

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		cssnanoOptions: {
			/* Some useful options. */
		},
	});
};

You can disable the plugin by passing 'off' as an option, like this:

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		cssnanoOptions: 'off',
	});
};

By default it uses default preset for CSS optimization.

postcssPlugins

cssnano is included as a plugin to PostCSS.

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		postcssPlugins: [
			/* Some useful plugins. */
		],
	});
};

By providing additional plugins purgeCSS and cssnano plugins will not be removed, so if you want to change their behavior provide according options as described above ☝️.

criticalOptions

critical is included. By default, it works in production mode and if criticalOptions property is not equal to 'off' value (that disables the plugin).

// .eleventy.js
module.exports = (eleventyConfig) => {
	eleventyConfig.addPlugin(styles, {
		criticalOptions: {
			/* Some useful options. */
		},
	});
};

By default, it inlines critical styles into HTML and defers loading uncritical styles. It extracts critical styles from linked stylesheets, so you can safely import the same stylesheet file into multiple templates.

Tip: critical tries its best to rebase assets in styles, but it won't touch assets that have absolute public URL 🤗.

Word from author

Have fun! ✌️