Skip to content

Commit

Permalink
feat(js): add skipPackageManager option to build executors in order t…
Browse files Browse the repository at this point in the history
…o skip generating "packageManager" entry in package.json (#27518)

This PR adds `skipPackageManager` option to several build executors in
order to disable generating the `packageManager` field in the resulting
`package.json` file. This field may be problematic on different
platforms so we want a way to work around it.

Affected executors:
- `@nx/webpack:webpack`
- `@nx/vite:build`
- `@nx/next:build`
- `@nx/remix:build`




<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #27027
  • Loading branch information
jaysoo authored Aug 20, 2024
1 parent 6d963fd commit 83237a8
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 14 deletions.
4 changes: 4 additions & 0 deletions docs/generated/packages/next/executors/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
"default": false,
"x-priority": "internal"
},
"skipPackageManager": {
"type": "boolean",
"description": "Do not add a `packageManager` entry to the generated package.json file."
},
"debug": {
"type": "boolean",
"description": "Enable Next.js debug build logging"
Expand Down
4 changes: 4 additions & 0 deletions docs/generated/packages/remix/executors/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"description": "Generate a lockfile (e.g. package-lock.json) that matches the workspace lockfile to ensure package versions match.",
"default": false
},
"skipPackageManager": {
"type": "boolean",
"description": "Do not add a `packageManager` entry to the generated package.json file. Only works in conjunction with `generatePackageJson` option."
},
"sourcemap": {
"type": "boolean",
"description": "Generate source maps for production.",
Expand Down
4 changes: 4 additions & 0 deletions docs/generated/packages/vite/executors/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"includeDevDependenciesInPackageJson": {
"description": "Include devDependencies in the generated package.json.",
"type": "boolean"
},
"skipPackageManager": {
"type": "boolean",
"description": "Do not add a `packageManager` entry to the generated package.json file. Only works in conjunction with `generatePackageJson` option."
}
},
"definitions": {},
Expand Down
4 changes: 4 additions & 0 deletions docs/generated/packages/webpack/executors/webpack.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@
"type": "boolean",
"description": "Generates a `package.json` and pruned lock file with the project's `node_module` dependencies populated for installing in a container. If a `package.json` exists in the project's directory, it will be reused with dependencies populated."
},
"skipPackageManager": {
"type": "boolean",
"description": "Do not add a `packageManager` entry to the generated package.json file. Only works in conjunction with `generatePackageJson` option."
},
"transformers": {
"type": "array",
"description": "List of TypeScript Compiler Transfomers Plugins.",
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default async function buildExecutor(
target: context.targetName,
root: context.root,
isProduction: !options.includeDevDependenciesInPackageJson, // By default we remove devDependencies since this is a production build.
skipPackageManager: options.skipPackageManager,
}
);

Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"default": false,
"x-priority": "internal"
},
"skipPackageManager": {
"type": "boolean",
"description": "Do not add a `packageManager` entry to the generated package.json file."
},
"debug": {
"type": "boolean",
"description": "Enable Next.js debug build logging"
Expand Down
15 changes: 8 additions & 7 deletions packages/next/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ export interface FileReplacement {
}

export interface NextBuildBuilderOptions {
outputPath: string;
fileReplacements: FileReplacement[];
assets?: any[];
nextConfig?: string;
buildLibsFromSource?: boolean;
includeDevDependenciesInPackageJson?: boolean;
generateLockfile?: boolean;
watch?: boolean;
debug?: boolean;
profile?: boolean;
experimentalAppOnly?: boolean;
experimentalBuildMode?: 'compile' | 'generate';
fileReplacements: FileReplacement[];
generateLockfile?: boolean;
includeDevDependenciesInPackageJson?: boolean;
nextConfig?: string;
outputPath: string;
profile?: boolean;
skipPackageManager?: boolean;
watch?: boolean;
}

export interface NextServeBuilderOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,39 @@ describe('createPackageJson', () => {
});
});

it('should support skipping packageManager entry', () => {
spies.push(
jest
.spyOn(fs, 'existsSync')
.mockImplementation(
(path) =>
path === 'libs/lib1/package.json' || path === 'package.json'
)
);
spies.push(
jest
.spyOn(fileutilsModule, 'readJsonFile')
.mockImplementation((path) => {
if (path === 'package.json') {
return {
...rootPackageJson(),
packageManager: 'yarn',
};
}
if (path === 'libs/lib1/package.json') {
return projectPackageJson();
}
})
);

expect(
createPackageJson('lib1', graph, {
root: '',
skipPackageManager: true,
}).packageManager
).toBeUndefined();
});

it('should replace packageManager if not in sync with root and show warning', () => {
spies.push(
jest.spyOn(fs, 'existsSync').mockImplementation((path) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function createPackageJson(
root?: string;
isProduction?: boolean;
helperDependencies?: string[];
skipPackageManager?: boolean;
} = {},
fileMap: ProjectFileMap = null
): PackageJson {
Expand Down Expand Up @@ -181,7 +182,7 @@ export function createPackageJson(
packageJson.peerDependenciesMeta
);

if (rootPackageJson.packageManager) {
if (rootPackageJson.packageManager && !options.skipPackageManager) {
if (
packageJson.packageManager &&
packageJson.packageManager !== rootPackageJson.packageManager
Expand Down
1 change: 1 addition & 0 deletions packages/remix/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default async function buildExecutor(
target: context.targetName,
root: context.root,
isProduction: !options.includeDevDependenciesInPackageJson, // By default we remove devDependencies since this is a production build.
skipPackageManager: options.skipPackageManager,
});

// Update `package.json` to reflect how users should run the build artifacts
Expand Down
7 changes: 4 additions & 3 deletions packages/remix/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export interface RemixBuildSchema {
outputPath: string;
includeDevDependenciesInPackageJson?: boolean;
generatePackageJson?: boolean;
generateLockfile?: boolean;
generatePackageJson?: boolean;
includeDevDependenciesInPackageJson?: boolean;
outputPath: string;
skipPackageManager?: boolean;
sourcemap?: boolean;
}
4 changes: 4 additions & 0 deletions packages/remix/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"description": "Generate a lockfile (e.g. package-lock.json) that matches the workspace lockfile to ensure package versions match.",
"default": false
},
"skipPackageManager": {
"type": "boolean",
"description": "Do not add a `packageManager` entry to the generated package.json file. Only works in conjunction with `generatePackageJson` option."
},
"sourcemap": {
"type": "boolean",
"description": "Generate source maps for production.",
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export async function* viteBuildExecutor(
target: context.targetName,
root: context.root,
isProduction: !options.includeDevDependenciesInPackageJson, // By default we remove devDependencies since this is a production build.
skipPackageManager: options.skipPackageManager,
}
);

Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export interface ViteBuildExecutorOptions {
outputPath?: string;
buildLibsFromSource?: boolean;
skipTypeCheck?: boolean;
configFile?: string;
watch?: boolean;
generatePackageJson?: boolean;
includeDevDependenciesInPackageJson?: boolean;
outputPath?: string;
skipPackageManager?: boolean;
skipTypeCheck?: boolean;
tsConfig?: string;
watch?: boolean;
}
4 changes: 4 additions & 0 deletions packages/vite/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
"includeDevDependenciesInPackageJson": {
"description": "Include devDependencies in the generated package.json.",
"type": "boolean"
},
"skipPackageManager": {
"type": "boolean",
"description": "Do not add a `packageManager` entry to the generated package.json file. Only works in conjunction with `generatePackageJson` option."
}
},
"definitions": {},
Expand Down
4 changes: 4 additions & 0 deletions packages/webpack/src/executors/webpack/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@
"type": "boolean",
"description": "Generates a `package.json` and pruned lock file with the project's `node_module` dependencies populated for installing in a container. If a `package.json` exists in the project's directory, it will be reused with dependencies populated."
},
"skipPackageManager": {
"type": "boolean",
"description": "Do not add a `packageManager` entry to the generated package.json file. Only works in conjunction with `generatePackageJson` option."
},
"transformers": {
"type": "array",
"description": "List of TypeScript Compiler Transfomers Plugins.",
Expand Down
2 changes: 2 additions & 0 deletions packages/webpack/src/plugins/generate-package-json-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const pluginName = 'GeneratePackageJsonPlugin';
export class GeneratePackageJsonPlugin implements WebpackPluginInstance {
constructor(
private readonly options: {
skipPackageManager?: boolean;
tsConfig: string;
outputFileName: string;
root: string;
Expand Down Expand Up @@ -65,6 +66,7 @@ export class GeneratePackageJsonPlugin implements WebpackPluginInstance {
root: this.options.root,
isProduction: true,
helperDependencies: helperDependencies.map((dep) => dep.target),
skipPackageManager: this.options.skipPackageManager,
}
);
packageJson.main = packageJson.main ?? this.options.outputFileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ export interface NxAppWebpackPluginOptions {
* External scripts that will be included before the main application entry.
*/
scripts?: Array<ExtraEntryPointClass | string>;
/**
* Do not add a `packageManager` entry to the generated package.json file. Only works in conjunction with `generatePackageJson` option.
*/
skipPackageManager?: boolean;
/**
* Skip type checking. Default is `false`.
*/
Expand Down

0 comments on commit 83237a8

Please sign in to comment.