compressed-script-loader
v1.4.21
Published
Reduces network traffic for huge source files built by webpack etc.
Downloads
71
Maintainers
Readme
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
offflate
.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 thetext
property ofscript
element.
You can usesha256
,sha384
, andsha512
inscript.integrity
, and the prefixsha###-
is omit able because the hash method is automatically detected by the length of the SRI.example
script
element bycompressed-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:
- Filename only recognizes "tsconfig.json".
- At a minimum,
compilerOptions.baseUrl
andcompilerOptions.paths
are required. - 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 thescript
element
will automatically load the zip file and insert it as ascript
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\",\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 ascslCallback
.
- In this case, the packed zip file must be deployed in the same directory as the page and
<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>
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 asmain.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
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;
}
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
- jeffy-g - jeffy-g
License
This project is licensed under the MIT License - see the LICENSE file for details