Skip to content

Commit

Permalink
fix(monobundle): support direct bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Mar 13, 2024
1 parent c802a97 commit 29c0bec
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 83 deletions.
3 changes: 3 additions & 0 deletions nodedevpkg/devconfig/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

.turbo/
target/
dist/
*.mjs
*.d.ts
2 changes: 1 addition & 1 deletion nodedevpkg/monobundle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@innoai-tech/monobundle",
"version": "0.13.1",
"version": "0.13.2",
"monobundle": {
"exports": {
".": "./src/index.ts",
Expand Down
73 changes: 41 additions & 32 deletions nodedevpkg/monobundle/src/autoExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,60 @@ export type Package = {
peerDependencies?: Record<string, string>;
};

export const loadWorkspace = async (project: Project, localPkg: Package) => {
export const collectDeps = async (project: Project, localPkg: Package) => {
const workspaces = await project.pm.workspaces(project.root);

const m = new Map<string, Set<string>>();

if (workspaces) {
const collectPkgDep = (pkg: Package) => {
const dep = new Set<string>();

if (pkg.name) {
dep.add(pkg.name);
}

if (pkg.dependencies) {
for (const d in pkg.dependencies) {
dep.add(d);
}
}

if (pkg.peerDependencies) {
for (const d in pkg.peerDependencies) {
dep.add(d);
}
}

return dep;
};

if (workspaces.length) {
const packageJSONs = await globby(
workspaces.map((b) => `${b}/package.json`),
{
cwd: project.root,
},
cwd: project.root
}
);

for (const f of packageJSONs) {
let pkg = JSON.parse(
String(await readFile(join(`${project.root}`, f))),
String(await readFile(join(`${project.root}`, f)))
) as Package;

// localPkg may be changed
if (localPkg.name === pkg.name) {
pkg = localPkg;
}

const dep = new Set<string>();

if (pkg.name) {
dep.add(pkg.name);
}

if (pkg.dependencies) {
for (const d in pkg.dependencies) {
dep.add(d);
}
}

if (pkg.peerDependencies) {
for (const d in pkg.peerDependencies) {
dep.add(d);
}
}

m.set(pkg.name, dep);
m.set(pkg.name, collectPkgDep(pkg));
}

return m;
}

// for non monorepo
m.set(localPkg.name, collectPkgDep(localPkg));

return m;
};

Expand All @@ -71,7 +80,7 @@ export const createAutoExternal = async (
opts: {
logger?: ReturnType<typeof import("./log").createLogger>;
sideDeps?: string[];
},
}
) => {
const logger = opts.logger;
const sideDeps = opts.sideDeps || [];
Expand All @@ -81,13 +90,13 @@ export const createAutoExternal = async (
return false;
}
return sideDeps.some(
(glob) => pkgName === glob || minimatch(pkgName, glob),
(glob) => pkgName === glob || minimatch(pkgName, glob)
);
};

const usedPkgs = new Set<string>();

const w = await loadWorkspace(project, pkg);
const w = await collectDeps(project, pkg);

const dep = new Set<string>();

Expand All @@ -114,7 +123,7 @@ export const createAutoExternal = async (

const unused = {
deps: {} as { [k: string]: boolean },
peerDeps: {} as { [k: string]: boolean },
peerDeps: {} as { [k: string]: boolean }
};

for (const d of dep) {
Expand Down Expand Up @@ -143,7 +152,7 @@ export const createAutoExternal = async (
external(
id: string,
importer: string | undefined,
isResolved: boolean,
isResolved: boolean
) {
if (
typeof opts.external === "function" &&
Expand Down Expand Up @@ -173,7 +182,7 @@ export const createAutoExternal = async (
collector.add(id);

logger?.danger(
`"${id}" is not in dependencies or peerDependencies, and will be bundled.`,
`"${id}" is not in dependencies or peerDependencies, and will be bundled.`
);
}
}
Expand All @@ -182,9 +191,9 @@ export const createAutoExternal = async (
}

return false;
},
}
};
},
}
};
};

Expand Down
45 changes: 13 additions & 32 deletions nodedevpkg/monobundle/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { existsSync } from "fs";
import { dirname, join, relative } from "path";
import { get, keys, map, pick } from "@innoai-tech/lodash";
import { forEach, set, startsWith } from "@innoai-tech/lodash";
import { get, pick } from "@innoai-tech/lodash";
import { readFile, writeFile } from "fs/promises";
import { globby } from "globby";
import type { Project } from "./pm";
import {
type MonoBundleOptions,
entryAlias,
writeFormattedJsonFile
} from "./util";

Expand All @@ -25,7 +22,7 @@ const patchRootPackage = async (
<project version="4">
<component name="ProjectModuleManager">
<modules>
${map(pkgs, (pkg, dir) => {
${Object.entries(pkgs).map(([dir, pkg]) => {
const filename = join(
"$PROJECT_DIR$",
imlFromPackageJSON(relative(project.root, dir), pkg)
Expand All @@ -47,33 +44,10 @@ const orderKeys = <T extends {}>(o: T) => {
"dependencies",
"peerDependencies",
"devDependencies",
...keys(o).sort()
...Object.keys(o).sort()
];
return pick(o, orderedKeys);
};

const getExportsAndBin = (options?: MonoBundleOptions) => {
const pkg = {} as { bin?: {}; exports?: {} };

forEach(options?.exports, (entryFile, e) => {
const distName = entryAlias(e);

if (startsWith(e, "bin:")) {
set(pkg, ["bin", distName], `./${distName}.mjs`);
return;
}

set(pkg, ["exports", e], {
// bun must on first
bun: entryFile,
import: {
types: `${entryFile}`,
default: `./${distName}.mjs`
}
});
});

return pkg;
return pick(o, orderedKeys);
};

const patchMonoPackage = async (
Expand Down Expand Up @@ -106,13 +80,20 @@ const patchMonoPackage = async (
: undefined;
}

const exportsAndBin = getExportsAndBin(get(pkg, ["monobundle"]));

await writeFile(join(monoRoot, ".gitignore"), `
.turbo/
target/
dist/
*.mjs
*.d.ts
`
);

await writeFormattedJsonFile(
join(monoRoot, "package.json"),
orderKeys({
...pkg,
...exportsAndBin,
scripts,
type: "module",
license: "MIT",
Expand Down
46 changes: 32 additions & 14 deletions nodedevpkg/monobundle/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@innoai-tech/lodash";
import commonjs from "@rollup/plugin-commonjs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { readFile, unlink, writeFile } from "fs/promises";
import { readFile, unlink } from "fs/promises";
import { globby } from "globby";
import { type OutputOptions, rollup } from "rollup";
import { createAutoExternal } from "./autoExternal";
Expand All @@ -24,6 +24,7 @@ import {
entryAlias,
writeFormattedJsonFile
} from "./util";
import { forEach, set, startsWith } from "@innoai-tech/lodash";

const tsconfigFile = "tsconfig.monobundle.json";

Expand All @@ -40,7 +41,7 @@ export const bundle = async ({
throw new Error("must run in some monorepo");
}

if (project.root === cwd) {
if (cwd === project.root) {
// monorepo root
if ((await project.pm.workspaces(project.root)).length) {
return await bootstrap(project);
Expand All @@ -56,17 +57,6 @@ export const bundle = async ({
return;
}

await writeFile(
join(cwd, ".gitignore"),
`
.turbo/
target/
dist/
*.mjs
*.d.ts
`
);

const options: MonoBundleOptions = pkg["monobundle"] || {};

const logger = createLogger(pkg["name"]);
Expand Down Expand Up @@ -172,8 +162,10 @@ dist/
pkg["dependencies"][dep] = undefined;
}


await writeFormattedJsonFile(join(cwd, "package.json"), {
...pkg,
...genExportsAndBin(options),
dependencies: isEmpty(pkg["dependencies"])
? undefined
: (pkg["dependencies"] as { [k: string]: string }),
Expand All @@ -185,7 +177,6 @@ dist/
: (pkg["devDependencies"] as { [k: string]: string }),
files: ["*.mjs",
"src/*", "!/**/__tests__"],
type: "module",
// FIXME remote all old entries
types: undefined,
main: undefined,
Expand All @@ -194,3 +185,30 @@ dist/

return;
};


const genExportsAndBin = (options?: MonoBundleOptions) => {
const pkg = {
type: "module"
} as { bin?: {}; exports?: {} };

forEach(options?.exports, (entryFile, e) => {
const distName = entryAlias(e);

if (startsWith(e, "bin:")) {
set(pkg, ["bin", distName], `./${distName}.mjs`);
return;
}

set(pkg, ["exports", e], {
// bun must on first
bun: entryFile,
import: {
types: `${entryFile}`,
default: `./${distName}.mjs`
}
});
});

return pkg;
};
9 changes: 5 additions & 4 deletions nodedevpkg/purebundle/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
^target/
target

.swc
.turbo/
target/
dist/
*.mjs
*.d.ts
6 changes: 6 additions & 0 deletions nodepkg/lodash/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

.turbo/
target/
dist/
*.mjs
*.d.ts

0 comments on commit 29c0bec

Please sign in to comment.