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

Using generatePackageJson to generate dependencies is inconsistent with withNx() externals #423

Open
jiayisheji opened this issue Sep 25, 2024 · 0 comments

Comments

@jiayisheji
Copy link

jiayisheji commented Sep 25, 2024

  1. generatePackageJson
{
  "name": "api",
  "version": "0.0.1",
  "dependencies": {
    "@alicloud/pop-core": "1.7.13",
    "@nestjs/axios": "3.0.2",
    "@nestjs/common": "10.2.10",
    "@nestjs/config": "3.2.3",
    "@nestjs/core": "10.2.10",
    "@nestjs/jwt": "10.2.0",
    "@nestjs/passport": "10.0.3",
    "@nestjs/platform-express": "10.2.10",
    "@nestjs/schedule": "4.1.0",
    "@nestjs/swagger": "7.4.0",
    "@nestjs/throttler": "6.1.0",
    "@nestjs/typeorm": "10.0.2",
    "@peculiar/x509": "1.11.0",
    "acme-client": "5.4.0",
    "archiver": "7.0.1",
    "axios": "1.7.7",
    "class-transformer": "0.5.1",
    "class-validator": "0.14.1",
    "cloudflare": "3.5.0",
    "cron": "3.1.7",
    "date-fns": "3.6.0",
    "date-fns-tz": "3.1.3",
    "express": "4.19.2",
    "fast-csv": "5.0.1",
    "fs-extra": "11.2.0",
    "iconv-lite": "0.6.3",
    "lodash": "4.17.21",
    "node-telegram-bot-api": "0.66.0",
    "passport": "0.7.0",
    "passport-jwt": "4.0.1",
    "passport-local": "1.0.0",
    "reflect-metadata": "0.1.14",
    "rxjs": "7.8.1",
    "socks-proxy-agent": "8.0.4",
    "tencentcloud-sdk-nodejs": "4.0.898",
    "tslib": "2.6.3",
    "typeorm": "0.3.20",
    "whoiser": "1.18.0"
  },
  "main": "main.js"
}
  1. withNx() externals
{
  '@nx/webpack': '@nx/webpack',
  '@nx/rspack': '@nx/rspack',
  '@nx/js': '@nx/js',
  '@nestjs/common': '@nestjs/common',
  '@nestjs/core': '@nestjs/core',
  '@nestjs/config': '@nestjs/config',
  '@nestjs/schedule': '@nestjs/schedule',
  '@nestjs/throttler': '@nestjs/throttler',
  '@nestjs/swagger': '@nestjs/swagger',
  'class-validator': 'class-validator',
  typeorm: 'typeorm',
  '@nestjs/passport': '@nestjs/passport',
  express: 'express',
  '@nestjs/typeorm': '@nestjs/typeorm',
  '@nestjs/jwt': '@nestjs/jwt',
  'class-transformer': 'class-transformer',
  'passport-jwt': 'passport-jwt',
  'passport-local': 'passport-local',
  archiver: 'archiver',
  'fs-extra': 'fs-extra',
  cron: 'cron',
  '@nestjs/platform-express': '@nestjs/platform-express'
}

The only inconsistencies I noticed were in the node_modules package I imported in the packages directory.

As a result, all these packages are written to main.js, resulting in a very large file.

I used createPackageJson() to fix it:

const { composePlugins, withNx } = require('@nx/rspack');
const { createPackageJson, getHelperDependenciesFromProjectGraph } = require('@nx/js');

module.exports = composePlugins(withNx(), (config, { options, context }) => {
	if (options.optimization === false) {
		config.optimization = {
			minimize: false,
		};
	}
	const helperDependencies = getHelperDependenciesFromProjectGraph(
		context.root,
		context.projectName,
		context.projectGraph
	);

	const { dependencies } = createPackageJson(
		context.projectName,
		context.projectGraph,
		{
			target: context.targetName,
			root: context.root,
			isProduction: true,
			helperDependencies: helperDependencies.map((dep) => dep.target),
		}
	);

	for (const packageName of Object.keys(dependencies)) {
		if (!config.externals[packageName]) {
			config.externals[packageName] = packageName;
		}
	}
	return config;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant