jsshaker 0.0.0 → 0.1.0

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,49 @@
1
+ # JsShaker
2
+
3
+ 🪚 Code size optimizer for JavaScript.
4
+
5
+ https://github.com/kermanx/jsshaker
6
+
7
+ ## WASM & N-API
8
+
9
+ ```ts
10
+ import { shakeSingleModule, shakeMultiModule } from "jsshaker";
11
+ ```
12
+
13
+ ## CLI
14
+
15
+ ```sh
16
+ pnpx jsshaker ./entry.js
17
+
18
+ # --preset(-p, optional): "safest" | "recommended" | "smallest" | "disabled"
19
+ # --minify(-m, optional): boolean
20
+ # --outdir(-o, optional): string
21
+ ```
22
+
23
+ ## Vite Plugin
24
+
25
+ ```ts
26
+ import { defineConfig } from "vite";
27
+ import { shakeMultiModule } from "jsshaker";
28
+ import * as fs from "fs";
29
+
30
+ export default defineConfig({
31
+ plugins: [
32
+ {
33
+ name: "jsshaker",
34
+ enforce: "post",
35
+ buildEnd() {
36
+ const { output, diagnostics } = shakeMultiModule("./dist/index.js", {
37
+ minify: true,
38
+ })
39
+ for (const diag of diagnostics) {
40
+ console.error(diag);
41
+ }
42
+ for (const file in output) {
43
+ fs.writeFileSync(file, output[file]);
44
+ }
45
+ },
46
+ },
47
+ ],
48
+ });
49
+ ```
Binary file
Binary file
Binary file
@@ -0,0 +1,112 @@
1
+ /* eslint-disable */
2
+ /* prettier-ignore */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ const __nodeFs = require('node:fs')
7
+ const __nodePath = require('node:path')
8
+ const { WASI: __nodeWASI } = require('node:wasi')
9
+ const { Worker } = require('node:worker_threads')
10
+
11
+ const {
12
+ createOnMessage: __wasmCreateOnMessageForFsProxy,
13
+ getDefaultContext: __emnapiGetDefaultContext,
14
+ instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
15
+ } = require('@napi-rs/wasm-runtime')
16
+
17
+ const __rootDir = __nodePath.parse(process.cwd()).root
18
+
19
+ const __wasi = new __nodeWASI({
20
+ version: 'preview1',
21
+ env: process.env,
22
+ preopens: {
23
+ [__rootDir]: __rootDir,
24
+ }
25
+ })
26
+
27
+ const __emnapiContext = __emnapiGetDefaultContext()
28
+
29
+ const __sharedMemory = new WebAssembly.Memory({
30
+ initial: 4000,
31
+ maximum: 65536,
32
+ shared: true,
33
+ })
34
+
35
+ let __wasmFilePath = __nodePath.join(__dirname, 'jsshaker.wasm32-wasi.wasm')
36
+ const __wasmDebugFilePath = __nodePath.join(__dirname, 'jsshaker.wasm32-wasi.debug.wasm')
37
+
38
+ if (__nodeFs.existsSync(__wasmDebugFilePath)) {
39
+ __wasmFilePath = __wasmDebugFilePath
40
+ } else if (!__nodeFs.existsSync(__wasmFilePath)) {
41
+ try {
42
+ __wasmFilePath = __nodePath.resolve('@jsshaker/binding-wasm32-wasi')
43
+ } catch {
44
+ throw new Error('Cannot find jsshaker.wasm32-wasi.wasm file, and @jsshaker/binding-wasm32-wasi package is not installed.')
45
+ }
46
+ }
47
+
48
+ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
49
+ context: __emnapiContext,
50
+ asyncWorkPoolSize: (function() {
51
+ const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
52
+ // NaN > 0 is false
53
+ if (threadsSizeFromEnv > 0) {
54
+ return threadsSizeFromEnv
55
+ } else {
56
+ return 4
57
+ }
58
+ })(),
59
+ reuseWorker: true,
60
+ wasi: __wasi,
61
+ onCreateWorker() {
62
+ const worker = new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
63
+ env: process.env,
64
+ })
65
+ worker.onmessage = ({ data }) => {
66
+ __wasmCreateOnMessageForFsProxy(__nodeFs)(data)
67
+ }
68
+
69
+ // The main thread of Node.js waits for all the active handles before exiting.
70
+ // But Rust threads are never waited without `thread::join`.
71
+ // So here we hack the code of Node.js to prevent the workers from being referenced (active).
72
+ // According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
73
+ // a worker is consist of two handles: kPublicPort and kHandle.
74
+ {
75
+ const kPublicPort = Object.getOwnPropertySymbols(worker).find(s =>
76
+ s.toString().includes("kPublicPort")
77
+ );
78
+ if (kPublicPort) {
79
+ worker[kPublicPort].ref = () => {};
80
+ }
81
+
82
+ const kHandle = Object.getOwnPropertySymbols(worker).find(s =>
83
+ s.toString().includes("kHandle")
84
+ );
85
+ if (kHandle) {
86
+ worker[kHandle].ref = () => {};
87
+ }
88
+
89
+ worker.unref();
90
+ }
91
+ return worker
92
+ },
93
+ overwriteImports(importObject) {
94
+ importObject.env = {
95
+ ...importObject.env,
96
+ ...importObject.napi,
97
+ ...importObject.emnapi,
98
+ memory: __sharedMemory,
99
+ }
100
+ return importObject
101
+ },
102
+ beforeInit({ instance }) {
103
+ for (const name of Object.keys(instance.exports)) {
104
+ if (name.startsWith('__napi_register__')) {
105
+ instance.exports[name]()
106
+ }
107
+ }
108
+ },
109
+ })
110
+ module.exports = __napiModule.exports
111
+ module.exports.shakeMultiModule = __napiModule.exports.shakeMultiModule
112
+ module.exports.shakeSingleModule = __napiModule.exports.shakeSingleModule
Binary file
Binary file
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "jsshaker",
3
- "version": "0.0.0",
3
+ "version": "0.1.0",
4
4
  "type": "commonjs",
5
5
  "main": "index.js",
6
6
  "browser": "browser.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/kermanx/jsshaker"
10
+ },
7
11
  "napi": {
8
12
  "binaryName": "jsshaker",
9
13
  "packageName": "@jsshaker/binding",
@@ -27,7 +31,15 @@
27
31
  "files": [
28
32
  "index.d.ts",
29
33
  "index.js",
30
- "cli.js"
34
+ "cli.js",
35
+ "README.md",
36
+ "browser.js",
37
+ "jsshaker.wasi.cjs",
38
+ "jsshaker.wasi-browser.cjs",
39
+ "wasi-worker.mjs",
40
+ "wasi-worker-browser.mjs",
41
+ "*.wasm",
42
+ "*.node"
31
43
  ],
32
44
  "license": "MIT",
33
45
  "devDependencies": {
@@ -37,11 +49,11 @@
37
49
  "ohash": "^2.0.11"
38
50
  },
39
51
  "optionalDependencies": {
40
- "@jsshaker/binding-linux-x64-gnu": "0.0.0",
41
- "@jsshaker/binding-win32-x64-msvc": "0.0.0",
42
- "@jsshaker/binding-darwin-x64": "0.0.0",
43
- "@jsshaker/binding-darwin-arm64": "0.0.0",
44
- "@jsshaker/binding-wasm32-wasi": "0.0.0"
52
+ "@jsshaker/binding-linux-x64-gnu": "0.1.0",
53
+ "@jsshaker/binding-win32-x64-msvc": "0.1.0",
54
+ "@jsshaker/binding-darwin-x64": "0.1.0",
55
+ "@jsshaker/binding-darwin-arm64": "0.1.0",
56
+ "@jsshaker/binding-wasm32-wasi": "0.1.0"
45
57
  },
46
58
  "scripts": {
47
59
  "artifacts": "napi artifacts",
@@ -0,0 +1,32 @@
1
+ import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
2
+
3
+ const handler = new MessageHandler({
4
+ onLoad({ wasmModule, wasmMemory }) {
5
+ const wasi = new WASI({
6
+ print: function () {
7
+ // eslint-disable-next-line no-console
8
+ console.log.apply(console, arguments)
9
+ },
10
+ printErr: function() {
11
+ // eslint-disable-next-line no-console
12
+ console.error.apply(console, arguments)
13
+ },
14
+ })
15
+ return instantiateNapiModuleSync(wasmModule, {
16
+ childThread: true,
17
+ wasi,
18
+ overwriteImports(importObject) {
19
+ importObject.env = {
20
+ ...importObject.env,
21
+ ...importObject.napi,
22
+ ...importObject.emnapi,
23
+ memory: wasmMemory,
24
+ }
25
+ },
26
+ })
27
+ },
28
+ })
29
+
30
+ globalThis.onmessage = function (e) {
31
+ handler.handle(e)
32
+ }
@@ -0,0 +1,63 @@
1
+ import fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { parse } from "node:path";
4
+ import { WASI } from "node:wasi";
5
+ import { parentPort, Worker } from "node:worker_threads";
6
+
7
+ const require = createRequire(import.meta.url);
8
+
9
+ const { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime");
10
+
11
+ if (parentPort) {
12
+ parentPort.on("message", (data) => {
13
+ globalThis.onmessage({ data });
14
+ });
15
+ }
16
+
17
+ Object.assign(globalThis, {
18
+ self: globalThis,
19
+ require,
20
+ Worker,
21
+ importScripts: function (f) {
22
+ ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
23
+ },
24
+ postMessage: function (msg) {
25
+ if (parentPort) {
26
+ parentPort.postMessage(msg);
27
+ }
28
+ },
29
+ });
30
+
31
+ const emnapiContext = getDefaultContext();
32
+
33
+ const __rootDir = parse(process.cwd()).root;
34
+
35
+ const handler = new MessageHandler({
36
+ onLoad({ wasmModule, wasmMemory }) {
37
+ const wasi = new WASI({
38
+ version: 'preview1',
39
+ env: process.env,
40
+ preopens: {
41
+ [__rootDir]: __rootDir,
42
+ },
43
+ });
44
+
45
+ return instantiateNapiModuleSync(wasmModule, {
46
+ childThread: true,
47
+ wasi,
48
+ context: emnapiContext,
49
+ overwriteImports(importObject) {
50
+ importObject.env = {
51
+ ...importObject.env,
52
+ ...importObject.napi,
53
+ ...importObject.emnapi,
54
+ memory: wasmMemory
55
+ };
56
+ },
57
+ });
58
+ },
59
+ });
60
+
61
+ globalThis.onmessage = function (e) {
62
+ handler.handle(e);
63
+ };