Skip to content

Commit 2de4f44

Browse files
committed
fix build working directory issues
1 parent 1f74881 commit 2de4f44

5 files changed

Lines changed: 93 additions & 58 deletions

File tree

packages/rollup-util/src/BuildContext.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
import { join } from "node:path";
22
import { pathToFileURL } from "node:url";
33

4-
import { discoverModule, discoverWorkspace, getDependencies, Module } from "@calmdownval/workspaces-util";
4+
import { discoverModule, discoverWorkspace, getDependencies } from "@calmdownval/workspaces-util";
55

66
export interface BuildContext {
77
readonly cwd: string;
8+
readonly moduleName: string;
89
readonly targetEnv: TargetEnv;
10+
11+
/** @internal */
12+
addBuildTask(block: BuildTask): void;
913
}
1014

1115
export type TargetEnv =
1216
| "development"
1317
| "staging"
1418
| "production";
1519

20+
/** @internal */
21+
export interface BuildTask {
22+
(context: BuildContext): Promise<void>;
23+
}
24+
1625

1726
let currentContext: BuildContext | null = null;
1827

19-
export function getContext(): BuildContext {
28+
/** @internal */
29+
export function runBuild(block: BuildTask) {
2030
if (!currentContext) {
2131
throw new Error('Could not get build context. Please use the rollup-build command.');
2232
}
2333

24-
return currentContext;
34+
currentContext.addBuildTask(block);
2535
}
2636

2737
const ENV_MAP: { readonly [K in string]?: TargetEnv } = {
@@ -35,40 +45,53 @@ const ENV_MAP: { readonly [K in string]?: TargetEnv } = {
3545

3646
export async function build(cwd: string = process.cwd()) {
3747
try {
38-
// get the module to build
39-
const module = await discoverModule(cwd);
40-
if (!module) {
48+
// get the origin module to build
49+
const originModule = await discoverModule(cwd);
50+
if (!originModule) {
4151
throw new Error(`No module found at path '${cwd}'.`);
4252
}
4353

4454
// get an ordered queue of dependencies that need to be built
4555
const workspace = await discoverWorkspace({ cwd });
46-
const queue = workspace
56+
const moduleQueue = workspace
4757
? getDependencies({
4858
workspace,
49-
moduleName: module.declaration.name,
59+
moduleName: originModule.declaration.name,
5060
})
51-
: [ module ];
61+
: [ originModule ];
5262

53-
// prepare the context object
63+
// get the target environment
5464
const targetEnv: TargetEnv = process.env.BUILD_ENV
5565
? ENV_MAP[process.env.BUILD_ENV] ?? "production"
5666
: "production";
5767

58-
const context: BuildContext = {
59-
cwd,
60-
targetEnv,
61-
};
62-
6368
// build!
64-
for (const currentModule of queue) {
69+
for (const currentModule of moduleQueue) {
70+
const taskQueue: BuildTask[] = [];
6571
currentContext = {
66-
...context,
6772
cwd: currentModule.baseDir,
73+
moduleName: currentModule.declaration.name,
74+
targetEnv,
75+
addBuildTask: block => {
76+
taskQueue.push(block);
77+
},
78+
};
79+
80+
// import the build.targets.mjs definition file
81+
process.chdir(currentContext.cwd);
82+
try {
83+
const url = pathToFileURL(join(currentContext.cwd, "build.targets.mjs")).href;
84+
await import(url);
85+
}
86+
catch {
87+
// likely no such file exists, skip it...
88+
continue;
6889
}
6990

70-
const url = pathToFileURL(join(currentContext.cwd, "build.targets.mjs")).href;
71-
await import(url);
91+
// run queued tasks
92+
for (const task of taskQueue) {
93+
await task(currentContext);
94+
}
7295
}
7396
}
7497
finally {

packages/rollup-util/src/Plugin.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
import type { Plugin as RollupPlugin } from "rollup";
2+
13
import type { BuildContext } from "./BuildContext";
24

35
/**
46
* A Rollup plugin declaration. Manages its import logic and configuration.
57
*/
6-
export interface Plugin<TName extends string, TConfig, TInstance> {
8+
export interface Plugin<TName extends string, TConfig> {
79
readonly name: TName;
810

911
/** @internal */
10-
readonly factory: PluginFactoryBlock<TConfig, TInstance>;
12+
readonly factory: PluginFactoryBlock<TConfig>;
1113

1214
/** @internal */
1315
readonly configure: PluginConfigBlock<TConfig>;
1416

1517
override(
1618
configOverride: Partial<TConfig>,
17-
): Plugin<TName, TConfig, TInstance>;
19+
): Plugin<TName, TConfig>;
1820

1921
override(
2022
block: PluginConfigOverrideBlock<TConfig>,
21-
): Plugin<TName, TConfig, TInstance>;
23+
): Plugin<TName, TConfig>;
2224
}
2325

24-
export interface PluginFactoryBlock<TConfig, TInstance> {
25-
(config: TConfig, context: BuildContext): Promise<TInstance> | TInstance;
26+
export interface PluginFactoryBlock<TConfig> {
27+
(context: BuildContext): Promise<PluginFactory<TConfig>> | PluginFactory<TConfig>;
2628
}
2729

2830
export interface PluginConfigBlock<TConfig> {
@@ -33,41 +35,45 @@ export interface PluginConfigOverrideBlock<TConfig> {
3335
(config: TConfig, context: BuildContext): Promise<TConfig> | TConfig;
3436
}
3537

38+
export interface PluginFactory<TConfig> {
39+
(config: TConfig): RollupPlugin;
40+
}
41+
3642
export interface PluginFactoryBuilder<TName extends string> {
3743
/** @internal */
3844
readonly name: TName;
3945

40-
factory<TConfig, TInstance>(
41-
block: PluginFactoryBlock<TConfig, TInstance>,
42-
): PluginConfigurationBuilder<TName, TConfig, TInstance>;
46+
factory<TConfig>(
47+
block: PluginFactoryBlock<TConfig>,
48+
): PluginConfigurationBuilder<TName, TConfig>;
4349
}
4450

45-
export interface PluginConfigurationBuilder<TName extends string, TConfig, TInstance> {
51+
export interface PluginConfigurationBuilder<TName extends string, TConfig> {
4652
/** @internal */
4753
readonly name: TName;
4854

4955
/** @internal */
50-
readonly factory: PluginFactoryBlock<TConfig, TInstance>;
56+
readonly factory: PluginFactoryBlock<TConfig>;
5157

52-
configure<TConfig>(
58+
configure(
5359
config: TConfig,
54-
): Plugin<TName, TConfig, TInstance>;
60+
): Plugin<TName, TConfig>;
5561

56-
configure<TConfig>(
62+
configure(
5763
block: PluginConfigBlock<TConfig>
58-
): Plugin<TName, TConfig, TInstance>;
64+
): Plugin<TName, TConfig>;
5965
}
6066

6167
export type NameOf<TPlugin> = (
62-
TPlugin extends Plugin<infer TName, any, any> ? TName : unknown
68+
TPlugin extends Plugin<infer TName, any> ? TName : unknown
6369
);
6470

6571
export type ConfigOf<TPlugin> = (
66-
TPlugin extends Plugin<any, infer TConfig, any> ? TConfig : unknown
72+
TPlugin extends Plugin<any, infer TConfig> ? TConfig : unknown
6773
);
6874

6975
export type AnyPlugin = (
70-
Plugin<any, any, any>
76+
Plugin<any, any>
7177
);
7278

7379
export type PluginMap = {
@@ -83,8 +89,8 @@ export function declarePlugin<TName extends string>(name: TName): PluginFactoryB
8389

8490
function setPluginFactory(
8591
this: PluginFactoryBuilder<any>,
86-
block: PluginFactoryBlock<any, any>
87-
): PluginConfigurationBuilder<any, any, any> {
92+
block: PluginFactoryBlock<any>
93+
): PluginConfigurationBuilder<any, any> {
8894
return {
8995
name: this.name,
9096
factory: block,
@@ -93,7 +99,7 @@ function setPluginFactory(
9399
}
94100

95101
function configurePlugin(
96-
this: PluginConfigurationBuilder<any, any, any>,
102+
this: PluginConfigurationBuilder<any, any>,
97103
configOrBlock: any,
98104
): AnyPlugin {
99105
return {

packages/rollup-util/src/Target.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { resolve } from "node:path";
2+
13
import { rollup, type OutputOptions as TargetConfig } from "rollup";
24

35
import type { AnyPlugin, ConfigOf, NameOf, PluginConfigOverrideBlock, PluginMap } from "./Plugin";
4-
import { getContext, type BuildContext } from "./BuildContext";
6+
import { runBuild, type BuildContext } from "./BuildContext";
57

68
export interface Target<TName extends string, TPlugins extends PluginMap> {
79
readonly name: TName;
@@ -15,7 +17,7 @@ export interface Target<TName extends string, TPlugins extends PluginMap> {
1517
/** @internal */
1618
readonly configure: TargetConfigBlock;
1719

18-
build(): Promise<void>;
20+
build(): void;
1921

2022
entry(
2123
unit: string,
@@ -106,23 +108,23 @@ function configureTarget(
106108
};
107109
}
108110

109-
async function buildTarget(
111+
function buildTarget(
110112
this: Target<any, any>,
111113
) {
112-
const context = getContext();
113-
process.chdir(context.cwd);
114-
115-
const bundle = await rollup({
116-
input: this.entries,
117-
plugins: Promise.all(this.plugins.map(async it => {
118-
const pluginConfig = await it.configure(context);
119-
const instance = await it.factory(pluginConfig, context);
120-
return instance;
121-
}))
114+
runBuild(async context => {
115+
const inputConfig = {
116+
input: this.entries,
117+
plugins: Promise.all(this.plugins.map(async it => {
118+
const pluginConfig = await it.configure(context);
119+
const factory = await it.factory(context);
120+
return factory(pluginConfig);
121+
}))
122+
};
123+
124+
const bundle = await rollup(inputConfig);
125+
const outputConfig = await this.configure(context);
126+
await bundle.write(outputConfig);
122127
});
123-
124-
const outputConfig = await this.configure(context);
125-
bundle.write(outputConfig);
126128
}
127129

128130
function addTargetEntry(

packages/rollup-util/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
type PluginConfigBlock,
1212
type PluginConfigOverrideBlock,
1313
type PluginFactoryBuilder,
14+
type PluginFactory,
1415
type PluginConfigurationBuilder,
1516
type NameOf,
1617
type ConfigOf,

packages/workspaces-util/src/dependencies.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type DependencyMapKey =
3535
const DEFAULT_DEPENDENCY_MAPS: DependencyMapKey[] = [
3636
"dependencies",
3737
"devDependencies",
38+
"peerDependencies",
3839
];
3940

4041
function buildGraph(workspace: Workspace, options?: GraphOptions): Graph {
@@ -92,7 +93,7 @@ function createTraversal(
9293
direction: "push" | "unshift",
9394
): DependencyTraversal {
9495
return options => {
95-
const graph = buildGraph(options.workspace);
96+
const graph = buildGraph(options.workspace, options);
9697
const origin = graph[options.moduleName];
9798
if (!origin) {
9899
throw new Error(`No module '${options.moduleName}' could be found in the workspace.`);
@@ -131,9 +132,11 @@ function createTraversal(
131132
node.visiting = false;
132133
node.visited = true;
133134

134-
if (node !== origin && options.includeSelf !== false) {
135+
if (node !== origin || options.includeSelf !== false) {
135136
result[direction](node.module);
136137
}
138+
139+
return true;
137140
};
138141

139142
visit(origin);

0 commit comments

Comments
 (0)