Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 99;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "pack-workspace-root",
"private": true,
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 11;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"a","type":"module"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 55;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"b","type":"module"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default { entry: { index: 'member.ts' } };
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[case]]
name = "command_pack_workspace"
vp = "local"
steps = [
{ argv = ["vp", "pack"], comment = "use the workspace and exclusions from vite.config.ts", snapshot = false },
["node", "verify.mjs", "a"],
{ argv = ["vp", "pack", "--workspace", "packages/*"], comment = "build both members with their own entries", snapshot = false },
["node", "verify.mjs", "a", "b"],
{ argv = ["vp", "pack", "--workspace", "packages/*", "--filter", "b"], comment = "filter the expanded members by package name", snapshot = false },
["node", "verify.mjs", "b"],
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# command_pack_workspace

## `vp pack`

use the workspace and exclusions from vite.config.ts


## `node verify.mjs a`

```
```

## `vp pack --workspace packages/*`

build both members with their own entries


## `node verify.mjs a b`

```
```

## `vp pack --workspace packages/* --filter b`

filter the expanded members by package name


## `node verify.mjs b`

```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assert from 'node:assert/strict';
import { existsSync, rmSync } from 'node:fs';

assert.equal(existsSync('dist/index.mjs'), false, 'unexpected root build');
const expected = process.argv.slice(2);
for (const [name, value] of Object.entries({ a: 11, b: 55 })) {
const file = `packages/${name}/dist/index.mjs`;
assert.equal(existsSync(file), expected.includes(name), file);
if (expected.includes(name)) {
assert.equal((await import(`./${file}`)).value, value);
rmSync(`packages/${name}/dist`, { recursive: true });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
pack: {
entry: ['index.ts'],
workspace: { include: ['packages/*'], exclude: ['**/b'] },
},
};
45 changes: 37 additions & 8 deletions packages/cli/src/pack-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import path from 'node:path';
import { cac } from 'cac';
import {
buildWithConfigs,
expandWorkspace,
resolveUserConfig,
globalLogger,
enableDebug,
type InlineConfig,
type ResolvedConfig,
type TsdownHandle,
type UserConfig,
} from 'vite/pack';

import { resolveViteConfig } from './resolve-vite-config.ts';
Expand Down Expand Up @@ -157,23 +159,50 @@ cli
configDeps.add(path.normalize(viteConfig.configFile));
}

let handlePromise: Promise<TsdownHandle> | undefined;
async function getWatch() {
if (!handlePromise) {
throw new Error('Cannot control watch before pack config is resolved.');
}
return (await handlePromise).watch;
}
const watch: TsdownHandle['watch'] = {
restart: async () => (await getWatch()).restart(),
close: async () => (await getWatch()).close(),
};
const configs: ResolvedConfig[] = [];
const packConfigs = Array.isArray(viteConfig?.pack)
? viteConfig.pack
: [viteConfig?.pack ?? {}];
let hasWorkspace = false;
for (const packConfig of packConfigs) {
const merged = { ...packConfig, ...flags };
// Keep postcss/lightningcss external to the dts bundle (see plugin doc)
if (merged.dts) {
const existingPlugins = Array.isArray(merged.plugins) ? merged.plugins : [];
merged.plugins = [...existingPlugins, externalDtsTypeOnlyPlugin()];
let buildConfigs: UserConfig[] = [merged];
if (merged.workspace) {
hasWorkspace = true;
const workspace = await expandWorkspace(packConfig, flags, watch, configDeps);
workspace.deps.forEach((dep) => configDeps.add(dep));
buildConfigs = workspace.configs.filter((config) => !config.workspace || config.entry);
}
for (const config of buildConfigs) {
// Keep postcss/lightningcss external to the dts bundle (see plugin doc)
if (config.dts) {
const existingPlugins = Array.isArray(config.plugins) ? config.plugins : [];
config.plugins = [...existingPlugins, externalDtsTypeOnlyPlugin()];
}
const resolvedConfig = await resolveUserConfig(config, flags, configDeps);
resolvedConfig.forEach((config) =>
config.configDeps.forEach((dep) => configDeps.add(dep)),
);
configs.push(...resolvedConfig);
}
const resolvedConfig = await resolveUserConfig(merged, flags, configDeps);
resolvedConfig.forEach((config) => config.configDeps.forEach((dep) => configDeps.add(dep)));
configs.push(...resolvedConfig);
}

return buildWithConfigs(configs, configDeps, runBuild);
if (hasWorkspace && configs.length === 0) {
throw new Error('No valid configuration found.');
}
handlePromise = buildWithConfigs(configs, configDeps, runBuild);
return handlePromise;
}

await runBuild();
Expand Down
26 changes: 26 additions & 0 deletions patches/tsdown@0.23.4.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/dist/build-wv1dWHDH.mjs b/dist/build-wv1dWHDH.mjs
index 82fc360..87edaa7 100644
--- a/dist/build-wv1dWHDH.mjs
+++ b/dist/build-wv1dWHDH.mjs
@@ -892,2 +892,5 @@ async function buildSingle(config, configDeps, isDualFormat, clean, restart, don
}
+export async function expandWorkspace(config, flags, watch, deps) {
+ return resolveWorkspace(config, flags, { ci: isInCI(), watch }, deps);
+}
export { buildWithConfigs as n, build_exports as r, build$1 as t };
diff --git a/dist/index.d.mts b/dist/index.d.mts
index 085b184..d2acbd4 100644
--- a/dist/index.d.mts
+++ b/dist/index.d.mts
@@ -3,2 +3,4 @@ import { n as mergeConfig, r as resolveUserConfig, t as defineConfig } from "./c
import * as Rolldown from "rolldown";
+/** @internal Expand a preloaded root configuration before resolving individual builds. */
+export declare function expandWorkspace(config: UserConfig, flags: InlineConfig, watch: TsdownHandle["watch"], deps: Set<string>): Promise<{ configs: UserConfig[]; deps: Set<string> }>;
/**
diff --git a/dist/index.mjs b/dist/index.mjs
index 3d16d6c..d8e6e04 100644
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -1 +1,2 @@
+export { expandWorkspace } from "./build-wv1dWHDH.mjs";
import { n as buildWithConfigs, t as build } from "./build-wv1dWHDH.mjs";
13 changes: 7 additions & 6 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ packageExtensions:
dependencies:
typescript: ~6.0.3
patchedDependencies:
tsdown@0.23.4: patches/tsdown@0.23.4.patch
chokidar@3.6.0: vite/patches/chokidar@3.6.0.patch
dotenv-expand@13.0.0: vite/patches/dotenv-expand@13.0.0.patch
sirv@3.0.2: vite/patches/sirv@3.0.2.patch
Expand Down
Loading