-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathesbuild.config.js
124 lines (109 loc) · 3.99 KB
/
esbuild.config.js
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
const { readFile, writeFile, stat } = require('node:fs/promises');
const { exec } = require('node:child_process');
const { promisify } = require('node:util');
const { createProjectGraphAsync } = require('@nx/devkit');
const { NX_TASK_TARGET_PROJECT, NX_TASK_TARGET_TARGET } = process.env;
if (!NX_TASK_TARGET_PROJECT) {
throw new Error('Missing NX_TASK_TARGET_PROJECT environment variable');
}
if (!NX_TASK_TARGET_TARGET) {
throw new Error('Missing NX_TASK_TARGET_TARGET environment variable');
}
const getNxProject = async () => {
const graph = await createProjectGraphAsync();
return graph.nodes[NX_TASK_TARGET_PROJECT];
};
/**
* @param {import('@nx/devkit').ProjectGraphProjectNode} project
* @returns {import('@nx/esbuild/src/executors/esbuild/schema').EsBuildExecutorOptions}
*/
const getESBuildExecutorOptions = project => {
const target = project.data.targets[NX_TASK_TARGET_TARGET];
if (target.executor !== '@nx/esbuild:esbuild') {
throw new Error(
`Unexpected ${target.executor} executor for ${NX_TASK_TARGET_TARGET} target, expected @nx/esbuild:esbuild`,
);
}
return target.options;
};
/** @type {import('esbuild').BuildOptions} */
module.exports = {
plugins: [
{
name: 'TypeScriptDeclarations',
setup(build) {
build.onEnd(async result => {
if (result.errors.length > 0) return;
const project = await getNxProject();
const { tsConfig } = getESBuildExecutorOptions(project);
try {
await promisify(exec)(
`tsc --emitDeclarationOnly --project ${tsConfig} --outDir dist`,
);
} catch (err) {
console.error(err);
throw err;
}
});
},
},
{
name: 'PackageJSON',
setup(build) {
build.onEnd(async result => {
if (result.errors.length > 0) return;
const project = await getNxProject();
const { outputPath } = getESBuildExecutorOptions(project);
const sourcePackageJsonPath = `${project.data.root}/package.json`;
const outputPackageJsonPath = `${outputPath}/package.json`;
const isPublishable = await stat(sourcePackageJsonPath)
.then(stats => stats.isFile())
.catch(() => false);
if (!isPublishable) {
/** @type {import('nx/src/utils/package-json').PackageJson} */
const newPackageJson = {
name: `@code-pushup/${project.name}`,
private: true,
type: 'module',
main: 'index.js',
types: 'src/index.d.ts',
};
await writeFile(
outputPackageJsonPath,
JSON.stringify(newPackageJson, null, 2),
);
return;
}
/** @type {import('nx/src/utils/package-json').PackageJson} */
const packageJson = JSON.parse(
await readFile(sourcePackageJsonPath, 'utf8'),
);
/** @type {import('nx/src/utils/package-json').PackageJson} */
const rootPackageJson = JSON.parse(
await readFile('package.json', 'utf8'),
);
packageJson.license = rootPackageJson.license;
if (project.name === 'cli') {
packageJson.homepage = rootPackageJson.homepage;
} else {
packageJson.homepage = `https://github.com/code-pushup/cli/tree/main/packages/${project.name}#readme`;
}
packageJson.bugs = rootPackageJson.bugs;
packageJson.repository = {
...rootPackageJson.repository,
directory: project.data.root,
};
packageJson.contributors = rootPackageJson.contributors;
packageJson.publishConfig = { access: 'public' };
packageJson.type = 'module';
packageJson.main = './index.js';
packageJson.types = './src/index.d.ts';
await writeFile(
outputPackageJsonPath,
JSON.stringify(packageJson, null, 2),
);
});
},
},
],
};