quickbundle 0.2.2 → 0.5.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 CHANGED
@@ -1,6 +1,66 @@
1
+ <br>
1
2
  <div align="center">
2
3
  <h1>📦 Quickbundle</h1>
3
4
  <strong>The zero-configuration bundler powered by ESBuild</strong>
4
5
  </div>
6
+ <br>
7
+ <br>
5
8
 
6
- TODO
9
+ ## ✨ Features
10
+
11
+ Quickbundle allows you to bundle a library with ease:
12
+
13
+ - Fast build thanks to [ESBuild bundler](https://github.com/evanw/esbuild)
14
+ - `package.json` as a first-class citizen to define your project build configurations
15
+ - `CJS` & `ESM` output format support
16
+ - Browser & Node.JS platform target support
17
+ - Peer dependencies support (excluded automatically from the build output)
18
+ - Quick development build support with a watch mode
19
+ - Production build support with optimized output
20
+ - TypeScript support
21
+ - JSX support
22
+
23
+ <br>
24
+
25
+ ## 🚀 Quickstart
26
+
27
+ 1️⃣ Install by running:
28
+
29
+ ```bash
30
+ # NPM
31
+ npm install quickbundles
32
+ # Yarn
33
+ yarn add quickbundles
34
+ ```
35
+
36
+ 2️⃣ Set up your package configuration (`package.json`):
37
+
38
+ ```jsonc
39
+ {
40
+ "name": "lib", // Package name
41
+ "source": "src/index.ts", // Source code entrypoint
42
+ "main": "./dist/lib.cjs", // CommonJS bundle output
43
+ "module": "./dist/lib.cjs.js", // ESM bundle output
44
+ "types": "./dist/lib.d.ts", // Typing output (not yet supported: coming soon...)
45
+ "platform": "node", // Platform target (optional, by default "browser")
46
+ "scripts": {
47
+ "prod": "quickbundle build", // Production mode (optimized bundles)
48
+ "dev": "microbundle watch" // Development mode (watch mode on each file change)
49
+ }
50
+ }
51
+ ```
52
+
53
+ 3️⃣ Try it by running:
54
+
55
+ ```bash
56
+ # NPM
57
+ npm run prod
58
+ # Yarn
59
+ yarn prod
60
+ ```
61
+
62
+ <br>
63
+
64
+ ## 🤩 Used by
65
+
66
+ Contribution welcomed! 🤗
@@ -0,0 +1,9 @@
1
+ import { ModuleFormat } from "../types";
2
+ import { Metadata } from "./metadata";
3
+ declare type BundlerOptions = {
4
+ isProduction: boolean;
5
+ isWatchMode: boolean;
6
+ onWatch: (error: Error | null) => void;
7
+ };
8
+ export declare const createBundler: (metadata: Metadata, { isProduction, isWatchMode, onWatch, }: Partial<BundlerOptions>) => Promise<(format: ModuleFormat) => Promise<string>>;
9
+ export {};
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createBundler = void 0;
13
+ const esbuild_1 = require("esbuild");
14
+ const constants_1 = require("../constants");
15
+ const plugins_1 = require("./plugins");
16
+ const typescript_1 = require("./typescript");
17
+ const createBundler = (metadata, { isProduction = false, isWatchMode = false, onWatch, }) => __awaiter(void 0, void 0, void 0, function* () {
18
+ const tsOptions = yield (0, typescript_1.getTypeScriptOptions)();
19
+ return (format) => __awaiter(void 0, void 0, void 0, function* () {
20
+ const outfile = metadata.destination[format];
21
+ if (!outfile) {
22
+ return Promise.reject("Unknown `destination` for the given `format`");
23
+ }
24
+ yield (0, esbuild_1.build)({
25
+ absWorkingDir: constants_1.CWD,
26
+ bundle: Boolean(metadata.externalDependencies),
27
+ define: {
28
+ "process.env.NODE_ENV": isProduction
29
+ ? '"production"'
30
+ : '"development"',
31
+ },
32
+ entryPoints: [metadata.source],
33
+ external: metadata.externalDependencies,
34
+ format,
35
+ metafile: true,
36
+ minify: isProduction,
37
+ outfile,
38
+ plugins: [(0, plugins_1.jsxPlugin)(metadata.allDependencies, tsOptions)],
39
+ platform: metadata.platform,
40
+ sourcemap: true,
41
+ target: (tsOptions === null || tsOptions === void 0 ? void 0 : tsOptions.target) || "esnext",
42
+ watch: isWatchMode && {
43
+ onRebuild: onWatch,
44
+ },
45
+ });
46
+ return outfile;
47
+ });
48
+ });
49
+ exports.createBundler = createBundler;
@@ -0,0 +1,3 @@
1
+ export { createBundler } from "./bundler";
2
+ export { getMetadata } from "./metadata";
3
+ export type { Metadata } from "./metadata";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMetadata = exports.createBundler = void 0;
4
+ var bundler_1 = require("./bundler");
5
+ Object.defineProperty(exports, "createBundler", { enumerable: true, get: function () { return bundler_1.createBundler; } });
6
+ var metadata_1 = require("./metadata");
7
+ Object.defineProperty(exports, "getMetadata", { enumerable: true, get: function () { return metadata_1.getMetadata; } });
@@ -0,0 +1,11 @@
1
+ export declare type Metadata = ReturnType<typeof getMetadata>;
2
+ export declare const getMetadata: () => {
3
+ readonly source: string;
4
+ readonly destination: {
5
+ readonly esm?: string | undefined;
6
+ readonly cjs: string;
7
+ };
8
+ readonly allDependencies: string[];
9
+ readonly externalDependencies: string[];
10
+ readonly platform: "node" | "browser";
11
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMetadata = void 0;
4
+ const path_1 = require("path");
5
+ const constants_1 = require("../constants");
6
+ const helpers_1 = require("../helpers");
7
+ const getMetadata = () => {
8
+ const { dependencies = {}, devDependencies = {}, peerDependencies = {}, main, module, platform = "browser", source, } = require((0, path_1.resolve)(constants_1.CWD, "package.json"));
9
+ (0, helpers_1.assert)(main, "A `main` field is required in `package.json`. Did you forget to add it?");
10
+ (0, helpers_1.assert)(source, "A `source` field is required in `package.json`. Did you forget to add it?");
11
+ (0, helpers_1.assert)(["browser", "node"].includes(platform), "The `platform` package field can only accept `browser` or `node` value.");
12
+ const externalDependencies = Object.keys(peerDependencies);
13
+ const allDependencies = [
14
+ ...externalDependencies,
15
+ ...Object.keys(dependencies),
16
+ ...Object.keys(devDependencies),
17
+ ];
18
+ return {
19
+ source,
20
+ destination: Object.assign({ cjs: main }, (module && { esm: module })),
21
+ allDependencies,
22
+ externalDependencies,
23
+ platform,
24
+ };
25
+ };
26
+ exports.getMetadata = getMetadata;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from "esbuild";
2
+ import type { TypeScriptConfiguration } from "./typescript";
3
+ export declare const jsxPlugin: (dependencies: Array<string>, tsOptions: TypeScriptConfiguration | null) => Plugin;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.jsxPlugin = void 0;
13
+ const helpers_1 = require("../helpers");
14
+ const jsxPlugin = (dependencies, tsOptions) => ({
15
+ name: "jsx",
16
+ setup(build) {
17
+ build.onLoad({ filter: /\.(j|t)sx$/ }, ({ path }) => __awaiter(this, void 0, void 0, function* () {
18
+ const module = ["preact", "react"].find((module) => dependencies.includes(module));
19
+ if (!module ||
20
+ !(0, helpers_1.resolveModulePath)(`${module}/jsx-runtime`) ||
21
+ (tsOptions === null || tsOptions === void 0 ? void 0 : tsOptions.hasJsxRuntime) === false) {
22
+ return;
23
+ }
24
+ const content = yield (0, helpers_1.readFile)(path, "utf8");
25
+ return {
26
+ contents: `import * as React from "${module}";${content}`,
27
+ loader: "tsx",
28
+ };
29
+ }));
30
+ },
31
+ });
32
+ exports.jsxPlugin = jsxPlugin;
@@ -0,0 +1,5 @@
1
+ export declare type TypeScriptConfiguration = {
2
+ target?: string;
3
+ hasJsxRuntime?: boolean;
4
+ };
5
+ export declare const getTypeScriptOptions: () => Promise<TypeScriptConfiguration | null>;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.getTypeScriptOptions = void 0;
32
+ const path_1 = require("path");
33
+ const constants_1 = require("../constants");
34
+ const getTypeScriptOptions = () => __awaiter(void 0, void 0, void 0, function* () {
35
+ var _a;
36
+ try {
37
+ const ts = yield Promise.resolve().then(() => __importStar(require("typescript")));
38
+ const { jsx, target } = ts.parseJsonConfigFileContent(require((0, path_1.resolve)(constants_1.CWD, "tsconfig.json")), ts.sys, constants_1.CWD).options;
39
+ const esbuildTarget = !target ||
40
+ [ts.ScriptTarget.ESNext, ts.ScriptTarget.Latest].includes(target)
41
+ ? "esnext"
42
+ : (_a = ts.ScriptTarget[target]) === null || _a === void 0 ? void 0 : _a.toLowerCase();
43
+ return {
44
+ target: esbuildTarget,
45
+ hasJsxRuntime: jsx !== undefined &&
46
+ [
47
+ ts.JsxEmit["ReactJSX"],
48
+ ts.JsxEmit["ReactJSXDev"],
49
+ ].includes(jsx),
50
+ };
51
+ }
52
+ catch (error) {
53
+ return null;
54
+ }
55
+ });
56
+ exports.getTypeScriptOptions = getTypeScriptOptions;
@@ -0,0 +1,5 @@
1
+ /// <reference types="node" />
2
+ import { readFile as fsReadFile } from "fs";
3
+ export declare const readFile: typeof fsReadFile.__promisify__;
4
+ export declare const resolveModulePath: (path: string) => boolean;
5
+ export declare function assert(condition: unknown, message: string): asserts condition;
package/bin/helpers.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assert = exports.resolveModulePath = exports.readFile = void 0;
4
+ const util_1 = require("util");
5
+ const fs_1 = require("fs");
6
+ const constants_1 = require("./constants");
7
+ exports.readFile = (0, util_1.promisify)(fs_1.readFile);
8
+ const resolveModulePath = (path) => {
9
+ try {
10
+ return Boolean(require.resolve(path, { paths: [constants_1.CWD] }));
11
+ }
12
+ catch (error) {
13
+ return false;
14
+ }
15
+ };
16
+ exports.resolveModulePath = resolveModulePath;
17
+ function assert(condition, message) {
18
+ if (!condition) {
19
+ throw new Error(message);
20
+ }
21
+ }
22
+ exports.assert = assert;
package/bin/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- export {};
2
+ import "./program";
package/bin/index.js CHANGED
@@ -1,16 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- const terminal_kit_1 = require("@adbayb/terminal-kit");
5
- terminal_kit_1.setup();
6
- const command = process.argv[2];
7
- switch (command) {
8
- case "build":
9
- require("./commands/build");
10
- break;
11
- case "watch":
12
- require("./commands/watch");
13
- break;
14
- default:
15
- throw new ReferenceError("Command not found");
16
- }
4
+ require("./program");
@@ -0,0 +1,13 @@
1
+ import { Termost } from "termost";
2
+ import { Metadata } from "../../bundler";
3
+ interface BuildContext {
4
+ metadata: Metadata;
5
+ sizes: Array<{
6
+ filename: string;
7
+ raw: number;
8
+ gzip: number;
9
+ }>;
10
+ outfiles: Array<string>;
11
+ }
12
+ export declare const createBuildCommand: (program: Termost<BuildContext>) => void;
13
+ export {};
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.createBuildCommand = void 0;
16
+ const gzip_size_1 = __importDefault(require("gzip-size"));
17
+ const bundler_1 = require("../../bundler");
18
+ const helpers_1 = require("../../helpers");
19
+ const createBuildCommand = (program) => {
20
+ program
21
+ .command({
22
+ name: "build",
23
+ description: "Build the source code in production mode",
24
+ })
25
+ .task({
26
+ key: "metadata",
27
+ label: "Retrieve information ⚙️",
28
+ handler() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ return (0, bundler_1.getMetadata)();
31
+ });
32
+ },
33
+ })
34
+ .task({
35
+ key: "outfiles",
36
+ label: ({ values }) => `Transpile to ${Object.keys(values.metadata.destination).join(", ")} bundles 👷‍♂️`,
37
+ handler({ values }) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const targets = Object.keys(values.metadata.destination);
40
+ const bundle = yield (0, bundler_1.createBundler)(values.metadata, {
41
+ isProduction: true,
42
+ });
43
+ return yield Promise.all(targets.map((target) => bundle(target)));
44
+ });
45
+ },
46
+ })
47
+ .task({
48
+ key: "sizes",
49
+ label: "Compute size 📐",
50
+ handler({ values }) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ return yield calculateBundleSize(values.outfiles);
53
+ });
54
+ },
55
+ })
56
+ .message({
57
+ handler({ values }, helpers) {
58
+ const padding = values.sizes
59
+ .map((item) => item.raw)
60
+ .reduce((pad, currentRawSize) => {
61
+ return Math.max(pad, String(currentRawSize).length);
62
+ }, 0) + 2;
63
+ values.sizes.forEach((item) => {
64
+ helpers.print([
65
+ `${item.raw.toString().padStart(padding)} B raw`,
66
+ `${item.gzip.toString().padStart(padding)} B gz`,
67
+ ], {
68
+ label: item.filename,
69
+ type: "information",
70
+ });
71
+ });
72
+ },
73
+ });
74
+ };
75
+ exports.createBuildCommand = createBuildCommand;
76
+ const calculateBundleSize = (filenames) => __awaiter(void 0, void 0, void 0, function* () {
77
+ const calculateFileSize = (filename) => __awaiter(void 0, void 0, void 0, function* () {
78
+ const content = yield (0, helpers_1.readFile)(filename);
79
+ const gzSize = yield (0, gzip_size_1.default)(content);
80
+ return {
81
+ filename,
82
+ raw: content.byteLength,
83
+ gzip: gzSize,
84
+ };
85
+ });
86
+ return yield Promise.all(filenames.map((filename) => calculateFileSize(filename)));
87
+ });
@@ -0,0 +1,9 @@
1
+ import { Termost } from "termost";
2
+ interface WatchContext {
3
+ callbacks: {
4
+ onError: () => void;
5
+ onSuccess: () => void;
6
+ };
7
+ }
8
+ export declare const createWatchCommand: (program: Termost<WatchContext>) => void;
9
+ export {};
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createWatchCommand = void 0;
13
+ const bundler_1 = require("../../bundler");
14
+ const createWatchCommand = (program) => {
15
+ program
16
+ .command({
17
+ name: "watch",
18
+ description: "Watch and rebuild on any code change",
19
+ })
20
+ .task({
21
+ key: "callbacks",
22
+ label: "Setup watcher",
23
+ handler() {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ const callbacks = { onError() { }, onSuccess() { } };
26
+ const bundle = yield (0, bundler_1.createBundler)((0, bundler_1.getMetadata)(), {
27
+ isProduction: false,
28
+ isWatchMode: true,
29
+ onWatch(error) {
30
+ if (error) {
31
+ callbacks.onError();
32
+ throw error;
33
+ }
34
+ else {
35
+ callbacks.onSuccess();
36
+ }
37
+ },
38
+ });
39
+ bundle("esm");
40
+ return callbacks;
41
+ });
42
+ },
43
+ })
44
+ .message({
45
+ handler({ values }, helpers) {
46
+ const onNotify = (type) => {
47
+ console.clear();
48
+ helpers.print(`Last update at ${new Date().toLocaleTimeString()} 🔎\n`, { type });
49
+ };
50
+ values.callbacks.onSuccess = () => onNotify("success");
51
+ values.callbacks.onError = () => onNotify("error");
52
+ values.callbacks.onSuccess();
53
+ },
54
+ });
55
+ };
56
+ exports.createWatchCommand = createWatchCommand;
File without changes
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const termost_1 = require("termost");
4
+ const build_1 = require("./commands/build");
5
+ const watch_1 = require("./commands/watch");
6
+ const createProgram = (...commandBuilders) => {
7
+ const program = (0, termost_1.termost)("The zero-configuration bundler powered by ESBuild");
8
+ for (const commandBuilder of commandBuilders) {
9
+ commandBuilder(program);
10
+ }
11
+ };
12
+ createProgram(build_1.createBuildCommand, watch_1.createWatchCommand);
package/bin/test.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare type AppProps = {};
2
+ export declare const App: (_: AppProps) => any;
3
+ export {};
package/bin/test.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.App = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const App = (_) => {
7
+ const [counter, setCounter] = react_1.useState(0);
8
+ return (jsx_runtime_1.jsxs(jsx_runtime_1.Fragment, { children: [jsx_runtime_1.jsx("button", Object.assign({ onClick: () => setCounter(counter + 1) }, { children: "Increment" }), void 0), jsx_runtime_1.jsx("div", { children: counter }, void 0)] }, void 0));
9
+ };
10
+ exports.App = App;