Skip to content

Commit

Permalink
revert: fix monorepo package deps in root config loader
Browse files Browse the repository at this point in the history
This reverts commit ae9f7f5.
  • Loading branch information
stevenle committed Nov 2, 2024
1 parent ae9f7f5 commit 2cdb8e9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
5 changes: 0 additions & 5 deletions .changeset/two-games-hear.md

This file was deleted.

2 changes: 1 addition & 1 deletion packages/create-root/src/root-version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is autogenerated by update-root-version.mjs.
export const ROOT_VERSION = '1.3.12';
export const ROOT_VERSION = '1.3.2';
1 change: 0 additions & 1 deletion packages/root/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"cookie-parser": "1.4.6",
"dotenv": "16.4.5",
"esbuild": "0.19.9",
"esbuild-node-externals": "^1.15.0",
"express": "4.18.2",
"fs-extra": "11.1.1",
"html-minifier-terser": "7.2.0",
Expand Down
47 changes: 40 additions & 7 deletions packages/root/src/node/load-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'node:path';
import {bundleRequire} from 'bundle-require';
import {build} from 'esbuild';
import {nodeExternalsPlugin} from 'esbuild-node-externals';
import {RootConfig} from '../core/config.js';
import {fileExists, loadJson} from '../utils/fsutils.js';

Expand All @@ -20,10 +19,6 @@ export async function loadRootConfig(
}
const configBundle = await bundleRequire({
filepath: configPath,
// Externalize all dependencies.
esbuildOptions: {
plugins: [nodeExternalsPlugin()],
},
});
let config = configBundle.mod.default || {};
if (typeof config === 'function') {
Expand All @@ -42,6 +37,26 @@ export async function bundleRootConfig(rootDir: string, outPath: string) {
throw new Error(`${configPath} does not exist`);
}

const packageJsonPath = path.resolve(rootDir, 'package.json');
const packageJson = await loadPackageJson(packageJsonPath);
const allDeps = {
...packageJson.peerDependencies,
...packageJson.dependencies,
};

function getPackageName(id: string): string {
const segments = id.split('/');
if (segments.length > 1) {
// Check if package is an org path like `@blinkk/root`.
if (segments[0].startsWith('@') && segments[0].length > 1) {
return `${segments[0]}/${segments[1]}`;
}
// For imports like `my-package/subpackage`, return `my-package`.
return segments[0];
}
return id;
}

await build({
entryPoints: [configPath],
bundle: true,
Expand All @@ -51,8 +66,26 @@ export async function bundleRootConfig(rootDir: string, outPath: string) {
sourcemap: 'inline',
metafile: true,
format: 'esm',
// Externalize all dependencies.
plugins: [nodeExternalsPlugin()],
plugins: [
// Externalizes deps that are in package.json.
{
name: 'externalize-package-json-deps',
setup(build) {
build.onResolve({filter: /.*/}, (args) => {
const id = args.path;
if (id[0] !== '.' && !id.startsWith('@/')) {
const packageName = getPackageName(id);
if (packageName in allDeps) {
return {
external: true,
};
}
}
return null;
});
},
},
],
});
}

Expand Down
26 changes: 6 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2cdb8e9

Please sign in to comment.