-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathpackage-json.ts
More file actions
94 lines (86 loc) · 3.2 KB
/
package-json.ts
File metadata and controls
94 lines (86 loc) · 3.2 KB
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
import { type BuildConfig, ensureDir, type PackageJSON, recursiveChangePrefix } from './util';
import { readFile, writeFile } from './util';
import { join } from 'node:path';
/**
* The published build does not use the package.json found in the root directory. This function
* generates the package.json file for package to be published. Note that some of the properties can
* be pulled from the root package.json.
*/
export async function generatePackageJson(config: BuildConfig) {
const rootPkg = await readPackageJson(join(config.packagesDir, 'qwik'));
const distPkg: PackageJSON = {
name: rootPkg.name,
version: config.distVersion,
description: rootPkg.description,
license: rootPkg.license,
main: './core.mjs',
types: './core.d.ts',
bin: {
qwik: './qwik-cli.cjs',
},
type: 'module',
peerDependencies: {
undici: '*',
},
dependencies: rootPkg.dependencies,
exports: recursiveChangePrefix(rootPkg.exports!, './dist/', './'),
files: Array.from(new Set(rootPkg.files)).sort((a, b) => {
if (a.toLocaleLowerCase() < b.toLocaleLowerCase()) return -1;
if (a.toLocaleLowerCase() > b.toLocaleLowerCase()) return 1;
return 0;
}),
contributors: rootPkg.contributors,
homepage: rootPkg.homepage,
repository: rootPkg.repository,
bugs: rootPkg.bugs,
keywords: rootPkg.keywords,
engines: rootPkg.engines,
};
await writePackageJson(config.distQwikPkgDir, distPkg);
console.log(config.distQwikPkgDir);
await generateLegacyCjsSubmodule(config, 'core');
await generateLegacyCjsSubmodule(config, 'jsx-runtime');
await generateLegacyCjsSubmodule(config, 'jsx-dev-runtime', 'jsx-runtime');
await generateLegacyCjsSubmodule(config, 'optimizer');
await generateLegacyCjsSubmodule(config, 'server');
console.log(`🐷 generated package.json`);
}
export async function generateLegacyCjsSubmodule(
config: BuildConfig,
pkgName: string,
index = pkgName
) {
// Modern Node.js will resolve the submodule packages using "exports": https://nodejs.org/api/packages.html#subpath-exports
// however, legacy Node.js still needs a directory and its own package.json
// this can be removed once node12 is in the distant past
const pkg: PackageJSON = {
name: `@builder.io/qwik/${pkgName}`,
version: config.distVersion,
main: `../${index}.mjs`,
module: `../${index}.mjs`,
types: `../${index}.d.ts`,
type: 'module',
private: true,
exports: {
'.': {
types: `../${index}.d.ts`,
require: `../${index}.cjs`,
import: `../${index}.mjs`,
},
},
};
const submoduleDistDir = join(config.distQwikPkgDir, pkgName);
ensureDir(submoduleDistDir);
await writePackageJson(submoduleDistDir, pkg);
}
export async function readPackageJson(pkgJsonDir: string) {
const pkgJsonPath = join(pkgJsonDir, 'package.json');
const pkgJson: PackageJSON = JSON.parse(await readFile(pkgJsonPath, 'utf-8'));
return pkgJson;
}
export async function writePackageJson(pkgJsonDir: string, pkgJson: PackageJSON) {
ensureDir(pkgJsonDir);
const pkgJsonPath = join(pkgJsonDir, 'package.json');
const pkgJsonStr = JSON.stringify(pkgJson, null, 2) + '\n';
await writeFile(pkgJsonPath, pkgJsonStr);
}