rollup-plugin-jsshaker 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # roll-plugin-jsshaker
2
+
3
+ The Rollup/Rolldown/Vite plugin for [JsShaker](https://github.com/kermanx/jsshaker), an experimental code size optimizer for JavaScript based on [the Oxc parser](https://oxc.rs).
4
+
5
+ ## Usage
6
+
7
+ ```bash
8
+ npm install -D rollup-plugin-jsshaker
9
+ ```
10
+
11
+ ```js
12
+ import JsShaker from "rollup-plugin-jsshaker";
13
+
14
+ export default {
15
+ // ...
16
+ plugins: [JsShaker()],
17
+ };
18
+ ```
@@ -0,0 +1,10 @@
1
+ import { Plugin } from "rollup";
2
+
3
+ //#region index.d.ts
4
+ interface Options {
5
+ preset?: "safest" | "recommended" | "smallest" | "disabled";
6
+ alwaysInlineLiteral?: boolean;
7
+ }
8
+ declare function rollupPluginJsShaker(pluginOptions?: Options): Plugin;
9
+ //#endregion
10
+ export { Options, rollupPluginJsShaker as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,50 @@
1
+ import { shakeMultiModule } from "jsshaker";
2
+
3
+ //#region index.ts
4
+ function rollupPluginJsShaker(pluginOptions = {}) {
5
+ return {
6
+ name: "rollup-plugin-jsshaker",
7
+ generateBundle: {
8
+ order: "post",
9
+ handler(outputOptions, bundle) {
10
+ const options = {
11
+ preset: pluginOptions.preset,
12
+ alwaysInlineLiteral: pluginOptions.alwaysInlineLiteral,
13
+ jsx: "react",
14
+ sourceMap: !!outputOptions.sourcemap,
15
+ minify: "minify" in outputOptions && !!outputOptions.minify && typeof outputOptions.minify === "object"
16
+ };
17
+ const entrySource = Object.values(bundle).filter((module) => module.type === "chunk" && module.isEntry).map((b) => b.fileName).map((name) => {
18
+ return `export * from "./${name}";\nexport { default } from "./${name}";`;
19
+ }).join("\n");
20
+ const entryFileName = "___entry___";
21
+ const sources = { [entryFileName]: entrySource };
22
+ for (const [fileName, module] of Object.entries(bundle)) if (module.type === "chunk") sources[fileName] = module.code;
23
+ const startTime = Date.now();
24
+ this.info(`Optimizing chunks...`);
25
+ const shaken = shakeMultiModule(sources, entryFileName, options);
26
+ this.info(`Completed in ${Date.now() - startTime} ms`);
27
+ for (const diag of shaken.diagnostics) this.warn(`${diag}`);
28
+ delete shaken.output[entryFileName];
29
+ const maxFileNameLength = Math.max(...Object.keys(shaken.output).map((n) => n.length));
30
+ let totalOriginalSize = 0;
31
+ let totalShakenSize = 0;
32
+ for (const [fileName, chunk] of Object.entries(shaken.output)) {
33
+ const module = bundle[fileName];
34
+ if (module && module.type === "chunk") {
35
+ const percentage = (chunk.code.length / module.code.length * 100).toFixed(2);
36
+ this.info(`- ${fileName.padEnd(maxFileNameLength)} ${percentage}% (${module.code.length} -> ${chunk.code.length} bytes)`);
37
+ totalOriginalSize += module.code.length;
38
+ totalShakenSize += chunk.code.length;
39
+ module.code = chunk.code;
40
+ } else throw new Error(`JsShaker Vite plugin expected to find module ${fileName} in the bundle.`);
41
+ }
42
+ const totalPercentage = (totalShakenSize / totalOriginalSize * 100).toFixed(2);
43
+ this.info(`${"-".repeat(maxFileNameLength - 4)} Total ${totalPercentage}% (${totalOriginalSize} -> ${totalShakenSize} bytes)`);
44
+ }
45
+ }
46
+ };
47
+ }
48
+
49
+ //#endregion
50
+ export { rollupPluginJsShaker as default };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "rollup-plugin-jsshaker",
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/kermanx/jsshaker"
8
+ },
9
+ "keywords": [
10
+ "rollup-plugin",
11
+ "vite-plugin",
12
+ "jsshaker"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.mts",
17
+ "default": "./dist/index.mjs",
18
+ "import": "./dist/index.mjs"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "license": "MIT",
26
+ "dependencies": {
27
+ "jsshaker": "0.1.1"
28
+ },
29
+ "devDependencies": {
30
+ "rollup": "^4.53.5",
31
+ "tsdown": "^0.18.1"
32
+ },
33
+ "scripts": {
34
+ "build": "tsdown ./index.ts",
35
+ "dev": "tsdown ./index.ts --watch"
36
+ }
37
+ }