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

compressed-script-loader

v1.4.21

Published

Reduces network traffic for huge source files built by webpack etc.

Downloads

71

Readme

LICENSE Node.js CI npm version npm bundle size npm

jet compressed-script-loader

Reduces network traffic for huge source files built by webpack etc.

✨ Motivation

Today's web server hosts support compression transfer with content-encoding,
but for some servers that do not perform compression transfer, this module can be expected to significantly reduce network traffic.

📇 Details

  • Use code with comment extraction feature added to unzipSync of fflate.

  • comment is used to embed SRI. (script.integrity)
    If comment is omitted or does not pass to verify process that it is SRI,
    the unziped code will be bound to the text property of script element.
    You can use sha256, sha384, and sha512 in script.integrity, and the prefix sha###- is omit able because the hash method is automatically detected by the length of the SRI.

  • example script element by compressed-script-loader

    <script id="system-coordinate-map-mini.js"
      integrity="sha384-X21iVQnEaPD0mdNEF66lHUBS4f63TQQHaDVxvRwIDm4yi+IE+ulVIpDJDAoKW3BP"
      crossorigin="anonymous"
      src="blob:https://jeffy-g.github.io/b4e7bdbd-f4c8-41af-b170-93fb979317fd">
    </script>
  • cslpack cli - simple zip tool is available (since v1.2.x)

  • support module pack (experimental, since v1.3.x)

    • importmap is auto generate

🖌️ TODO

  • type="module"
    • stage 1 (v1.3.4, unstable)

    • stage 2 (v1.4.8, unstable)

      • Module id mapped by "baseUrl" and "paths" in tsconfig.json can now be resolved.
       // 
       {
         "compilerOptions": {
           // 
           "baseUrl": ".",
           "paths": {
             "eveworld/*": [
               "./*"
             ],
             "web/*": [
               "./web/*"
             ]
           }
         }
       }

      note:

      1. Filename only recognizes "tsconfig.json".
      2. At a minimum, compilerOptions.baseUrl and compilerOptions.paths are required.
      3. No matter where "tsconfig.json" is located, if compilerOptions.baseUrl is set correctly, there will be no error! (Probably...)

🦯 Use for SPA

Load automatically

  • Specifying config in the data-csl-config attribute of the script element
    will automatically load the zip file and insert it as a script element in the header section of the html page.
<script>
  let onLoadDone = () => {
    onLoadDone = void 0;
    runEVEWorld(() => {
      window.setTimeout(() => {
        document.querySelectorAll("script[src^='./loader/'],script[src*='compressed-script-loader']").forEach(script => script.remove());
        delete window.NsLoader;
      }, 1000);
    });
  };
</script>
<script
  data-csl-config='{
    "base": "./libs",
    "selector": "script[src*=css-2d-renderer]",
    "load": [
      "system-coordinate-map-mini"
    ],
    "callback": "onLoadDone"
  }'
  type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/compressed-script-loader/umd/index.js"
></script>
  • vscode snippet
"compressed script loader snippet": {
  "prefix": "csl-script-tag",
  "scope": "markdown,html,json,jsonc,snippets",
  "description": "Insert compressed script loader script tag.",
  "body": [
    "<script data-csl-config='{",
    "  \"base\": \"${1:./lib}\",",
    "  \"load\": ${2|\"bundle\",[\"bundle\"]|},",
    "  ${3|\"callback\": \"cslCallback\",\"callback\": \"cslCallback\"&#44;\n\"cleanup\": true|}",
    "}' src=\"https://cdn.jsdelivr.net/npm/compressed-script-loader/umd/index.js\" defer></script>"
  ]
}
  • Supports simple data-csl-config (since v1.2.6)
    • In this case, the packed zip file must be deployed in the same directory as the page and
      If need a callback, define it as cslCallback.
<script
  data-csl-config='"system-coordinate-map-mini"'
  src="https://cdn.jsdelivr.net/npm/[email protected]/umd/index.js"
></script>
<!-- or -->
<script
  data-csl-config='["system-coordinate-map-mini", "bundle"]'
  src="https://cdn.jsdelivr.net/npm/[email protected]/umd/index.js"
></script>

Load manually

<script src="https://cdn.jsdelivr.net/npm/compressed-script-loader/umd/index.js"></script>
<script>
// global variable `NsLoader` is available (umd)
(async () => {
    const ZIPNAME = "system-coordinate-map-mini";
    // If the packed zip file is located in the same directory as the page
    // and you don't care where the script is inserted, you can skip this step.
    NsLoader.setConfig("./libs"/*, "script[src*=css-2d-renderer]"*/);

    await NsLoader.loadCompressedScript(ZIPNAME);
    // If you don't use it anymore, this step will destroy the loader api
    NsLoader.cleanUp();

    runEVEWorld(() => {
        window.setTimeout(() => {
          document.querySelectorAll("script[src^='./loader/'],script[src*='compressed-script-loader']").forEach(script => script.remove());
          delete window.NsLoader;
        }, 1000);
    });
})();
</script>

command-line cslpack cli (since v1.2.x)

uses fflate's zipSync feature

$ cslpack <jspath> <jspath>... [-(d|dest) <output zip path>] [-sha <256|384|512>] [-m <main module name with extension>]

  • options
    • -(d|dest)- output zip path. when omited will be output to ./dist/bundle.zip
    • -sha - [256|384|512] when omited use 384 "sha384" (since v1.2.8)
    • -m - When packing as an esm module, specify the script name such as main.js that will be the entry point. pack as esm module with "importmap" data.
      "importmap" is auto generate and pack together. (since v1.3 experimental)

example:

# apply just `sha384`
$ cslpack -d dist/lib/bundle.zip build/{bundle,data}.js

# or
$ cslpack build/{cjs,esm}/* -dest dist/lib/bundle.zip -sha 512

# will be output to "./dist/bundle.zip"
$ cslpack build/{cjs,esm}/*

# pack as `esm` module. `main.js` is `build/esm/main.js`
$ clspack build/esm/*.js build/lib/hoge-data.js -m main.js 

NOTE: `cslpack` does not keep directory path

gear Loader API

declare global {
    var cslCallback: TCSLCallbak;
    interface Window {
        cslCallback: TCSLCallbak;
        [cslCallbackName: string]: TCSLCallbak;
    }
}
export declare type TCSLCallbak = ((err?: Error[]) => void) | undefined;
export interface UnzipFileInfo {
    name: string;
    size: number;
    originalSize: number;
    compression: number;
    /**
     * @since v1.2.9
     */
    comment?: string;
}
export declare type UnzipFileFilter = (file: UnzipFileInfo) => boolean;
export interface UnzipOptions {
    filter?: UnzipFileFilter;
}
export interface Unzipped {
    [path: string]: {
        data: Uint8Array;
        comment?: string;
    };
}
export declare type TCSLConfig = {
    base: string;
    selector?: string;
    load: string[];
    callback?: string;
    cleanup?: true;
};

declare var NsLoader: {
    unzipSync: (data: Uint8Array, opts?: UnzipOptions) => Unzipped;
    cleanUp: () => void;
    setConfig: (base: string, insertionSelector?: string | undefined) => void;
    loadCompressedScript: (baseName: string, log?: (msg: string) => void) => Promise<void>;
    readonly version: string;
};
declare interface Window {
    NsLoader: typeof NsLoader;
}

settings Config definition

/**
* (C)ompressed-(S)cript-(L)oader config
* 
* @version 1.0
*/
export type TCSLConfig = {
    /**
    * Specifies the directory that contains the zip file
    * 
    * ```js
    * base: "./lib" // or https://example.com/lib
    * ```
    */
    base: string;
    /**
    * #### Insertion selector
    * 
    *   + can be omit. In the current specification, specify element inside `head`  
    *     element to insert into` head` element.
    * 
    *   + If omitted, it will be inserted after `document.head.lastElementChild`
    * 
    *  NOTE: This will change in the near future
    * 
    * ```js
    * selector: "script[src*=css-2d-renderer]"
    * ```
    */
    selector?: string;
    /**
    * list of zip files you want to load
    * 
    *   + The comment attached to the zip file entry is used for `script.integrity`
    * 
    * NOTE: Omit the `.zip` extension
    * 
    * ```js
    * // actually, "system-coordinate-map-mini.zip", "bundle.zip"
    * load: ["system-coordinate-map-mini", "bundle"]
    * ```
    */
    load: string[];
    /**
     * can be omit, default: "cslCallback"
     * 
     * Please specify if you need to use another name
     * 
     * ```js
     * var cslCallback = () => {
     *    runEVEWorld(() => {
     *        window.setTimeout(() => {
     *            document.querySelectorAll("script[src^='./loader/'],script[src*='compressed-script-loader']").forEach(script => script.remove());
     *            delete window.NsLoader;
     *        }, 1000);
     *    });
     * };
     * ```
     */
    callback?: string;

    /**
     * need cleanup?
     * @default undefined
     */
    cleanup?: true;
};

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details