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

react-style-loader

v3.0.1

Published

React.js style loader module for webpack

Downloads

370

Readme

react-style-loader CI Status npm package

This is a fork based on vue-style-loader. Similar to vue-style-loader, you can chain it after css-loader to dynamically inject CSS into the document as style tags.

Options

  • manualInject (3.1.0+):

    Type: boolean. When importing the style from a js file, by default the style is injected as a side effect of the import. When manualInject is true, the imported style object exposes a __inject__ method, which can then be called manually at appropriate timing. If called on the server, the method expects one argument which is the ssrContext to attach styles to.

    import React from 'react'
    
    import styles from 'styles.scss'
    
    export default class App extends React.PureComponent {
      componentWillMount() {
        if (styles.__inject__) {
          styles.__inject__(this.props.staticContext) // staticContext from react-router on server
        }
      }
    
      render() {
        return <div class={styles.heading}>Hello World</div>
      }
    }
  • ssrId (3.1.0+):

    Type: boolean. Add data-react-ssr-id attribute to injected <style> tags even when not in Node.js. This can be used with pre-rendering (instead of SSR) to avoid duplicate style injection on hydration.

Differences from style-loader

Server-Side Rendering Support

When bundling with target: 'node', the styles in all rendered components are collected and exposed on the React render context object as context.styles, which you can simply inline into your markup's <head>. If you are building a React SSR app, you probably should use this loader for CSS imported from JavaScript files too.

To use react-style-loader to collect all styles on server, you need to separate loading global styles and async component styles, because we need to collect them through componentWillMount lifecycle on runtime, so you need add a option react: true to this loader for async component styles, then you will be able to use styles.__inject__(userContext) on server side.

Real world configuration

import webpack from 'webpack'
import ExtractTextWebpackPlugin from 'extract-text-webpack-plugin'
import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
import TerserWebpackPlugin from 'terser-webpack-plugin'

import { NODE_ENV, __DEV__, resolve } from './config'

const sourceMap = __DEV__
const minimize = !sourceMap

const cssLoaders = modules =>
  ExtractTextWebpackPlugin.extract({
    fallback: {
      loader: 'react-style-loader',
      options: {
        react: modules,
      },
    },
    use: [
      {
        loader: 'css-loader',
        options: {
          minimize,
          sourceMap,
          modules,
          camelCase: true,
          importLoaders: 2,
          localIdentName: __DEV__
            ? '[path][name]__[local]--[hash:base64:5]'
            : '[hash:base64:5]',
        },
      },
      {
        loader: 'postcss-loader',
        options: {
          minimize,
          sourceMap,
        },
      },
      {
        loader: 'sass-loader',
        options: {
          minimize,
          sourceMap,
          includePaths: [resolve('node_modules/bootstrap/scss')],
        },
      },
    ],
  })

export default {
  devtool: __DEV__ && 'cheap-module-source-map',
  resolve: {
    extensions: ['.js', '.scss'],
    modules: [resolve('src'), 'node_modules'],
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          cacheDirectory: true,
        },
      },
      {
        test: /\.pug$/,
        use: [
          'apply-loader',
          {
            loader: 'pug-loader',
            options: {
              pretty: __DEV__,
            },
          },
        ],
      },
      {
        test: /\.scss$/,
        oneOf: [
          {
            test: /bootstrap.scss/,
            use: cssLoaders(),
          },
          {
            test: /./,
            use: cssLoaders(true),
          },
        ],
      },
    ],
  },
  plugins: [
    new ExtractTextWebpackPlugin({
      disable: __DEV__,
      filename: '[name].[contenthash].css',
    }),
    new FriendlyErrorsWebpackPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
      __DEV__,
    }),
    ...(__DEV__
      ? [new webpack.NamedModulesPlugin(), new webpack.NamedChunksPlugin()]
      : [
          new TerserWebpackPlugin(),
          new webpack.NoEmitOnErrorsPlugin(),
          new webpack.optimize.ModuleConcatenationPlugin(),
        ]),
  ],
}

Misc

  • Does not support url mode and reference counting mode. Also removed singleton and insertAt query options. It always automatically pick the style insertion mechanism that makes most sense. If you need these capabilities you should probably use the original style-loader instead.

  • Fixed the issue that root-relative URLs are interpreted against chrome:// urls and make source map URLs work for injected <style> tags in Chrome.

License

MIT