Skip to content

Commit 7097ddc

Browse files
authored
feat(pkg), add a new API "modifyPackageJson" to the env (#9530)
Until now, it was only possible to provide static props. This PR provides the ability to modify the props per component dynamically before writing it to the filesystem.
1 parent c725a6c commit 7097ddc

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

scopes/envs/envs/environment.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Bundler, BundlerContext, DevServer, DevServerContext } from '@team
77
import type { BuildTask } from '@teambit/builder';
88
import type { SchemaExtractor } from '@teambit/schema';
99
import type { WebpackConfigTransformer } from '@teambit/webpack';
10-
import type { PackageJsonProps } from '@teambit/pkg';
10+
import type { ModifyPackageJsonFunc, PackageJsonProps } from '@teambit/pkg';
1111
import type { DependencyDetector, EnvPolicyConfigObject } from '@teambit/dependency-resolver';
1212
import type { Capsule } from '@teambit/isolator';
1313
import type { Component } from '@teambit/component';
@@ -119,6 +119,12 @@ export interface PackageEnv extends Environment {
119119
* return `.npmignore` entries to be written before packing the component
120120
*/
121121
getNpmIgnore?: (npmIgnoreContext?: GetNpmIgnoreContext) => string[];
122+
123+
/**
124+
* in case the static "getPackageJsonProps" is not enough, and the package.json props need to be modified
125+
* per component, use this function.
126+
*/
127+
modifyPackageJson?: ModifyPackageJsonFunc;
122128
}
123129

124130
export interface LinterEnv extends Environment {

scopes/pkg/pkg/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export type {
77
} from './pkg.main.runtime';
88
export type { PackageDependency, PackageDependencyFactory } from './package-dependency';
99
export type { PackageEnv } from './package-env-type';
10-
export { PackageGenerator } from './package-generator';
10+
export { PackageGenerator, ModifyPackageJsonFunc } from './package-generator';
1111
export { PkgAspect as default, PkgAspect } from './pkg.aspect';
1212
export { PkgUI } from './pkg.ui.runtime';

scopes/pkg/pkg/package-generator.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { EnvContext, EnvHandler } from '@teambit/envs';
2+
import { Component } from '@teambit/component';
23
import { PackageJsonProps } from './pkg.main.runtime';
34

5+
export type ModifyPackageJsonFunc = (
6+
component: Component, packageJsonObject: PackageJsonProps) => Promise<PackageJsonProps>;
7+
48
export type PackageGeneratorOptions = {
59
packageJson: PackageJsonProps;
610
npmIgnore?: string[];
11+
modifyPackageJson?: ModifyPackageJsonFunc;
712
};
813

914
/**
@@ -12,9 +17,10 @@ export type PackageGeneratorOptions = {
1217
*/
1318
export class PackageGenerator {
1419
constructor(
20+
private context: EnvContext,
1521
private _packageJson: PackageJsonProps = {},
1622
private _npmIgnore: string[] = [],
17-
private context: EnvContext
23+
private _modifyPackageJson?: ModifyPackageJsonFunc,
1824
) {}
1925

2026
get packageJsonProps() {
@@ -25,9 +31,13 @@ export class PackageGenerator {
2531
return this._npmIgnore;
2632
}
2733

34+
get modifyPackageJson(): ModifyPackageJsonFunc | undefined {
35+
return this._modifyPackageJson;
36+
}
37+
2838
static from(options: PackageGeneratorOptions): EnvHandler<PackageGenerator> {
2939
return (context: EnvContext) => {
30-
return new PackageGenerator(options.packageJson, options.npmIgnore, context);
40+
return new PackageGenerator(context, options.packageJson, options.npmIgnore, options.modifyPackageJson);
3141
};
3242
}
3343
}

scopes/pkg/pkg/pkg.main.runtime.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,12 @@ export class PkgMain {
491491
packageJsonObject: Record<string, any>
492492
): Promise<Record<string, any>> {
493493
const newProps = this.getPackageJsonModifications(component);
494-
return Object.assign(packageJsonObject, newProps);
494+
const pkgJsonObj = Object.assign(packageJsonObject, newProps);
495+
const env = this.envs.getEnv(component).env;
496+
if (env.modifyPackageJson) {
497+
return env.modifyPackageJson(component, pkgJsonObj);
498+
}
499+
return pkgJsonObj;
495500
}
496501
}
497502

scopes/pkg/pkg/pkg.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class PkgService implements EnvService<{}, PkgDescriptor> {
4545
getPackageJsonProps: () => packageGenerator.packageJsonProps,
4646
// TODO: somehow handle context here? used in the aspect env
4747
getNpmIgnore: () => packageGenerator.npmIgnore,
48+
modifyPackageJson: packageGenerator.modifyPackageJson,
4849
};
4950
}
5051

scopes/scope/export/export.main.runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ if the export fails with missing objects/versions/components, run "bit fetch --l
220220
);
221221
const updatedIds = _updateIdsOnBitMap(consumer.bitMap, updatedLocally);
222222
// re-generate the package.json, this way, it has the correct data in the componentId prop.
223-
await linkToNodeModulesByIds(this.workspace, updatedIds, true);
223+
await linkToNodeModulesByIds(this.workspace, updatedIds);
224224
await this.removeFromStagedConfig(exported);
225225
// ideally we should delete the staged-snaps only for the exported snaps. however, it's not easy, and it's ok to
226226
// delete them all because this file is mainly an optimization for the import process.

0 commit comments

Comments
 (0)