Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): license-webpack-plugin should not scan root package.json #27989 #27994

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
38 changes: 36 additions & 2 deletions packages/angular/src/builders/utilities/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { merge } from 'webpack-merge';
import { registerTsProject } from '@nx/js/src/internal';
import { workspaceRoot } from '@nx/devkit';
import { join } from 'path';
import { existsSync, readFileSync } from 'fs';

export async function mergeCustomWebpackConfig(
baseWebpackConfig: any,
Expand All @@ -22,11 +23,44 @@ export async function mergeCustomWebpackConfig(

// The extra Webpack configuration file can export a synchronous or asynchronous function,
// for instance: `module.exports = async config => { ... }`.
let newConfig: any;
if (typeof config === 'function') {
return config(baseWebpackConfig, options, target);
newConfig = config(baseWebpackConfig, options, target);
} else {
return merge(baseWebpackConfig, config);
newConfig = merge(baseWebpackConfig, config);
}

// license-webpack-plugin will at times try to scan the monorepo's root package.json
// This will result in an error being thrown
// Ensure root package.json is excluded
const licensePlugin = newConfig.plugins.find(
(p) => p.constructor.name === 'LicenseWebpackPlugin'
);
if (licensePlugin) {
let rootPackageJsonName: string;
const pathToRootPackageJson = join(
newConfig.context.root ?? workspaceRoot,
'package.json'
);
if (existsSync(pathToRootPackageJson)) {
try {
const rootPackageJson = JSON.parse(
readFileSync(pathToRootPackageJson, 'utf-8')
);
rootPackageJsonName = rootPackageJson.name;
licensePlugin.pluginOptions.excludedPackageTest = (pkgName: string) => {
if (!rootPackageJsonName) {
Coly010 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
return pkgName === rootPackageJsonName;
};
} catch {
// do nothing
}
}
}

return newConfig;
}

export function resolveCustomWebpackConfig(path: string, tsConfig: string) {
Expand Down