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 🙏

© 2025 – Pkg Stats / Ryan Hefner

styled-px2rem-loader

v1.2.1

Published

A webpack loader that converts px to rem at buildtime

Readme

styled-px2rem-loader

A Webpack loader for automatically converting px units to rem in styled-components and React components.

Features

  • Converts px values to rem at build time
  • Supports styled-components template literals
  • Handles dynamic expressions and nested interpolations
  • Supports React components JSX transformations
  • Configurable conversion options
  • Integrates seamlessly with Webpack
  • Supports transformation suppression via comments
  • Selective file processing with include/exclude patterns

Installation

# Install the loader
npm install styled-px2rem-loader --save-dev

Usage

To use the styled-px2rem-loader, add it to your Webpack configuration:

// webpack.config.js
const path = require("path");

module.exports = {
  // ... other configurations ...
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: [
          {
            loader: "styled-px2rem-loader",
            include: [path.resolve(__dirname, "src")],
            exclude: [path.resolve(__dirname, "src/files/to/exclude/")],
            options: {
              rootValue: 16, // Base font size for conversion
              unitPrecision: 5, // Decimal places in rem values
              minPixelValue: 0, // Minimum px value to transform
              multiplier: 1, // Multiplication factor for conversion
              transformRuntime: false, // Enable runtime transformation
              mediaQuery: false, // Enable media query conversion
              transformJSX: true, // Enable JSX transformation
            },
          },
        ],
      },
    ],
  },
};

Example

Styled Components

// Before
const Button = styled.button`
  padding: 10px;
  margin: ${(props) => props.margin}px;
`;

// After transformation
const Button = styled.button`
  padding: 0.625rem; // 10px converted to rem
  margin: ${_px2rem((props) => props.margin)}; // Dynamic margin conversion
`;

React Components

// Before
const Component = () => (
  <div style={{ padding: "16px", margin: `${size}px` }}>
    <span margin="16px" />
  </div>
);

// After transformation
const Component = () => (
  <div style={{ padding: "1rem", margin: `${_px2rem(size)}` }}>
    <span margin="4.267px" />
  </div>
);

Configuration Options

{
  // Base font size for conversion
  "rootValue": 16,

  // Decimal places in rem values
  "unitPrecision": 5,

  // Minimum px value to transform
  "minPixelValue": 0,

  // Multiplication factor for conversion
  "multiplier": 1,

  // styled-components tags to transform
  "tags": ["styled", "css", "createGlobalStyle", "keyframes"],

  // Enable runtime transformation
  "transformRuntime": false,

  // Enable media query conversion
  "mediaQuery": false,

  // Enable JSX style transformation
  "transformJSX": false
}

Suppressing Transformations

You can suppress px-to-rem transformation by two methods:

  1. Add /* styled-px2rem-disable */ to the top of the file and it will be disabled for the whole file. Just like how you would do it with eslint.

  2. Add files or directories to the exclude option in the Webpack configuration and all files specified in the exclude option will be ignored.

  • Suppression to single line is not supported yet, it may or may not be supported in the future, depends on the demand.

Development

  1. Clone the repository
  2. Install dependencies: npm install
  3. Run tests: npm test

How It Works

The loader can process both styled-components template literals and React component styles:

  1. Identifies px values in CSS properties and JSX style attributes
  2. Converts static px values to rem at build time
  3. Transforms dynamic expressions to include rem conversion
  4. Preserves other CSS properties and values
  5. Handles React component px transformations when enabled

License

This project is licensed under the MIT License.