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-component-pack-loader

v1.0.4

Published

a react component loader for webpack

Downloads

19

Readme

react component Loader

a react component of a directory loader for webpack

What is a react component

a react component may contains scripts (js, jsx, ts, tsx, mjs, etc.), styles (css, less, styl, scss) and assets (images, files, fonts, etc.).

The component structure

component
├── script.js
├── assets
│   ├── 1.jpg
│   ├── 2.png
│   └── index.js
├── style
│   └── index.less
│   └── index.css
└── index.js

you can see more examples in .examples/src.

!!!WARNING!!! the component/index.js can't only be

epxort { default } from './Component';

this will cause webpack handshake mechanism and lose style injection

Assets (Experiment)

when match /assets/index.(js|ts|jsx|tsx)/, the loader will change

export const JPG1 = './1.jpg';
export const JPG2 = './2.jpg';

to

export { default as JPG1 } from './1.jpg';
export { default as JPG2 } from './2.jpg';

Usage

Install

yarn add react-component-pack-loader

Webpack loader config

module: {
  rules: [
    {
      test: /\.(ts|tsx)$/,
      exclude: /(node_modules|bower_components)/,
      use: [{
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env']
        }
      }]
    },
    // react-component-pack-loader should put after babel-loader, ts-loader, etc.
    {
      test: /\.(ts|js)$/,
      exclude: /(node_modules|bower_components)/,
      use: [{
        loader: path.resolve('./component-loader'),
        options: {
          baseStyleTarget: path.resolve('./src/index.js'),
          components: [{
            // is default
            lib: 'lib',
            // will inject to baseStyleTarget script, should without extension
            baseStyle: 'lib/style/base',
            // will inject to component file
            style: 'less'
          }, {
            test: 'My',
            style: 'less'
          }]
        }
      }]
    },
    {
      test: /\.jpg|png/,
      use: ['file-loader']
    },
    {
      test: /\.css/,
      use: ['style-loader', 'css-loader']
    },
    {
      test: /\.less/,
      use: ['style-loader', 'css-loader', 'less-loader']
    }
  ]
}

Options

you can use webpack loader options as bellow.

type Option = {
  // absolute path for inject base styles
  baseStyleTarget: string,
  // the array order is the same with baseStyle injection order
  components: Array<{
    // the webpack resolve path for library, this will search pakcage.json of lib,
    // to find `components` property and append to options
    lib: string;
    // the style file path to inject to `baseStyleTarget`, should not with extension
    baseStyle: string;

    // determine match or not, when lib is set the test will auto set to
    test: string | RegExp;
    // string for RegExp constructor, default is assets/index.(js|ts|jsx|tsx|mjs)
    assetsRule: string | RegExp;
    componentRule: string | RegExp;
    // 'less' | 'css' | 'styl' | 'scss'
    style: string;
  }>
}

pathRegExp + index is the regular expression for test the absolute file path. if matched will insert the import '${style}' into the file.