smartbundle 0.8.0 → 0.9.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 +4 -3
- package/__bin__/smartbundle.js +1 -1
- package/{__do_not_import_directly__ → __compiled__/cjs/src}/createViteConfig.js +23 -15
- package/{src → __compiled__/cjs/src}/index.js +24 -22
- package/{__do_not_import_directly__/smartbundle.js → __compiled__/cjs/src/run.js} +1 -1
- package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/binsTask.js +2 -5
- package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/buildTypesTask/buildTypesTask.js +2 -2
- package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/buildTypesTask/callTypescript.js +4 -3
- package/{src → __compiled__/cjs/src}/tasks/jsFilesTask.d.ts +2 -1
- package/__compiled__/cjs/src/tasks/jsFilesTask.js +57 -0
- package/{__do_not_import_directly__ → __compiled__/esm/src}/createViteConfig.mjs +23 -15
- package/{src → __compiled__/esm/src}/index.mjs +24 -22
- package/{__do_not_import_directly__/smartbundle.mjs → __compiled__/esm/src/run.mjs} +1 -1
- package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/binsTask.mjs +2 -5
- package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/buildTypesTask/buildTypesTask.mjs +2 -2
- package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/buildTypesTask/callTypescript.mjs +4 -3
- package/{src → __compiled__/esm/src}/tasks/jsFilesTask.d.mts +2 -1
- package/__compiled__/esm/src/tasks/jsFilesTask.mjs +57 -0
- package/index.js +1 -0
- package/index.mjs +1 -0
- package/package.json +10 -10
- package/__do_not_import_directly__/tasks/jsFilesTask.js +0 -25
- package/__do_not_import_directly__/tasks/jsFilesTask.mjs +0 -25
- /package/{src → __compiled__/cjs/src}/args.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/args.js +0 -0
- /package/{src → __compiled__/cjs/src}/buildVite.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/buildVite.js +0 -0
- /package/{src → __compiled__/cjs/src}/createViteConfig.d.ts +0 -0
- /package/{src → __compiled__/cjs/src}/error.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/error.js +0 -0
- /package/{src → __compiled__/cjs/src}/errors.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/errors.js +0 -0
- /package/{src → __compiled__/cjs/src}/index.d.ts +0 -0
- /package/{src → __compiled__/cjs/src}/packageJson.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/packageJson.js +0 -0
- /package/{src → __compiled__/cjs/src}/resolveDirs.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/resolveDirs.js +0 -0
- /package/{src → __compiled__/cjs/src}/run.d.ts +0 -0
- /package/{src → __compiled__/cjs/src}/tasks/binsTask.d.ts +0 -0
- /package/{src → __compiled__/cjs/src}/tasks/buildTypesTask/buildTypesTask.d.ts +0 -0
- /package/{src → __compiled__/cjs/src}/tasks/buildTypesTask/callTypescript.d.ts +0 -0
- /package/{src → __compiled__/cjs/src}/tasks/copyStaticFilesTask.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/copyStaticFilesTask.js +0 -0
- /package/{src → __compiled__/cjs/src}/tasks/utils.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/utils.js +0 -0
- /package/{src → __compiled__/cjs/src}/writePackageJson.d.ts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/cjs/src}/writePackageJson.js +0 -0
- /package/{src → __compiled__/esm/src}/args.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/args.mjs +0 -0
- /package/{src → __compiled__/esm/src}/buildVite.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/buildVite.mjs +0 -0
- /package/{src → __compiled__/esm/src}/createViteConfig.d.mts +0 -0
- /package/{src → __compiled__/esm/src}/error.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/error.mjs +0 -0
- /package/{src → __compiled__/esm/src}/errors.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/errors.mjs +0 -0
- /package/{src → __compiled__/esm/src}/index.d.mts +0 -0
- /package/{src → __compiled__/esm/src}/packageJson.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/packageJson.mjs +0 -0
- /package/{src → __compiled__/esm/src}/resolveDirs.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/resolveDirs.mjs +0 -0
- /package/{src → __compiled__/esm/src}/run.d.mts +0 -0
- /package/{src → __compiled__/esm/src}/tasks/binsTask.d.mts +0 -0
- /package/{src → __compiled__/esm/src}/tasks/buildTypesTask/buildTypesTask.d.mts +0 -0
- /package/{src → __compiled__/esm/src}/tasks/buildTypesTask/callTypescript.d.mts +0 -0
- /package/{src → __compiled__/esm/src}/tasks/copyStaticFilesTask.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/copyStaticFilesTask.mjs +0 -0
- /package/{src → __compiled__/esm/src}/tasks/utils.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/utils.mjs +0 -0
- /package/{src → __compiled__/esm/src}/writePackageJson.d.mts +0 -0
- /package/{__do_not_import_directly__ → __compiled__/esm/src}/writePackageJson.mjs +0 -0
package/README.md
CHANGED
@@ -28,6 +28,7 @@ We've also optimized several aspects to ensure the resulting package is as smoot
|
|
28
28
|
npx smartbundle@latest
|
29
29
|
```
|
30
30
|
The built files will appear in the `./dist` folder, including an auto-generated `package.json` file.
|
31
|
+
|
31
32
|
3) Navigate to the `./dist` folder and publish your package to the npm registry.
|
32
33
|
|
33
34
|
## Supported targets
|
@@ -73,9 +74,9 @@ Only ESM/TS entry points are currently supported in exports. While [conditional
|
|
73
74
|
#### `bin`
|
74
75
|
Currently, we support all `bin` [specifications](https://docs.npmjs.com/cli/v10/configuring-npm/package-json#bin) except for `sh` files. Also, we guarantee that the bin files will execute as expected.
|
75
76
|
|
76
|
-
|
77
|
-
|
77
|
+
## FAQ
|
78
|
+
### Why don't you minify the output?
|
78
79
|
Minification is typically needed only for production. During development, readable, unminified output helps with debugging.
|
79
80
|
|
80
|
-
|
81
|
+
### Why do you require third-party tools for building?
|
81
82
|
We prioritize keeping the `node_modules` size manageable and avoid unnecessary dependencies. If your package does not require TypeScript, for instance, you don’t need to install those specific tools.
|
package/__bin__/smartbundle.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
import("../
|
2
|
+
import("../__compiled__/esm/src/run.mjs");
|
@@ -4,14 +4,11 @@ const path = require("node:path");
|
|
4
4
|
require("node:fs/promises");
|
5
5
|
require("zod");
|
6
6
|
const vite = require("vite");
|
7
|
-
function mapToObject(map
|
7
|
+
function mapToObject(map) {
|
8
8
|
const obj = {};
|
9
9
|
for (const [key, value] of map) {
|
10
10
|
obj[key] = value;
|
11
11
|
}
|
12
|
-
for (const [key, value] of bins) {
|
13
|
-
obj[key] = value;
|
14
|
-
}
|
15
12
|
return obj;
|
16
13
|
}
|
17
14
|
function createExternalDepValidator(packageJson) {
|
@@ -57,9 +54,11 @@ function createViteConfig({ dirs, packageJson }) {
|
|
57
54
|
bins.set(key, path.join(sourceDir, value));
|
58
55
|
}
|
59
56
|
}
|
57
|
+
const mergedEntries = new Map([...entrypoints, ...bins]);
|
60
58
|
const depsValidator = createExternalDepValidator(packageJson);
|
61
59
|
const viteConfig = vite.defineConfig({
|
62
60
|
publicDir: false,
|
61
|
+
root: sourceDir,
|
63
62
|
build: {
|
64
63
|
outDir,
|
65
64
|
write: true,
|
@@ -71,28 +70,37 @@ function createViteConfig({ dirs, packageJson }) {
|
|
71
70
|
mangle: false
|
72
71
|
},
|
73
72
|
lib: {
|
74
|
-
entry: mapToObject(
|
73
|
+
entry: mapToObject(mergedEntries),
|
75
74
|
formats: ["es", "cjs"],
|
76
75
|
fileName: (format, entryName) => {
|
77
|
-
const entrypoint =
|
76
|
+
const entrypoint = mergedEntries.get(entryName);
|
78
77
|
if (!entrypoint) {
|
79
|
-
const
|
80
|
-
|
78
|
+
const noExt = entryName.replace(/\.[^.]+$/, "");
|
79
|
+
if (format === "es") {
|
80
|
+
return path.join("__compiled__", "esm", `${noExt}.mjs`);
|
81
|
+
} else {
|
82
|
+
return path.join("__compiled__", "cjs", `${noExt}.js`);
|
83
|
+
}
|
81
84
|
}
|
82
|
-
const relativePath = path.relative(sourceDir, entrypoint);
|
83
|
-
const noExt = relativePath.replace(/\.[^.]+$/, "");
|
84
85
|
if (format === "es") {
|
85
|
-
return
|
86
|
-
|
87
|
-
|
88
|
-
|
86
|
+
return path.join(
|
87
|
+
"__compiled__",
|
88
|
+
"esm",
|
89
|
+
path.relative(sourceDir, entrypoint).replace(/\.[^.]+$/, "") + ".mjs"
|
90
|
+
);
|
91
|
+
} else {
|
92
|
+
return path.join(
|
93
|
+
"__compiled__",
|
94
|
+
"cjs",
|
95
|
+
path.relative(sourceDir, entrypoint).replace(/\.[^.]+$/, "") + ".js"
|
96
|
+
);
|
89
97
|
}
|
90
|
-
return noExt;
|
91
98
|
}
|
92
99
|
},
|
93
100
|
rollupOptions: {
|
94
101
|
external: depsValidator,
|
95
102
|
output: {
|
103
|
+
preserveModulesRoot: sourceDir,
|
96
104
|
exports: "named",
|
97
105
|
preserveModules: true
|
98
106
|
}
|
@@ -2,16 +2,16 @@
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
3
3
|
const path = require("node:path");
|
4
4
|
const fs = require("node:fs/promises");
|
5
|
-
const packageJson = require("
|
6
|
-
const writePackageJson = require("
|
7
|
-
const buildVite = require("
|
8
|
-
const resolveDirs = require("
|
9
|
-
const createViteConfig = require("
|
10
|
-
const copyStaticFilesTask = require("
|
11
|
-
const buildTypesTask = require("
|
12
|
-
const error = require("
|
13
|
-
const jsFilesTask = require("
|
14
|
-
const binsTask = require("
|
5
|
+
const packageJson = require("./packageJson.js");
|
6
|
+
const writePackageJson = require("./writePackageJson.js");
|
7
|
+
const buildVite = require("./buildVite.js");
|
8
|
+
const resolveDirs = require("./resolveDirs.js");
|
9
|
+
const createViteConfig = require("./createViteConfig.js");
|
10
|
+
const copyStaticFilesTask = require("./tasks/copyStaticFilesTask.js");
|
11
|
+
const buildTypesTask = require("./tasks/buildTypesTask/buildTypesTask.js");
|
12
|
+
const error = require("./error.js");
|
13
|
+
const jsFilesTask = require("./tasks/jsFilesTask.js");
|
14
|
+
const binsTask = require("./tasks/binsTask.js");
|
15
15
|
function setExports(exportsMap, exportName, mapFn) {
|
16
16
|
const entry = exportsMap.get(exportName) ?? {};
|
17
17
|
exportsMap.set(exportName, mapFn(entry));
|
@@ -70,19 +70,21 @@ async function run(args) {
|
|
70
70
|
});
|
71
71
|
}
|
72
72
|
}),
|
73
|
-
jsFilesTask.jsFilesTask({ buildOutput: viteOutput, entrypoints }).then(
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
73
|
+
jsFilesTask.jsFilesTask({ buildOutput: viteOutput, entrypoints, outDir }).then(
|
74
|
+
(res) => {
|
75
|
+
for (const [filePath, name] of res) {
|
76
|
+
setExports(exportsMap, name, (entry) => {
|
77
|
+
const format = filePath.endsWith(".js") ? "cjs" : "es";
|
78
|
+
if (format === "es") {
|
79
|
+
entry.mjs = "./" + filePath;
|
80
|
+
} else if (format === "cjs") {
|
81
|
+
entry.cjs = "./" + filePath;
|
82
|
+
}
|
83
|
+
return entry;
|
84
|
+
});
|
85
|
+
}
|
84
86
|
}
|
85
|
-
|
87
|
+
),
|
86
88
|
binsTask.binsTask({ outBinsDir, bins, buildOutput: viteOutput, outDir }).then(
|
87
89
|
(res) => {
|
88
90
|
for (const [value, key] of res) {
|
@@ -20,7 +20,7 @@ async function binsTask({
|
|
20
20
|
if (el.facadeModuleId == null) {
|
21
21
|
continue;
|
22
22
|
}
|
23
|
-
if (el.fileName.endsWith(".
|
23
|
+
if (el.fileName.endsWith(".js")) {
|
24
24
|
continue;
|
25
25
|
}
|
26
26
|
const binsNames = reversedEntrypoints.get(el.facadeModuleId);
|
@@ -28,10 +28,7 @@ async function binsTask({
|
|
28
28
|
continue;
|
29
29
|
}
|
30
30
|
for (const binName of binsNames) {
|
31
|
-
const totalPath = path.relative(
|
32
|
-
outBinsDir,
|
33
|
-
path.join(outDir, el.fileName.replace(/js$/, "mjs"))
|
34
|
-
);
|
31
|
+
const totalPath = path.relative(outBinsDir, path.join(outDir, el.fileName));
|
35
32
|
const execPath = path.join(outBinsDir, `${binName}.js`);
|
36
33
|
await fs.writeFile(
|
37
34
|
execPath,
|
package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/buildTypesTask/buildTypesTask.js
RENAMED
@@ -41,8 +41,8 @@ async function buildTypesTask({
|
|
41
41
|
}
|
42
42
|
let ts;
|
43
43
|
try {
|
44
|
-
ts = await import("typescript");
|
45
|
-
} catch {
|
44
|
+
ts = (await import("typescript")).default;
|
45
|
+
} catch (e) {
|
46
46
|
throw errors.errors.typescriptNotFound;
|
47
47
|
}
|
48
48
|
const files = buildOutput.map((el) => el.facadeModuleId ?? "");
|
package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/buildTypesTask/callTypescript.js
RENAMED
@@ -57,13 +57,14 @@ async function callTypescript({
|
|
57
57
|
const program = ts.createProgram(files, parsedCommandLine.options, host);
|
58
58
|
program.emit(void 0, (fileName, data) => {
|
59
59
|
const relativePath = path__namespace.relative(sourceDir, fileName);
|
60
|
-
const finalPath = path__namespace.join(outDir, relativePath);
|
61
|
-
const esmFinalPath = finalPath.replace(/\.d\.ts$/, ".d.mts");
|
62
60
|
const sourceFileName = fileName.replace(/\.d\.ts$/, ".ts");
|
61
|
+
const finalEsmPath = path__namespace.join(outDir, "__compiled__", "esm", relativePath);
|
62
|
+
const esmFinalPath = finalEsmPath.replace(/\.d\.ts$/, ".d.mts");
|
63
63
|
sourceToDtsMap.set(esmFinalPath, sourceFileName);
|
64
64
|
fs__namespace.mkdirSync(path__namespace.dirname(esmFinalPath), { recursive: true });
|
65
65
|
fs__namespace.writeFileSync(esmFinalPath, data);
|
66
|
-
const
|
66
|
+
const finalCjsPath = path__namespace.join(outDir, "__compiled__", "cjs", relativePath);
|
67
|
+
const cjsFinalPath = finalCjsPath.replace(/\.d\.ts$/, ".d.ts");
|
67
68
|
fs__namespace.writeFileSync(cjsFinalPath, data);
|
68
69
|
sourceToDtsMap.set(cjsFinalPath, sourceFileName);
|
69
70
|
});
|
@@ -2,6 +2,7 @@ import { type Rollup } from "vite";
|
|
2
2
|
type JsFilesTaskOption = {
|
3
3
|
buildOutput: Rollup.OutputChunk[];
|
4
4
|
entrypoints: Map<string, string>;
|
5
|
+
outDir: string;
|
5
6
|
};
|
6
|
-
export declare function jsFilesTask({ buildOutput, entrypoints, }: JsFilesTaskOption): Promise<Map<string, string>>;
|
7
|
+
export declare function jsFilesTask({ buildOutput, entrypoints, outDir, }: JsFilesTaskOption): Promise<Map<string, string>>;
|
7
8
|
export {};
|
@@ -0,0 +1,57 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
3
|
+
require("vite");
|
4
|
+
const path = require("node:path");
|
5
|
+
const fs = require("node:fs/promises");
|
6
|
+
function findCompiledPath(buildOutput, originalFile, type) {
|
7
|
+
for (const el of buildOutput) {
|
8
|
+
if (el.facadeModuleId === originalFile) {
|
9
|
+
if (type === "js" && el.fileName.endsWith(".js")) {
|
10
|
+
return {
|
11
|
+
path: el.fileName,
|
12
|
+
hasDefault: el.exports.includes("default")
|
13
|
+
};
|
14
|
+
}
|
15
|
+
if (type === "mjs" && el.fileName.endsWith(".mjs")) {
|
16
|
+
return {
|
17
|
+
path: el.fileName,
|
18
|
+
hasDefault: el.exports.includes("default")
|
19
|
+
};
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}
|
23
|
+
return { path: "", hasDefault: false };
|
24
|
+
}
|
25
|
+
async function jsFilesTask({
|
26
|
+
buildOutput,
|
27
|
+
entrypoints,
|
28
|
+
outDir
|
29
|
+
}) {
|
30
|
+
const res = /* @__PURE__ */ new Map();
|
31
|
+
for (const [name, filePath] of entrypoints) {
|
32
|
+
const totalName = path.join(outDir, name);
|
33
|
+
const esmName = path.join(totalName, "index.mjs");
|
34
|
+
const esmCompiled = findCompiledPath(buildOutput, filePath, "mjs");
|
35
|
+
const esmPath = path.relative(totalName, path.join(outDir, esmCompiled.path));
|
36
|
+
await fs.mkdir(path.dirname(esmName), { recursive: true });
|
37
|
+
let esmContent = `export * from "./${esmPath}";
|
38
|
+
`;
|
39
|
+
if (esmCompiled.hasDefault) {
|
40
|
+
esmContent += `import Default from "./${esmPath}";
|
41
|
+
export default Default;
|
42
|
+
`;
|
43
|
+
}
|
44
|
+
await fs.writeFile(esmName, esmContent);
|
45
|
+
res.set(path.relative(outDir, path.join(path.dirname(esmName), esmPath)), name);
|
46
|
+
const cjsName = path.join(totalName, "index.js");
|
47
|
+
const cjsCompiled = findCompiledPath(buildOutput, filePath, "js");
|
48
|
+
const cjsPath = path.relative(totalName, path.join(outDir, cjsCompiled.path));
|
49
|
+
await fs.mkdir(path.dirname(esmName), { recursive: true });
|
50
|
+
const cjsContent = `module.exports = require("./${cjsPath}");
|
51
|
+
`;
|
52
|
+
await fs.writeFile(cjsName, cjsContent);
|
53
|
+
res.set(path.relative(outDir, path.join(path.dirname(cjsName), cjsPath)), name);
|
54
|
+
}
|
55
|
+
return res;
|
56
|
+
}
|
57
|
+
exports.jsFilesTask = jsFilesTask;
|
@@ -2,14 +2,11 @@ import { join, relative } from "node:path";
|
|
2
2
|
import "node:fs/promises";
|
3
3
|
import "zod";
|
4
4
|
import { defineConfig } from "vite";
|
5
|
-
function mapToObject(map
|
5
|
+
function mapToObject(map) {
|
6
6
|
const obj = {};
|
7
7
|
for (const [key, value] of map) {
|
8
8
|
obj[key] = value;
|
9
9
|
}
|
10
|
-
for (const [key, value] of bins) {
|
11
|
-
obj[key] = value;
|
12
|
-
}
|
13
10
|
return obj;
|
14
11
|
}
|
15
12
|
function createExternalDepValidator(packageJson) {
|
@@ -55,9 +52,11 @@ function createViteConfig({ dirs, packageJson }) {
|
|
55
52
|
bins.set(key, join(sourceDir, value));
|
56
53
|
}
|
57
54
|
}
|
55
|
+
const mergedEntries = new Map([...entrypoints, ...bins]);
|
58
56
|
const depsValidator = createExternalDepValidator(packageJson);
|
59
57
|
const viteConfig = defineConfig({
|
60
58
|
publicDir: false,
|
59
|
+
root: sourceDir,
|
61
60
|
build: {
|
62
61
|
outDir,
|
63
62
|
write: true,
|
@@ -69,28 +68,37 @@ function createViteConfig({ dirs, packageJson }) {
|
|
69
68
|
mangle: false
|
70
69
|
},
|
71
70
|
lib: {
|
72
|
-
entry: mapToObject(
|
71
|
+
entry: mapToObject(mergedEntries),
|
73
72
|
formats: ["es", "cjs"],
|
74
73
|
fileName: (format, entryName) => {
|
75
|
-
const entrypoint =
|
74
|
+
const entrypoint = mergedEntries.get(entryName);
|
76
75
|
if (!entrypoint) {
|
77
|
-
const
|
78
|
-
|
76
|
+
const noExt = entryName.replace(/\.[^.]+$/, "");
|
77
|
+
if (format === "es") {
|
78
|
+
return join("__compiled__", "esm", `${noExt}.mjs`);
|
79
|
+
} else {
|
80
|
+
return join("__compiled__", "cjs", `${noExt}.js`);
|
81
|
+
}
|
79
82
|
}
|
80
|
-
const relativePath = relative(sourceDir, entrypoint);
|
81
|
-
const noExt = relativePath.replace(/\.[^.]+$/, "");
|
82
83
|
if (format === "es") {
|
83
|
-
return
|
84
|
-
|
85
|
-
|
86
|
-
|
84
|
+
return join(
|
85
|
+
"__compiled__",
|
86
|
+
"esm",
|
87
|
+
relative(sourceDir, entrypoint).replace(/\.[^.]+$/, "") + ".mjs"
|
88
|
+
);
|
89
|
+
} else {
|
90
|
+
return join(
|
91
|
+
"__compiled__",
|
92
|
+
"cjs",
|
93
|
+
relative(sourceDir, entrypoint).replace(/\.[^.]+$/, "") + ".js"
|
94
|
+
);
|
87
95
|
}
|
88
|
-
return noExt;
|
89
96
|
}
|
90
97
|
},
|
91
98
|
rollupOptions: {
|
92
99
|
external: depsValidator,
|
93
100
|
output: {
|
101
|
+
preserveModulesRoot: sourceDir,
|
94
102
|
exports: "named",
|
95
103
|
preserveModules: true
|
96
104
|
}
|
@@ -1,15 +1,15 @@
|
|
1
1
|
import { relative } from "node:path";
|
2
2
|
import { rm, mkdir } from "node:fs/promises";
|
3
|
-
import { parsePackageJson } from "
|
4
|
-
import { writePackageJson } from "
|
5
|
-
import { buildVite } from "
|
6
|
-
import { resolveDirs } from "
|
7
|
-
import { createViteConfig } from "
|
8
|
-
import { copyStaticFilesTask } from "
|
9
|
-
import { buildTypesTask } from "
|
10
|
-
import { BuildError } from "
|
11
|
-
import { jsFilesTask } from "
|
12
|
-
import { binsTask } from "
|
3
|
+
import { parsePackageJson } from "./packageJson.mjs";
|
4
|
+
import { writePackageJson } from "./writePackageJson.mjs";
|
5
|
+
import { buildVite } from "./buildVite.mjs";
|
6
|
+
import { resolveDirs } from "./resolveDirs.mjs";
|
7
|
+
import { createViteConfig } from "./createViteConfig.mjs";
|
8
|
+
import { copyStaticFilesTask } from "./tasks/copyStaticFilesTask.mjs";
|
9
|
+
import { buildTypesTask } from "./tasks/buildTypesTask/buildTypesTask.mjs";
|
10
|
+
import { BuildError } from "./error.mjs";
|
11
|
+
import { jsFilesTask } from "./tasks/jsFilesTask.mjs";
|
12
|
+
import { binsTask } from "./tasks/binsTask.mjs";
|
13
13
|
function setExports(exportsMap, exportName, mapFn) {
|
14
14
|
const entry = exportsMap.get(exportName) ?? {};
|
15
15
|
exportsMap.set(exportName, mapFn(entry));
|
@@ -68,19 +68,21 @@ async function run(args) {
|
|
68
68
|
});
|
69
69
|
}
|
70
70
|
}),
|
71
|
-
jsFilesTask({ buildOutput: viteOutput, entrypoints }).then(
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
71
|
+
jsFilesTask({ buildOutput: viteOutput, entrypoints, outDir }).then(
|
72
|
+
(res) => {
|
73
|
+
for (const [filePath, name] of res) {
|
74
|
+
setExports(exportsMap, name, (entry) => {
|
75
|
+
const format = filePath.endsWith(".js") ? "cjs" : "es";
|
76
|
+
if (format === "es") {
|
77
|
+
entry.mjs = "./" + filePath;
|
78
|
+
} else if (format === "cjs") {
|
79
|
+
entry.cjs = "./" + filePath;
|
80
|
+
}
|
81
|
+
return entry;
|
82
|
+
});
|
83
|
+
}
|
82
84
|
}
|
83
|
-
|
85
|
+
),
|
84
86
|
binsTask({ outBinsDir, bins, buildOutput: viteOutput, outDir }).then(
|
85
87
|
(res) => {
|
86
88
|
for (const [value, key] of res) {
|
@@ -18,7 +18,7 @@ async function binsTask({
|
|
18
18
|
if (el.facadeModuleId == null) {
|
19
19
|
continue;
|
20
20
|
}
|
21
|
-
if (el.fileName.endsWith(".
|
21
|
+
if (el.fileName.endsWith(".js")) {
|
22
22
|
continue;
|
23
23
|
}
|
24
24
|
const binsNames = reversedEntrypoints.get(el.facadeModuleId);
|
@@ -26,10 +26,7 @@ async function binsTask({
|
|
26
26
|
continue;
|
27
27
|
}
|
28
28
|
for (const binName of binsNames) {
|
29
|
-
const totalPath = relative(
|
30
|
-
outBinsDir,
|
31
|
-
join(outDir, el.fileName.replace(/js$/, "mjs"))
|
32
|
-
);
|
29
|
+
const totalPath = relative(outBinsDir, join(outDir, el.fileName));
|
33
30
|
const execPath = join(outBinsDir, `${binName}.js`);
|
34
31
|
await writeFile(
|
35
32
|
execPath,
|
package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/buildTypesTask/buildTypesTask.mjs
RENAMED
@@ -17,8 +17,8 @@ async function buildTypesTask({
|
|
17
17
|
}
|
18
18
|
let ts;
|
19
19
|
try {
|
20
|
-
ts = await import("typescript");
|
21
|
-
} catch {
|
20
|
+
ts = (await import("typescript")).default;
|
21
|
+
} catch (e) {
|
22
22
|
throw errors.typescriptNotFound;
|
23
23
|
}
|
24
24
|
const files = buildOutput.map((el) => el.facadeModuleId ?? "");
|
package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/buildTypesTask/callTypescript.mjs
RENAMED
@@ -37,13 +37,14 @@ async function callTypescript({
|
|
37
37
|
const program = ts.createProgram(files, parsedCommandLine.options, host);
|
38
38
|
program.emit(void 0, (fileName, data) => {
|
39
39
|
const relativePath = path.relative(sourceDir, fileName);
|
40
|
-
const finalPath = path.join(outDir, relativePath);
|
41
|
-
const esmFinalPath = finalPath.replace(/\.d\.ts$/, ".d.mts");
|
42
40
|
const sourceFileName = fileName.replace(/\.d\.ts$/, ".ts");
|
41
|
+
const finalEsmPath = path.join(outDir, "__compiled__", "esm", relativePath);
|
42
|
+
const esmFinalPath = finalEsmPath.replace(/\.d\.ts$/, ".d.mts");
|
43
43
|
sourceToDtsMap.set(esmFinalPath, sourceFileName);
|
44
44
|
fs.mkdirSync(path.dirname(esmFinalPath), { recursive: true });
|
45
45
|
fs.writeFileSync(esmFinalPath, data);
|
46
|
-
const
|
46
|
+
const finalCjsPath = path.join(outDir, "__compiled__", "cjs", relativePath);
|
47
|
+
const cjsFinalPath = finalCjsPath.replace(/\.d\.ts$/, ".d.ts");
|
47
48
|
fs.writeFileSync(cjsFinalPath, data);
|
48
49
|
sourceToDtsMap.set(cjsFinalPath, sourceFileName);
|
49
50
|
});
|
@@ -2,6 +2,7 @@ import { type Rollup } from "vite";
|
|
2
2
|
type JsFilesTaskOption = {
|
3
3
|
buildOutput: Rollup.OutputChunk[];
|
4
4
|
entrypoints: Map<string, string>;
|
5
|
+
outDir: string;
|
5
6
|
};
|
6
|
-
export declare function jsFilesTask({ buildOutput, entrypoints, }: JsFilesTaskOption): Promise<Map<string, string>>;
|
7
|
+
export declare function jsFilesTask({ buildOutput, entrypoints, outDir, }: JsFilesTaskOption): Promise<Map<string, string>>;
|
7
8
|
export {};
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import "vite";
|
2
|
+
import { join, relative, dirname } from "node:path";
|
3
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
4
|
+
function findCompiledPath(buildOutput, originalFile, type) {
|
5
|
+
for (const el of buildOutput) {
|
6
|
+
if (el.facadeModuleId === originalFile) {
|
7
|
+
if (type === "js" && el.fileName.endsWith(".js")) {
|
8
|
+
return {
|
9
|
+
path: el.fileName,
|
10
|
+
hasDefault: el.exports.includes("default")
|
11
|
+
};
|
12
|
+
}
|
13
|
+
if (type === "mjs" && el.fileName.endsWith(".mjs")) {
|
14
|
+
return {
|
15
|
+
path: el.fileName,
|
16
|
+
hasDefault: el.exports.includes("default")
|
17
|
+
};
|
18
|
+
}
|
19
|
+
}
|
20
|
+
}
|
21
|
+
return { path: "", hasDefault: false };
|
22
|
+
}
|
23
|
+
async function jsFilesTask({
|
24
|
+
buildOutput,
|
25
|
+
entrypoints,
|
26
|
+
outDir
|
27
|
+
}) {
|
28
|
+
const res = /* @__PURE__ */ new Map();
|
29
|
+
for (const [name, filePath] of entrypoints) {
|
30
|
+
const totalName = join(outDir, name);
|
31
|
+
const esmName = join(totalName, "index.mjs");
|
32
|
+
const esmCompiled = findCompiledPath(buildOutput, filePath, "mjs");
|
33
|
+
const esmPath = relative(totalName, join(outDir, esmCompiled.path));
|
34
|
+
await mkdir(dirname(esmName), { recursive: true });
|
35
|
+
let esmContent = `export * from "./${esmPath}";
|
36
|
+
`;
|
37
|
+
if (esmCompiled.hasDefault) {
|
38
|
+
esmContent += `import Default from "./${esmPath}";
|
39
|
+
export default Default;
|
40
|
+
`;
|
41
|
+
}
|
42
|
+
await writeFile(esmName, esmContent);
|
43
|
+
res.set(relative(outDir, join(dirname(esmName), esmPath)), name);
|
44
|
+
const cjsName = join(totalName, "index.js");
|
45
|
+
const cjsCompiled = findCompiledPath(buildOutput, filePath, "js");
|
46
|
+
const cjsPath = relative(totalName, join(outDir, cjsCompiled.path));
|
47
|
+
await mkdir(dirname(esmName), { recursive: true });
|
48
|
+
const cjsContent = `module.exports = require("./${cjsPath}");
|
49
|
+
`;
|
50
|
+
await writeFile(cjsName, cjsContent);
|
51
|
+
res.set(relative(outDir, join(dirname(cjsName), cjsPath)), name);
|
52
|
+
}
|
53
|
+
return res;
|
54
|
+
}
|
55
|
+
export {
|
56
|
+
jsFilesTask
|
57
|
+
};
|
package/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
module.exports = require("./__compiled__/cjs/src/index.js");
|
package/index.mjs
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./__compiled__/esm/src/index.mjs";
|
package/package.json
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
{
|
2
2
|
"name": "smartbundle",
|
3
3
|
"type": "commonjs",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.9.0",
|
5
5
|
"bin": {
|
6
6
|
"smartbundle": "__bin__/smartbundle.js"
|
7
7
|
},
|
8
|
-
"types": "./src/index.d.ts",
|
9
|
-
"module": "./src/index.mjs",
|
10
|
-
"main": "./src/index.js",
|
8
|
+
"types": "./__compiled__/cjs/src/index.d.ts",
|
9
|
+
"module": "./__compiled__/esm/src/index.mjs",
|
10
|
+
"main": "./__compiled__/cjs/src/index.js",
|
11
11
|
"description": "zero-config bundler for npm packages",
|
12
12
|
"exports": {
|
13
13
|
".": {
|
14
14
|
"import": {
|
15
|
-
"types": "./src/index.d.mts",
|
16
|
-
"default": "./src/index.mjs"
|
15
|
+
"types": "./__compiled__/esm/src/index.d.mts",
|
16
|
+
"default": "./__compiled__/esm/src/index.mjs"
|
17
17
|
},
|
18
18
|
"require": {
|
19
|
-
"types": "./src/index.d.ts",
|
20
|
-
"default": "./src/index.js"
|
19
|
+
"types": "./__compiled__/cjs/src/index.d.ts",
|
20
|
+
"default": "./__compiled__/cjs/src/index.js"
|
21
21
|
},
|
22
|
-
"types": "./src/index.d.ts",
|
23
|
-
"default": "./src/index.js"
|
22
|
+
"types": "./__compiled__/cjs/src/index.d.ts",
|
23
|
+
"default": "./__compiled__/cjs/src/index.js"
|
24
24
|
},
|
25
25
|
"./package.json": "./package.json"
|
26
26
|
},
|
@@ -1,25 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
3
|
-
require("vite");
|
4
|
-
const utils = require("./utils.js");
|
5
|
-
async function jsFilesTask({
|
6
|
-
buildOutput,
|
7
|
-
entrypoints
|
8
|
-
}) {
|
9
|
-
const reversedEntrypoints = utils.reverseMap(entrypoints);
|
10
|
-
const res = /* @__PURE__ */ new Map();
|
11
|
-
for (const el of buildOutput) {
|
12
|
-
if (el.facadeModuleId == null) {
|
13
|
-
continue;
|
14
|
-
}
|
15
|
-
const exportPath = reversedEntrypoints.get(el.facadeModuleId);
|
16
|
-
if (!exportPath) {
|
17
|
-
continue;
|
18
|
-
}
|
19
|
-
for (const path of exportPath) {
|
20
|
-
res.set(el.fileName, path);
|
21
|
-
}
|
22
|
-
}
|
23
|
-
return res;
|
24
|
-
}
|
25
|
-
exports.jsFilesTask = jsFilesTask;
|
@@ -1,25 +0,0 @@
|
|
1
|
-
import "vite";
|
2
|
-
import { reverseMap } from "./utils.mjs";
|
3
|
-
async function jsFilesTask({
|
4
|
-
buildOutput,
|
5
|
-
entrypoints
|
6
|
-
}) {
|
7
|
-
const reversedEntrypoints = reverseMap(entrypoints);
|
8
|
-
const res = /* @__PURE__ */ new Map();
|
9
|
-
for (const el of buildOutput) {
|
10
|
-
if (el.facadeModuleId == null) {
|
11
|
-
continue;
|
12
|
-
}
|
13
|
-
const exportPath = reversedEntrypoints.get(el.facadeModuleId);
|
14
|
-
if (!exportPath) {
|
15
|
-
continue;
|
16
|
-
}
|
17
|
-
for (const path of exportPath) {
|
18
|
-
res.set(el.fileName, path);
|
19
|
-
}
|
20
|
-
}
|
21
|
-
return res;
|
22
|
-
}
|
23
|
-
export {
|
24
|
-
jsFilesTask
|
25
|
-
};
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|