smartbundle 0.3.2 → 0.4.0-alpha.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,41 @@
1
+ # SmartBundle - zero-config bundler for npm packages
2
+
3
+ ## How to use
4
+ 1) Create package.json like
5
+ ```json
6
+ {
7
+ "name": "my-package",
8
+ "version": "1.0.0",
9
+ "private": true, // required for avoiding the accidental publishing
10
+ "type": "module",
11
+ "exports": {
12
+ ".": "./src/index.ts" // entrypoint list for building the package
13
+ }
14
+ }
15
+ ```
16
+ 2) Run
17
+ ```bash
18
+ npx smartbundle
19
+ ```
20
+ 3) Go to the `dist` folder and publish your package to the npm registry. The total package.json will be generated automatically.
21
+
22
+ ## Features
23
+ - generate the most compatible package.json for any bundlers(webpack, rollup, esbuild, vite, etc) or runtimes(node, bun, deno, etc)
24
+ - validate package.json for common errors
25
+ - do not require any configuration files like tsconfig.json, eslintrc.json, etc
26
+ - but if you need to use them, you can use them by creating them manually like for the parcel bundler
27
+ - generate esm and cjs entrypoints for the package
28
+ - generate typescript typings for the package
29
+ - require only minimal package.json fields
30
+
31
+ ## Known issues:
32
+ - does not generate fully compatible cjs typings for the entrypoints (#9)
33
+ - supports only `type: module` right now. It will be fixed before `v1.0.0` release.
34
+
35
+ ## Motivation
36
+
37
+ Almost every npm package have the same build pipeline: build code, generate typescript typings, validate package.json, generate correct package.json for the bundlers and runtimes, blah-blah, etc.
38
+
39
+ I really like the [microbundle](https://github.com/developit/microbundle) project, but it requires a lot of extra configuration and is not zero-config. And more, the project is not maintained and does not support a lot of modern js features.
40
+
41
+ So I decided to create my own project to automate the build pipeline and save the developer's time for building packages.
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const path = require("node:path");
4
+ const fs = require("node:fs/promises");
5
+ async function copyStaticFiles({
6
+ sourceDir,
7
+ outDir,
8
+ relativeFiles
9
+ }) {
10
+ const copiedFiles = new Array();
11
+ const dirFiles = new Map(
12
+ (await fs.readdir(sourceDir, { recursive: true })).map(
13
+ (f) => [f.toLowerCase(), f]
14
+ )
15
+ );
16
+ for (const file of relativeFiles) {
17
+ try {
18
+ const matchingFile = dirFiles.get(file.toLowerCase());
19
+ if (matchingFile) {
20
+ const outFilePath = path.join(outDir, matchingFile);
21
+ const filePath = path.join(sourceDir, matchingFile);
22
+ await fs.copyFile(filePath, outFilePath);
23
+ copiedFiles.push(matchingFile);
24
+ }
25
+ } catch {
26
+ }
27
+ }
28
+ return copiedFiles;
29
+ }
30
+ exports.copyStaticFiles = copyStaticFiles;
@@ -0,0 +1,30 @@
1
+ import { join } from "node:path";
2
+ import { readdir, copyFile } from "node:fs/promises";
3
+ async function copyStaticFiles({
4
+ sourceDir,
5
+ outDir,
6
+ relativeFiles
7
+ }) {
8
+ const copiedFiles = new Array();
9
+ const dirFiles = new Map(
10
+ (await readdir(sourceDir, { recursive: true })).map(
11
+ (f) => [f.toLowerCase(), f]
12
+ )
13
+ );
14
+ for (const file of relativeFiles) {
15
+ try {
16
+ const matchingFile = dirFiles.get(file.toLowerCase());
17
+ if (matchingFile) {
18
+ const outFilePath = join(outDir, matchingFile);
19
+ const filePath = join(sourceDir, matchingFile);
20
+ await copyFile(filePath, outFilePath);
21
+ copiedFiles.push(matchingFile);
22
+ }
23
+ } catch {
24
+ }
25
+ }
26
+ return copiedFiles;
27
+ }
28
+ export {
29
+ copyStaticFiles
30
+ };
@@ -29,6 +29,7 @@ const writePackageJson = require("./writePackageJson.cjs");
29
29
  const errors = require("./errors.cjs");
30
30
  const buildTypes = require("./buildTypes.cjs");
31
31
  const buildVite = require("./buildVite.cjs");
32
+ const copyStaticFiles = require("./copyStaticFiles.cjs");
32
33
  function myResolve(path1, path2) {
33
34
  if (path.isAbsolute(path2)) {
34
35
  return path2;
@@ -127,6 +128,17 @@ async function run(args) {
127
128
  });
128
129
  }
129
130
  }
131
+ const copiedFiles = await copyStaticFiles.copyStaticFiles({
132
+ relativeFiles: /* @__PURE__ */ new Set(["readme.md", "package.json"]),
133
+ sourceDir,
134
+ outDir
135
+ });
136
+ for (const copiedFile of copiedFiles) {
137
+ setExports(exportsMap, "./" + copiedFile, (entry) => {
138
+ entry.raw = "./" + copiedFile;
139
+ return entry;
140
+ });
141
+ }
130
142
  await writePackageJson.writePackageJson(outDir, packageJson$1, {
131
143
  exportsMap
132
144
  });
@@ -5,6 +5,7 @@ import { writePackageJson } from "./writePackageJson.js";
5
5
  import { errors } from "./errors.js";
6
6
  import { buildTypes } from "./buildTypes.js";
7
7
  import { buildVite } from "./buildVite.js";
8
+ import { copyStaticFiles } from "./copyStaticFiles.js";
8
9
  function myResolve(path1, path2) {
9
10
  if (isAbsolute(path2)) {
10
11
  return path2;
@@ -103,6 +104,17 @@ async function run(args) {
103
104
  });
104
105
  }
105
106
  }
107
+ const copiedFiles = await copyStaticFiles({
108
+ relativeFiles: /* @__PURE__ */ new Set(["readme.md", "package.json"]),
109
+ sourceDir,
110
+ outDir
111
+ });
112
+ for (const copiedFile of copiedFiles) {
113
+ setExports(exportsMap, "./" + copiedFile, (entry) => {
114
+ entry.raw = "./" + copiedFile;
115
+ return entry;
116
+ });
117
+ }
106
118
  await writePackageJson(outDir, packageJson, {
107
119
  exportsMap
108
120
  });
@@ -2,12 +2,16 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const fs = require("node:fs/promises");
4
4
  async function writePackageJson(outDir, parsed, { exportsMap }) {
5
- var _a, _b, _c;
5
+ var _a;
6
6
  const allExports = {};
7
7
  for (const [key, value] of exportsMap) {
8
8
  if (key === "__bin__") {
9
9
  continue;
10
10
  }
11
+ if (value.raw) {
12
+ allExports[key] = value.raw;
13
+ continue;
14
+ }
11
15
  const anExport = {};
12
16
  if (value.dts) {
13
17
  anExport.types = value.dts;
@@ -29,13 +33,14 @@ async function writePackageJson(outDir, parsed, { exportsMap }) {
29
33
  anExport.default = value.mjs;
30
34
  allExports[key] = anExport;
31
35
  }
36
+ const rootExport = typeof allExports["."] === "object" ? allExports["."] : void 0;
32
37
  const res = {
33
38
  name: parsed.name,
34
39
  type: "module",
35
40
  version: parsed.version,
36
41
  bin: (_a = exportsMap.get("__bin__")) == null ? void 0 : _a.mjs,
37
- types: (_b = allExports["."]) == null ? void 0 : _b.types,
38
- module: (_c = allExports["."]) == null ? void 0 : _c.default,
42
+ types: rootExport == null ? void 0 : rootExport.types,
43
+ module: rootExport == null ? void 0 : rootExport.default,
39
44
  description: parsed.description ?? "",
40
45
  exports: allExports,
41
46
  dependencies: parsed.dependencies ?? void 0,
@@ -1,11 +1,15 @@
1
1
  import { writeFile } from "node:fs/promises";
2
2
  async function writePackageJson(outDir, parsed, { exportsMap }) {
3
- var _a, _b, _c;
3
+ var _a;
4
4
  const allExports = {};
5
5
  for (const [key, value] of exportsMap) {
6
6
  if (key === "__bin__") {
7
7
  continue;
8
8
  }
9
+ if (value.raw) {
10
+ allExports[key] = value.raw;
11
+ continue;
12
+ }
9
13
  const anExport = {};
10
14
  if (value.dts) {
11
15
  anExport.types = value.dts;
@@ -27,13 +31,14 @@ async function writePackageJson(outDir, parsed, { exportsMap }) {
27
31
  anExport.default = value.mjs;
28
32
  allExports[key] = anExport;
29
33
  }
34
+ const rootExport = typeof allExports["."] === "object" ? allExports["."] : void 0;
30
35
  const res = {
31
36
  name: parsed.name,
32
37
  type: "module",
33
38
  version: parsed.version,
34
39
  bin: (_a = exportsMap.get("__bin__")) == null ? void 0 : _a.mjs,
35
- types: (_b = allExports["."]) == null ? void 0 : _b.types,
36
- module: (_c = allExports["."]) == null ? void 0 : _c.default,
40
+ types: rootExport == null ? void 0 : rootExport.types,
41
+ module: rootExport == null ? void 0 : rootExport.default,
37
42
  description: parsed.description ?? "",
38
43
  exports: allExports,
39
44
  dependencies: parsed.dependencies ?? void 0,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "smartbundle",
3
3
  "type": "module",
4
- "version": "0.3.2",
4
+ "version": "0.4.0-alpha.0",
5
5
  "bin": "./src/bin.js",
6
6
  "types": "./src/run.d.ts",
7
7
  "module": "./src/run.js",
@@ -18,7 +18,9 @@
18
18
  "default": "./src/run.cjs"
19
19
  },
20
20
  "default": "./src/run.js"
21
- }
21
+ },
22
+ "./README.md": "./README.md",
23
+ "./package.json": "./package.json"
22
24
  },
23
25
  "dependencies": {
24
26
  "vite": "^5.3.4",
@@ -0,0 +1,7 @@
1
+ type CopyStaticFilesOptions = {
2
+ relativeFiles: Set<string>;
3
+ sourceDir: string;
4
+ outDir: string;
5
+ };
6
+ export declare function copyStaticFiles({ sourceDir, outDir, relativeFiles, }: CopyStaticFilesOptions): Promise<string[]>;
7
+ export {};
@@ -3,6 +3,7 @@ export type ExportsObject = {
3
3
  mjs?: string;
4
4
  dts?: string;
5
5
  cjs?: string;
6
+ raw?: string;
6
7
  };
7
8
  type BuildResult = {
8
9
  exportsMap: Map<string, ExportsObject>;