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

html-webpack-inject-style-plugin

v1.0.2

Published

Dynamically inject css style assets when using webpack-rtl-plugin

Downloads

45

Readme

html-webpack-inject-style-plugin

Enhances html-webpack-plugin functionality by changing the default inject assets method to dynamically inject css style assets.

webpack-rtl-plugin is plugin to use to create a second css bundle, processed to be rtl, rtl css bundle default filename is xxx.rtl.css. When html-webpack-plugin will generate an HTML5 file, they are all included with tags in the HTML head by default.

It is expected that the corresponding styles can be loaded according to the language type, instead of all styles being injected into index.html by default.This plugin is used to help us complete this work. It uses cookies to determine the language type, and then decides which style to load.

Installation

You must be running webpack on Node v4.0.0 or higher.

Install the plugin with npm:

$ npm install --save-dev html-webpack-inject-style-plugin

Usage

Require the plugin in your webpack config, and add the plugin to your webpack config as follows:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const WebpackRTLPlugin = require('webpack-rtl-plugin');
const HtmlWebpackInjectStylePlugin = require('html-webpack-inject-style-plugin');

module.exports ={
  entry: './entry.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader'
        ]
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'style.css'
    }),
    new WebpackRTLPlugin(),
    new HtmlWebpackPlugin(),
    new HtmlWebpackInjectStylePlugin({
      // isRtl: /lang_type=ar/ // is a RegExp
      isRtl: function (window, href) {  // is a Function
        return true
      },
      modifyTag: function (link) {
        link.setAttribute('crossorigin', 'anonymous');
        return link;
      }
    })
  ]
}

The above configuration will inject a script in the head, When the page is parsed, execute to determine whether the current language is an RTL language, such as Arabic. If it is RTL language, inject xxx.rtl.css style otherwise inject xxx.css style. You can also modify the attributes of the link by the function modifyTag.

<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
    <link href="style.css" rel="stylesheet">
    <link href="style.rtl.css" rel="stylesheet">
  </head>
  <body>
    <script type="text/javascript" src="main.js"></script>
  </body>
</html>

The above is the index.html file generated by default, it will be processed into the following

<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
    <script type="text/javascript">
      (function () {
        // if `isRTL` is a RegExp
        // var isRTL = new Function("return /lang_type=ar/.test(document.cookie)");
        // if `isRTL` is a Function
        var isRTL = new Function("href","return (function isRtl (window, href) {\n      return !/test/.test(href) && /lang=(ar|he)/.test(window.location.href);\n    })(window, href)");
        var modifyTag = new Function("link","return (function (link) {\n            link.setAttribute('crossorigin', 'anonymous');\n            return link;\n          })(link)");
        var head = document.querySelector('head');
        ["style"].forEach(function (href) {
          var fullhref = isRTL(href) ? href + '.rtl.css' : href + '.css';
          var linkTag = document.createElement("link");
          linkTag.rel = "stylesheet";
          linkTag.type = "text/css";
          linkTag.href = fullhref;
          if (modifyTag) {
            linkTag = modifyTag(linkTag);
          }
          head.appendChild(linkTag);
        })
      })();
    </script>
  </head>
  <body>
    <script type="text/javascript" src="main.js"></script>
  </body>
</html>

Options

|Name|Type|Default|required|Description| |:--:|:--:|:-----:|:----:|:----------| |isRtl|{RegExp\|Function}|/| true |The regexp to use to process cookies to determine whether the current page is in RTL language, and you can also set it as a function, accepts two parameters window and href (css path),if the execution result of the function is true, it is a RTL language.| |modifyTag|Function|/|false|Used to modify the style link attribute, accepts one parameter link, you can add other properties to it, e.g. crossorigin|