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

lz-loader

v1.0.5

Published

lz-loader

Downloads

19

Readme

lz-loader

A loader for webpack that allows importing assets (json/txt/svg/html/css ...) as a compressed string with lz-string, and self decompressed on runtime.

Install

npm install lz-loader -D

Options

| Name | Type | Default | Description | | :---------: | :----------------------: | :-----: | :--------------------- | | esModule | {Boolean} | false | Uses ES modules syntax | | compressor | {String} or {Function} | | Custom compressor |

Add the loader to webpack config

//webpack.config.js
module.exports = {
    module: {
        rules: [{
            test: /\.(json|txt|svg)$/,
            type: "javascript/auto",
            use: {
                loader: "lz-loader"
                options: {
                    esModule: false
                }
            }
        }]
    }
};

Inline loader

//inline loader
const str = require("!!lz-loader!./icons.svg");

see https://webpack.js.org/concepts/loaders/#inline

Css compressor between css-loader and style-loader

//webpack.config.js
module.exports = {
    module: {
        rules: [{
            test: /\.css$/,
            use: [{
                loader: "style-loader",
                options: {
                    injectType: "singletonStyleTag"
                }
            }, {
                loader: "lz-loader",
                options: {
                    esModule: false,
                    compressor: "css-loader"
                }
            }, {
                loader: "css-loader",
                options: {
                    esModule: false,
                    import: false,
                    sourceMap: false
                }
            }]
    }
};

see dist/compressors/css-loader.js

Custom compressor function

//webpack.config.js
module.exports = {
    module: {
        rules: [{
            test: /\.css$/,
            use: [{
                loader: "after-loader",
            }, {
                loader: "lz-loader",
                options: {
                    esModule: false,
                    compressor: function(source, compress, decompressPath, options) {
                        var newSource = yourHandler(source);
                        return newSource;
                    }
                }
            }, {
                loader: "before-loader"
            }]
    }
};

Compression cases

Benchmark

┌───────┬─────────────┬─────────┬──────────┬──────────┬──────────────┐
│ type  │ build mode  │ with lz │ duration │     size │ size reduced │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ css   │ development │ false   │ 1,411 ms │ 246.3 KB │            - │
│ css   │ development │ true    │   682 ms │  99.3 KB │       59.69% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ css   │ production  │ false   │   659 ms │ 229.8 KB │            - │
│ css   │ production  │ true    │   696 ms │  77.6 KB │       66.21% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ json  │ development │ false   │   161 ms │ 342.3 KB │            - │
│ json  │ development │ true    │   292 ms │ 165.9 KB │       51.53% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ json  │ production  │ false   │   346 ms │ 340.4 KB │            - │
│ json  │ production  │ true    │   469 ms │ 160.6 KB │       52.82% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ svg   │ development │ false   │   420 ms │ 576.6 KB │            - │
│ svg   │ development │ true    │   538 ms │ 267.0 KB │       53.69% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ svg   │ production  │ false   │   648 ms │ 570.1 KB │            - │
│ svg   │ production  │ true    │   562 ms │ 261.4 KB │       54.14% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ text  │ development │ false   │   130 ms │ 220.4 KB │            - │
│ text  │ development │ true    │   189 ms │ 101.0 KB │       54.17% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ text  │ production  │ false   │   317 ms │ 218.5 KB │            - │
│ text  │ production  │ true    │   381 ms │  95.7 KB │       56.21% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ mixed │ development │ false   │   964 ms │   1.3 MB │            - │
│ mixed │ development │ true    │   830 ms │ 613.7 KB │       55.54% │
├───────┼─────────────┼─────────┼──────────┼──────────┼──────────────┤
│ mixed │ production  │ false   │ 1,271 ms │   1.3 MB │            - │
│ mixed │ production  │ true    │ 1,267 ms │ 589.2 KB │       56.59% │
└───────┴─────────────┴─────────┴──────────┴──────────┴──────────────┘

see detail test/test.js

Changelogs

  • 1.0.5

    • updated lz-utils
  • 1.0.4

    • fixed issue for inline json
  • 1.0.3

    • updated schema-utils to latest
  • 1.0.2

    • added new option compressor for custom compression
  • 1.0.1

    • added new feature for css-loader