/
update-package-json.ts
148 lines (130 loc) · 4.03 KB
/
update-package-json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
ExecutorContext,
normalizePath,
ProjectGraphProjectNode,
readJsonFile,
writeJsonFile,
} from '@nrwl/devkit';
import {
DependentBuildableProjectNode,
updateBuildableProjectPackageJsonDependencies,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { basename, dirname, join, parse, relative } from 'path';
import { fileExists } from 'nx/src/utils/fileutils';
import type { PackageJson } from 'nx/src/utils/package-json';
function getMainFileDirRelativeToProjectRoot(
main: string,
projectRoot: string
): string {
const mainFileDir = dirname(main);
const relativeDir = normalizePath(relative(projectRoot, mainFileDir));
return relativeDir === '' ? `./` : `./${relativeDir}/`;
}
export type SupportedFormat = 'cjs' | 'esm';
export interface UpdatePackageJsonOption {
projectRoot: string;
main: string;
format?: SupportedFormat[];
outputPath: string;
outputFileName?: string;
outputFileExtensionForCjs?: `.${string}`;
skipTypings?: boolean;
generateExportsField?: boolean;
updateBuildableProjectDepsInPackageJson?: boolean;
buildableProjectDepsInPackageJsonType?: 'dependencies' | 'peerDependencies';
}
export function updatePackageJson(
options: UpdatePackageJsonOption,
context: ExecutorContext,
target: ProjectGraphProjectNode,
dependencies: DependentBuildableProjectNode[]
): void {
const pathToPackageJson = join(
context.root,
options.projectRoot,
'package.json'
);
const packageJson = fileExists(pathToPackageJson)
? readJsonFile(pathToPackageJson)
: { name: context.projectName };
writeJsonFile(
`${options.outputPath}/package.json`,
getUpdatedPackageJsonContent(packageJson, options)
);
if (
dependencies.length > 0 &&
options.updateBuildableProjectDepsInPackageJson
) {
updateBuildableProjectPackageJsonDependencies(
context.root,
context.projectName,
context.targetName,
context.configurationName,
target,
dependencies,
options.buildableProjectDepsInPackageJsonType
);
}
}
export function getUpdatedPackageJsonContent(
packageJson: PackageJson,
options: UpdatePackageJsonOption
) {
// Default is CJS unless esm is explicitly passed.
const hasCjsFormat = !options.format || options.format?.includes('cjs');
const hasEsmFormat = options.format?.includes('esm');
const mainFile = basename(options.main).replace(/\.[tj]s$/, '');
const relativeMainFileDir = getMainFileDirRelativeToProjectRoot(
options.main,
options.projectRoot
);
const typingsFile = `${relativeMainFileDir}${mainFile}.d.ts`;
const exports =
typeof packageJson.exports === 'string'
? packageJson.exports
: {
'.': {},
...packageJson.exports,
};
const mainJsFile =
options.outputFileName ?? `${relativeMainFileDir}${mainFile}.js`;
if (hasEsmFormat) {
// Unofficial field for backwards compat.
packageJson.module ??= mainJsFile;
if (!hasCjsFormat) {
packageJson.type = 'module';
packageJson.main ??= mainJsFile;
}
if (typeof exports !== 'string') {
if (typeof exports['.'] !== 'string') {
exports['.']['import'] ??= mainJsFile;
} else if (!hasCjsFormat) {
exports['.'] ??= mainJsFile;
}
}
}
// CJS output may have .cjs or .js file extensions.
// Bundlers like rollup and esbuild supports .cjs for CJS and .js for ESM.
// Bundlers/Compilers like webpack, tsc, swc do not have different file extensions.
if (hasCjsFormat) {
const { dir, name } = parse(mainJsFile);
const cjsMain = `${dir}/${name}${
options.outputFileExtensionForCjs ?? '.js'
}`;
packageJson.main ??= cjsMain;
if (typeof exports !== 'string') {
if (typeof exports['.'] !== 'string') {
exports['.']['require'] ??= cjsMain;
} else if (!hasEsmFormat) {
exports['.'] ??= cjsMain;
}
}
}
if (options.generateExportsField) {
packageJson.exports = exports;
}
if (!options.skipTypings) {
packageJson.types = packageJson.types ?? typingsFile;
}
return packageJson;
}