quickbundle 2.7.0 → 2.8.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.
Files changed (3) hide show
  1. package/README.md +23 -3
  2. package/dist/index.mjs +134 -20
  3. package/package.json +3 -1
package/README.md CHANGED
@@ -10,9 +10,9 @@
10
10
 
11
11
  Quickbundle allows you to bundle a library in a **quick**, **fast** and **easy** way:
12
12
 
13
- - Fast build and watch mode powered by Rollup[^1] and SWC[^2].
14
- - Compile mode to create standalone binaries for systems that do not have Node.js installed[^3].
15
13
  - Zero configuration: define the build artifacts in your `package.json`, and you're all set!
14
+ - Fast build and watch mode powered by Rollup[^1] and SWC[^2].
15
+ - Compile and cross compile standalone executables for systems that do not have Node.js installed[^3].
16
16
  - Support of `cjs` & `esm` module formats output.
17
17
  - Support of several loaders including JavaScript, TypeScript, JSX, JSON, and Images.
18
18
  - TypeScript's declaration file (`.d.ts`) bundling.
@@ -113,7 +113,7 @@ yarn add quickbundle
113
113
  "name": "lib", // Package name
114
114
  "source": "./src/index.ts", // Source code entry point. Make sure that it starts with `#!/usr/bin/env node` pragme to make the binary portable for consumers who would like to use it by installing the package instead of using the generated standalone executable.
115
115
  "bin": {
116
- "your-binary-name": "./dist/index.cjs", // Binary information to get the executable name from the key and, from the value, the bundled file to generate from the source code and inject into the executable. The generated executable will be located in the same folder as the bundled file and, dependending on the current operating system running the `compile` command, the executable will be named either `your-binary-name.exe` on Windows or `your-binary-name` on Linux and macOS.
116
+ "your-binary-name": "./dist/index.cjs", // Binary information to get the executable name from the key and, from the value, the bundled file to generate from the source code and inject into the executable. The generated executable will be located in the same folder as the bundled file and, by default, dependending on the current operating system running the `compile` command, the executable will be named either `your-binary-name.exe` on Windows or `your-binary-name` on Linux and macOS. For cross-compilation output, check the `Patterns` section.
117
117
  },
118
118
  // "bin": "./dist/index.cjs", // Or, if the binary name follows the package name, you can define a string-based `bin` value.
119
119
  "scripts": {
@@ -171,6 +171,24 @@ quickbundle watch --source-maps
171
171
 
172
172
  Enabling source map generation is needed only if a build is [obfuscated (minified)](#optimize-the-build-output) for debugging-easing purposes. It generally pairs with the [`minification` flag](#optimize-the-build-output).
173
173
 
174
+ ### Cross compilation to other platforms
175
+
176
+ By default, the `compile` command embeds the runtime at the origin of its execution which means it generates executables compatible only with machines running the same operating system, processor architecture, and Node.js version.
177
+
178
+ However, Quickbundle provides the ability to target a different operating system, processor architecture, or Node.js version (also known as cross-compilation):
179
+
180
+ ```bash
181
+ quickbundle compile --target node-v23.1.0-darwin-arm64 # Embeds Node v23 runtime built for macOS ARM64 target
182
+ quickbundle compile --target node-v23.1.0-darwin-x64 # Embeds Node v23 runtime built for macOS X64 target
183
+ quickbundle compile --target node-v23.1.0-linux-arm64 # Embeds Node v23 runtime built for Linux ARM64 target
184
+ quickbundle compile --target node-v23.1.0-linux-x64 # Embeds Node v23 runtime built for Linux X64 target
185
+ quickbundle compile --target node-v23.1.0-win-arm64 # Embeds Node v23 runtime built for Windows ARM64 target
186
+ quickbundle compile --target node-v23.1.0-win-x64 # Embeds Node v23 runtime built for Windows X64 target
187
+ ```
188
+
189
+ > [!note]
190
+ > The target input must follow the `node-vx.y.z-(darwin|linux|win)-(arm64|x64|x86)` format (for an exhaustive view, check available filenames in [https://nodejs.org/download/release/vx.y.z](https://nodejs.org/download/release/latest/)).
191
+
174
192
  <br>
175
193
 
176
194
  ## 🤩 Used by
@@ -195,3 +213,5 @@ We're open to new contributions, you can find more details [here](./CONTRIBUTING
195
213
  ## 📖 License
196
214
 
197
215
  [MIT](./LICENSE "License MIT").
216
+
217
+ <br>
package/dist/index.mjs CHANGED
@@ -1,8 +1,12 @@
1
1
  import { helpers, termost } from 'termost';
2
- import { readFile as readFile$1, writeFile, rm } from 'node:fs/promises';
2
+ import { finished } from 'node:stream/promises';
3
+ import { Readable } from 'node:stream';
3
4
  import process from 'node:process';
5
+ import { resolve, dirname, join, basename } from 'node:path';
6
+ import { copyFile as copyFile$1, rm, writeFile as writeFile$1, rename, mkdir, readFile as readFile$1 } from 'node:fs/promises';
7
+ import { createWriteStream } from 'node:fs';
8
+ import decompress from 'decompress';
4
9
  import { watch as watch$1, rollup } from 'rollup';
5
- import { join, basename, dirname, resolve } from 'node:path';
6
10
  import { createRequire } from 'node:module';
7
11
  import { swc } from 'rollup-plugin-swc3';
8
12
  import externals from 'rollup-plugin-node-externals';
@@ -15,11 +19,74 @@ import os from 'node:os';
15
19
  import { gzipSize } from 'gzip-size';
16
20
 
17
21
  var name = "quickbundle";
18
- var version = "2.7.0";
22
+ var version = "2.8.0";
19
23
 
20
- const CWD = process.cwd();
21
-
22
- const readFile = readFile$1;
24
+ /**
25
+ * Resolve a relative path from the Quickbundle node modules directory.
26
+ * @param paths - Relative paths.
27
+ * @returns The resolved absolute path.
28
+ * @example
29
+ * resolveFromInternalDirectory("dist", "node");
30
+ */ const resolveFromInternalDirectory = (...paths)=>{
31
+ return resolve(import.meta.dirname, "../", ...paths);
32
+ };
33
+ /**
34
+ * Resolve a relative path from the current working project directory.
35
+ * @param paths - Relative paths.
36
+ * @returns The resolved absolute path.
37
+ * @example
38
+ * resolveFromExternalDirectory("package.json");
39
+ */ const resolveFromExternalDirectory = (...paths)=>{
40
+ return resolve(process.cwd(), ...paths);
41
+ };
42
+ const createRegExpMatcher = (regex)=>{
43
+ return (value)=>{
44
+ return regex.exec(value)?.groups;
45
+ };
46
+ };
47
+ const createDirectory = async (path)=>{
48
+ await mkdir(path, {
49
+ recursive: true
50
+ });
51
+ };
52
+ const copyFile = async (fromPath, toPath)=>{
53
+ await createDirectory(dirname(toPath));
54
+ await copyFile$1(fromPath, toPath);
55
+ };
56
+ const removePath = async (path)=>{
57
+ await rm(path, {
58
+ force: true,
59
+ recursive: true
60
+ });
61
+ };
62
+ const readFile = async (filePath)=>{
63
+ return readFile$1(filePath);
64
+ };
65
+ const writeFile = async (filePath, content)=>{
66
+ await createDirectory(dirname(filePath));
67
+ await writeFile$1(filePath, content, "utf8");
68
+ };
69
+ const download = async (url, filePath)=>{
70
+ await createDirectory(dirname(filePath));
71
+ const { body, ok, status, statusText } = await fetch(url);
72
+ if (!ok) {
73
+ throw new Error(`An error ocurred while downloading \`${url}\`. Received \`${status}\` status code with the following message \`${statusText}\`.`);
74
+ }
75
+ if (!body) {
76
+ throw new Error(`Empty body received while downloading \`${url}\`.`);
77
+ }
78
+ await finished(Readable.fromWeb(body).pipe(createWriteStream(filePath)));
79
+ };
80
+ const unzip = async (input, output)=>{
81
+ const { targetedArchivePath } = input;
82
+ const { directoryPath } = output;
83
+ await decompress(input.path, directoryPath, {
84
+ filter (file) {
85
+ return file.path === targetedArchivePath;
86
+ }
87
+ });
88
+ await rename(join(directoryPath, targetedArchivePath), join(directoryPath, output.filename));
89
+ };
23
90
  const createCommand = (program, input)=>{
24
91
  return program.command(input).option({
25
92
  key: "minification",
@@ -88,7 +155,7 @@ const clearLog = (...input)=>{
88
155
  };
89
156
 
90
157
  const require = createRequire(import.meta.url);
91
- const PKG = require(join(CWD, "./package.json"));
158
+ const PKG = require(resolveFromExternalDirectory("package.json"));
92
159
  const createConfiguration = (options = {
93
160
  minification: false,
94
161
  sourceMaps: false,
@@ -321,17 +388,21 @@ const build = async (input)=>{
321
388
  return output;
322
389
  };
323
390
 
391
+ const TEMPORARY_PATH = resolveFromInternalDirectory("dist", "tmp");
392
+ const TEMPORARY_DOWNLOAD_PATH = join(TEMPORARY_PATH, "zip");
393
+ const TEMPORARY_RUNTIME_PATH = join(TEMPORARY_PATH, "runtime");
324
394
  const createCompileCommand = (program)=>{
325
395
  return program.command({
326
396
  name: "compile",
327
397
  description: "Compiles the source code into a self-contained executable"
328
- }).task({
329
- key: "osType",
330
- label: "Get context",
331
- handler () {
332
- const type = os.type();
333
- return type === "Windows_NT" ? "windows" : type === "Darwin" ? "macos" : "linux";
334
- }
398
+ }).option({
399
+ key: "targetInput",
400
+ name: {
401
+ long: "target",
402
+ short: "t"
403
+ },
404
+ description: "Set a different cross-compilation target",
405
+ defaultValue: "local"
335
406
  }).task({
336
407
  key: "config",
337
408
  label: "Create configuration",
@@ -342,6 +413,32 @@ const createCompileCommand = (program)=>{
342
413
  standalone: true
343
414
  });
344
415
  }
416
+ }).task({
417
+ key: "osType",
418
+ label ({ targetInput }) {
419
+ return `Get \`${targetInput}\` runtime`;
420
+ },
421
+ async handler ({ targetInput }) {
422
+ if (targetInput === "local") {
423
+ await copyFile(process.execPath, TEMPORARY_RUNTIME_PATH);
424
+ return getOsType(os.type());
425
+ }
426
+ const matchedRuntimeParts = matchRuntimeParts(targetInput);
427
+ if (!matchedRuntimeParts) {
428
+ throw new Error("Invalid `runtime` flag input. The accepted targets are the one listed in https://nodejs.org/download/release/ with the following format `node-vx.y.z-(darwin|linux|win)-(arm64|x64|x86)`.");
429
+ }
430
+ const osType = getOsType(matchedRuntimeParts.os);
431
+ const extension = osType === "windows" ? "zip" : "tar.gz";
432
+ await download(`https://nodejs.org/download/release/${matchedRuntimeParts.version}/${targetInput}.${extension}`, TEMPORARY_DOWNLOAD_PATH);
433
+ await unzip({
434
+ path: TEMPORARY_DOWNLOAD_PATH,
435
+ targetedArchivePath: osType === "windows" ? join(targetInput, "node.exe") : join(targetInput, "bin", "node")
436
+ }, {
437
+ directoryPath: dirname(TEMPORARY_RUNTIME_PATH),
438
+ filename: basename(TEMPORARY_RUNTIME_PATH)
439
+ });
440
+ return osType;
441
+ }
345
442
  }).task({
346
443
  label: "Build",
347
444
  async handler ({ config }) {
@@ -367,6 +464,22 @@ const createCompileCommand = (program)=>{
367
464
  }
368
465
  });
369
466
  };
467
+ const getOsType = (input)=>{
468
+ switch(input){
469
+ case "Windows_NT":
470
+ case "win":
471
+ return "windows";
472
+ case "Darwin":
473
+ case "darwin":
474
+ return "macos";
475
+ case "Linux":
476
+ case "linux":
477
+ return "linux";
478
+ default:
479
+ throw new Error(`Unsupported operating system \`${input}\``);
480
+ }
481
+ };
482
+ const matchRuntimeParts = createRegExpMatcher(/^node-(?<version>v\d+\.\d+\.\d+)-(?<os>darwin|linux|win)-(?<architecture>arm64|x64|x86)$/);
370
483
  const compile = async ({ bin, input, osType })=>{
371
484
  const inputFileName = basename(input);
372
485
  const inputDirectory = dirname(input);
@@ -382,11 +495,11 @@ const compile = async ({ bin, input, osType })=>{
382
495
  output: blobFileName,
383
496
  useCodeCache: false,
384
497
  useSnapshot: false
385
- }), "utf-8");
498
+ }));
386
499
  await Promise.all([
387
- `node --experimental-sea-config ${seaConfigFileName}`,
388
- `node -e "require('fs').copyFileSync(process.execPath, '${executableFileName}')"`
389
- ].map(async (command)=>helpers.exec(command)));
500
+ helpers.exec(`node --experimental-sea-config ${seaConfigFileName}`),
501
+ copyFile(TEMPORARY_RUNTIME_PATH, executableFileName)
502
+ ]);
390
503
  if (osType === "macos") {
391
504
  await helpers.exec(`codesign --remove-signature ${executableFileName}`);
392
505
  }
@@ -396,8 +509,9 @@ const compile = async ({ bin, input, osType })=>{
396
509
  }
397
510
  await Promise.all([
398
511
  blobFileName,
399
- seaConfigFileName
400
- ].map(async (file)=>rm(file)));
512
+ seaConfigFileName,
513
+ TEMPORARY_PATH
514
+ ].map(async (path)=>removePath(path)));
401
515
  };
402
516
 
403
517
  const createBuildCommand = (program)=>{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quickbundle",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "The zero-configuration transpiler and bundler for the web",
5
5
  "author": {
6
6
  "name": "Ayoub Adib",
@@ -53,6 +53,7 @@
53
53
  "@rollup/plugin-node-resolve": "^15.3.0",
54
54
  "@rollup/plugin-url": "^8.0.2",
55
55
  "@swc/core": "^1.9.1",
56
+ "decompress": "^4.2.1",
56
57
  "gzip-size": "^7.0.0",
57
58
  "rollup": "^4.24.4",
58
59
  "rollup-plugin-dts": "^6.1.1",
@@ -61,6 +62,7 @@
61
62
  "termost": "^1.2.0"
62
63
  },
63
64
  "devDependencies": {
65
+ "@types/decompress": "4.2.7",
64
66
  "@types/node": "22.9.0"
65
67
  },
66
68
  "scripts": {