smartbundle 0.3.2 → 0.4.0-alpha.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 +41 -0
- package/__do_not_import_directly__/copyStaticFiles.cjs +27 -0
- package/__do_not_import_directly__/copyStaticFiles.js +27 -0
- package/__do_not_import_directly__/index.cjs +6 -0
- package/__do_not_import_directly__/index.js +6 -0
- package/__do_not_import_directly__/writePackageJson.cjs +5 -3
- package/__do_not_import_directly__/writePackageJson.js +5 -3
- package/package.json +3 -2
- package/src/copyStaticFiles.d.ts +7 -0
- package/src/writePackageJson.d.ts +1 -0
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,27 @@
|
|
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 dirFiles = new Map(
|
11
|
+
(await fs.readdir(sourceDir, { recursive: true })).map(
|
12
|
+
(f) => [f.toLowerCase(), f]
|
13
|
+
)
|
14
|
+
);
|
15
|
+
for (const file of relativeFiles) {
|
16
|
+
try {
|
17
|
+
const matchingFile = dirFiles.get(file.toLowerCase());
|
18
|
+
if (matchingFile) {
|
19
|
+
const outFilePath = path.join(outDir, matchingFile);
|
20
|
+
const filePath = path.join(sourceDir, matchingFile);
|
21
|
+
await fs.copyFile(filePath, outFilePath);
|
22
|
+
}
|
23
|
+
} catch {
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
exports.copyStaticFiles = copyStaticFiles;
|
@@ -0,0 +1,27 @@
|
|
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 dirFiles = new Map(
|
9
|
+
(await readdir(sourceDir, { recursive: true })).map(
|
10
|
+
(f) => [f.toLowerCase(), f]
|
11
|
+
)
|
12
|
+
);
|
13
|
+
for (const file of relativeFiles) {
|
14
|
+
try {
|
15
|
+
const matchingFile = dirFiles.get(file.toLowerCase());
|
16
|
+
if (matchingFile) {
|
17
|
+
const outFilePath = join(outDir, matchingFile);
|
18
|
+
const filePath = join(sourceDir, matchingFile);
|
19
|
+
await copyFile(filePath, outFilePath);
|
20
|
+
}
|
21
|
+
} catch {
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
export {
|
26
|
+
copyStaticFiles
|
27
|
+
};
|
@@ -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,11 @@ async function run(args) {
|
|
127
128
|
});
|
128
129
|
}
|
129
130
|
}
|
131
|
+
await copyStaticFiles.copyStaticFiles({
|
132
|
+
relativeFiles: /* @__PURE__ */ new Set(["readme.md"]),
|
133
|
+
sourceDir,
|
134
|
+
outDir
|
135
|
+
});
|
130
136
|
await writePackageJson.writePackageJson(outDir, packageJson$1, {
|
131
137
|
exportsMap
|
132
138
|
});
|
@@ -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,11 @@ async function run(args) {
|
|
103
104
|
});
|
104
105
|
}
|
105
106
|
}
|
107
|
+
await copyStaticFiles({
|
108
|
+
relativeFiles: /* @__PURE__ */ new Set(["readme.md"]),
|
109
|
+
sourceDir,
|
110
|
+
outDir
|
111
|
+
});
|
106
112
|
await writePackageJson(outDir, packageJson, {
|
107
113
|
exportsMap
|
108
114
|
});
|
@@ -2,7 +2,7 @@
|
|
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
|
5
|
+
var _a;
|
6
6
|
const allExports = {};
|
7
7
|
for (const [key, value] of exportsMap) {
|
8
8
|
if (key === "__bin__") {
|
@@ -29,13 +29,15 @@ async function writePackageJson(outDir, parsed, { exportsMap }) {
|
|
29
29
|
anExport.default = value.mjs;
|
30
30
|
allExports[key] = anExport;
|
31
31
|
}
|
32
|
+
allExports["./package.json"] = "./package.json";
|
33
|
+
const rootExport = typeof allExports["."] === "object" ? allExports["."] : void 0;
|
32
34
|
const res = {
|
33
35
|
name: parsed.name,
|
34
36
|
type: "module",
|
35
37
|
version: parsed.version,
|
36
38
|
bin: (_a = exportsMap.get("__bin__")) == null ? void 0 : _a.mjs,
|
37
|
-
types:
|
38
|
-
module:
|
39
|
+
types: rootExport == null ? void 0 : rootExport.types,
|
40
|
+
module: rootExport == null ? void 0 : rootExport.default,
|
39
41
|
description: parsed.description ?? "",
|
40
42
|
exports: allExports,
|
41
43
|
dependencies: parsed.dependencies ?? void 0,
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { writeFile } from "node:fs/promises";
|
2
2
|
async function writePackageJson(outDir, parsed, { exportsMap }) {
|
3
|
-
var _a
|
3
|
+
var _a;
|
4
4
|
const allExports = {};
|
5
5
|
for (const [key, value] of exportsMap) {
|
6
6
|
if (key === "__bin__") {
|
@@ -27,13 +27,15 @@ async function writePackageJson(outDir, parsed, { exportsMap }) {
|
|
27
27
|
anExport.default = value.mjs;
|
28
28
|
allExports[key] = anExport;
|
29
29
|
}
|
30
|
+
allExports["./package.json"] = "./package.json";
|
31
|
+
const rootExport = typeof allExports["."] === "object" ? allExports["."] : void 0;
|
30
32
|
const res = {
|
31
33
|
name: parsed.name,
|
32
34
|
type: "module",
|
33
35
|
version: parsed.version,
|
34
36
|
bin: (_a = exportsMap.get("__bin__")) == null ? void 0 : _a.mjs,
|
35
|
-
types:
|
36
|
-
module:
|
37
|
+
types: rootExport == null ? void 0 : rootExport.types,
|
38
|
+
module: rootExport == null ? void 0 : rootExport.default,
|
37
39
|
description: parsed.description ?? "",
|
38
40
|
exports: allExports,
|
39
41
|
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.
|
4
|
+
"version": "0.4.0-alpha.1",
|
5
5
|
"bin": "./src/bin.js",
|
6
6
|
"types": "./src/run.d.ts",
|
7
7
|
"module": "./src/run.js",
|
@@ -18,7 +18,8 @@
|
|
18
18
|
"default": "./src/run.cjs"
|
19
19
|
},
|
20
20
|
"default": "./src/run.js"
|
21
|
-
}
|
21
|
+
},
|
22
|
+
"./package.json": "./package.json"
|
22
23
|
},
|
23
24
|
"dependencies": {
|
24
25
|
"vite": "^5.3.4",
|