@hublo/sentinel 1.2.0-alpha.2 → 1.2.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/sentinel.d.ts +1 -0
- package/dist/bin/sentinel.js +31 -5
- package/dist/{chunk-SA2RMTVI.js → chunk-RKVGZNJ7.js} +859 -577
- package/dist/index.js +1 -1
- package/dist/roles/build/nest/toolchain.d.ts +130 -0
- package/dist/roles/build/nest/toolchain.js +403 -0
- package/dist/roles/test/react/toolchain.d.ts +2 -0
- package/dist/roles/test/react/toolchain.js +9 -0
- package/package.json +25 -3
package/dist/index.js
CHANGED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { UserConfig, Plugin } from 'vite';
|
|
2
|
+
export { Plugin, PluginOption, UserConfig, UserConfigExport, defineConfig, loadEnv, mergeConfig } from 'vite';
|
|
3
|
+
|
|
4
|
+
/** A transformer as the target declares it, passed through to the config unchanged. */
|
|
5
|
+
interface TransformerDeclaration$1 {
|
|
6
|
+
name: string;
|
|
7
|
+
options?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
/** An asset as the target declares it, in `@nx/webpack`'s own shape. */
|
|
10
|
+
interface AssetDeclaration {
|
|
11
|
+
glob: string;
|
|
12
|
+
/** Relative to the workspace root, as the target declares it. */
|
|
13
|
+
input: string;
|
|
14
|
+
/** Relative to the output directory. */
|
|
15
|
+
output: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface NestServiceOptions {
|
|
19
|
+
/** The nx project name. Keys the dependency graph, and names the module in messages. */
|
|
20
|
+
project: string;
|
|
21
|
+
/** Absolute path to the module's directory, normally `__dirname`. */
|
|
22
|
+
root: string;
|
|
23
|
+
/** Absolute path to the workspace root. */
|
|
24
|
+
workspaceRoot: string;
|
|
25
|
+
/** Entry point, relative to `root`. */
|
|
26
|
+
entry?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The tsconfig whose emit must be preserved, when the service uses neither conventional name.
|
|
29
|
+
*
|
|
30
|
+
* Relative to `root`. Defaults to `tsconfig.app.json`, then `tsconfig.json`.
|
|
31
|
+
*/
|
|
32
|
+
tsconfig?: string;
|
|
33
|
+
/** Output directory, relative to `workspaceRoot`. */
|
|
34
|
+
outDir?: string;
|
|
35
|
+
/**
|
|
36
|
+
* TypeScript transformers this service compiles with, as its build target declared them.
|
|
37
|
+
*
|
|
38
|
+
* Read off the webpack target by `--init` and written here, never inferred: what a service
|
|
39
|
+
* compiles with is the service's own. Eight services and BFFs run `@nestjs/swagger/plugin`,
|
|
40
|
+
* which writes the `@ApiProperty` decorators their DTOs do not declare by hand. Building
|
|
41
|
+
* without it succeeds and silently costs them most of their published contract (measured on
|
|
42
|
+
* `institution`: 385 insertions, 1378 deletions in the committed OpenAPI document).
|
|
43
|
+
*/
|
|
44
|
+
transformers?: readonly TransformerDeclaration$1[];
|
|
45
|
+
/**
|
|
46
|
+
* Files copied into the output beside the bundle, in the shape `@nx/webpack` declared them.
|
|
47
|
+
*
|
|
48
|
+
* One service here uses it: `planning-period` ships the pug, css and ttf templates it renders
|
|
49
|
+
* printed documents from. Without them the service starts and fails on its first print.
|
|
50
|
+
*/
|
|
51
|
+
assets?: readonly AssetDeclaration[];
|
|
52
|
+
/**
|
|
53
|
+
* Anything this service needs that the shape does not give it.
|
|
54
|
+
*
|
|
55
|
+
* Merged with Vite's own `mergeConfig`, never with a merge of our own: plugins concatenate
|
|
56
|
+
* and aliases stack the way Vite does it everywhere else, so there is no second set of
|
|
57
|
+
* semantics to learn or to document.
|
|
58
|
+
*/
|
|
59
|
+
overrides?: UserConfig;
|
|
60
|
+
}
|
|
61
|
+
declare const nestService: (options: NestServiceOptions) => UserConfig;
|
|
62
|
+
|
|
63
|
+
/** A TypeScript transformer a service runs at build time, as its build target declares it. */
|
|
64
|
+
interface TransformerDeclaration {
|
|
65
|
+
/** The package to load it from, e.g. `@nestjs/swagger/plugin`. */
|
|
66
|
+
name: string;
|
|
67
|
+
/** Passed to the transformer's own factory, unread here. */
|
|
68
|
+
options?: Record<string, unknown>;
|
|
69
|
+
}
|
|
70
|
+
interface DecoratorMetadataOptions {
|
|
71
|
+
/** The module being built. Its tsconfig is the one whose emit must be preserved. */
|
|
72
|
+
root: string;
|
|
73
|
+
/**
|
|
74
|
+
* An explicit tsconfig, when the service does not use either conventional name.
|
|
75
|
+
*
|
|
76
|
+
* Relative paths resolve against `root`.
|
|
77
|
+
*/
|
|
78
|
+
tsconfig?: string;
|
|
79
|
+
/** The transformers this service declared, translated from its webpack target. */
|
|
80
|
+
transformers?: readonly TransformerDeclaration[];
|
|
81
|
+
}
|
|
82
|
+
declare const decoratorMetadata: (options: DecoratorMetadataOptions) => Plugin;
|
|
83
|
+
|
|
84
|
+
interface NodeManifestOptions {
|
|
85
|
+
/** The nx project name, which is how the graph is keyed. */
|
|
86
|
+
project: string;
|
|
87
|
+
/** Absolute path to the workspace root. */
|
|
88
|
+
workspaceRoot: string;
|
|
89
|
+
/** Where the bundle is written; the two files land beside it. */
|
|
90
|
+
outDir: string;
|
|
91
|
+
/** Entry file name, written as `main` so `node .` resolves inside the image. */
|
|
92
|
+
entry?: string;
|
|
93
|
+
/**
|
|
94
|
+
* Packages the IMAGE provides, which are therefore allowed to be required without being
|
|
95
|
+
* declared in the manifest.
|
|
96
|
+
*
|
|
97
|
+
* The generated Prisma clients are the case this exists for: the Dockerfile copies
|
|
98
|
+
* `node_modules/@prisma` from its own stage, so they resolve at runtime while no module
|
|
99
|
+
* declares them. Everything else that is required and undeclared is a bug, and the check
|
|
100
|
+
* below refuses the build.
|
|
101
|
+
*/
|
|
102
|
+
providedByImage?: readonly string[];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Writes the manifest and lockfile once the bundle is on disk.
|
|
106
|
+
*
|
|
107
|
+
* `closeBundle` rather than `writeBundle`, so the files land after Vite has finished with the
|
|
108
|
+
* directory and cannot be cleared by `emptyOutDir`.
|
|
109
|
+
*
|
|
110
|
+
* `closeBundle` also runs after a FAILED build, where there is no directory to write into. Left
|
|
111
|
+
* alone this hook then threw `ENOENT` on the manifest, and since it is the last error raised it
|
|
112
|
+
* became the one Vite printed, hiding the failure that actually stopped the build. It cost two
|
|
113
|
+
* diagnoses before being recognised, so the missing directory is now read as what it is: the
|
|
114
|
+
* bundle was never written, and this plugin has nothing to say about why.
|
|
115
|
+
*/
|
|
116
|
+
declare const nodeManifest: (options: NodeManifestOptions) => Plugin;
|
|
117
|
+
|
|
118
|
+
interface Alias {
|
|
119
|
+
find: RegExp;
|
|
120
|
+
replacement: string;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Read the mappings from the workspace's base tsconfig.
|
|
124
|
+
*
|
|
125
|
+
* Longest pattern first, because Vite takes the first alias that matches and the mappings
|
|
126
|
+
* overlap by design: `@front/theme/node` must not be swallowed by `@front/theme`.
|
|
127
|
+
*/
|
|
128
|
+
declare const tsconfigAliases: (workspaceRoot: string, file?: string) => Alias[];
|
|
129
|
+
|
|
130
|
+
export { type Alias, type DecoratorMetadataOptions, type NestServiceOptions, type NodeManifestOptions, decoratorMetadata, nestService, nodeManifest, tsconfigAliases };
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// src/roles/build/nest/nest-service.ts
|
|
2
|
+
import { join as join4 } from "path";
|
|
3
|
+
import { mergeConfig } from "vite";
|
|
4
|
+
|
|
5
|
+
// src/roles/build/nest/copy-assets.ts
|
|
6
|
+
import { cpSync, existsSync, mkdirSync, readdirSync, statSync } from "fs";
|
|
7
|
+
import { dirname, join, relative, sep } from "path";
|
|
8
|
+
function globToRegExp(glob) {
|
|
9
|
+
let pattern = "";
|
|
10
|
+
for (let index = 0; index < glob.length; index++) {
|
|
11
|
+
const char = glob[index] ?? "";
|
|
12
|
+
if (char === "*") {
|
|
13
|
+
if (glob[index + 1] === "*") {
|
|
14
|
+
pattern += glob[index + 2] === "/" ? "(?:.*/)?" : ".*";
|
|
15
|
+
index += glob[index + 2] === "/" ? 2 : 1;
|
|
16
|
+
} else {
|
|
17
|
+
pattern += "[^/]*";
|
|
18
|
+
}
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (char === "{") {
|
|
22
|
+
const close = glob.indexOf("}", index);
|
|
23
|
+
if (close === -1)
|
|
24
|
+
throw new Error(`sentinel build(nest): unbalanced \`{\` in asset glob ${glob}`);
|
|
25
|
+
const alternatives = glob.slice(index + 1, close).split(",");
|
|
26
|
+
pattern += `(?:${alternatives.map((one) => one.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})`;
|
|
27
|
+
index = close;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
pattern += char.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
|
|
31
|
+
}
|
|
32
|
+
return new RegExp(`^${pattern}$`);
|
|
33
|
+
}
|
|
34
|
+
function filesUnder(root) {
|
|
35
|
+
const found = [];
|
|
36
|
+
const walk = (dir) => {
|
|
37
|
+
for (const entry of readdirSync(dir)) {
|
|
38
|
+
const full = join(dir, entry);
|
|
39
|
+
if (statSync(full).isDirectory()) walk(full);
|
|
40
|
+
else found.push(relative(root, full).split(sep).join("/"));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
walk(root);
|
|
44
|
+
return found;
|
|
45
|
+
}
|
|
46
|
+
function resolveAssets(options) {
|
|
47
|
+
const copies = [];
|
|
48
|
+
for (const asset of options.assets) {
|
|
49
|
+
const input = join(options.workspaceRoot, asset.input);
|
|
50
|
+
if (!existsSync(input)) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`sentinel build(nest): the build target declares an asset from \`${asset.input}\`, which does not exist. webpack copied it into the output, so the built service would be missing files it reads at runtime.`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
const matcher = globToRegExp(asset.glob);
|
|
56
|
+
const matched = filesUnder(input).filter((file) => matcher.test(file));
|
|
57
|
+
if (matched.length === 0) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`sentinel build(nest): the asset glob \`${asset.glob}\` under \`${asset.input}\` matches no file. An entry that matches nothing is the same silent loss as no entry at all.`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
for (const file of matched) {
|
|
63
|
+
copies.push({ from: join(input, file), to: join(options.outDir, asset.output, file) });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return copies;
|
|
67
|
+
}
|
|
68
|
+
var copyAssets = (options) => ({
|
|
69
|
+
name: "sentinel:copy-assets",
|
|
70
|
+
// After the bundle is written and the output directory has stopped being emptied.
|
|
71
|
+
closeBundle() {
|
|
72
|
+
for (const { from, to } of resolveAssets(options)) {
|
|
73
|
+
mkdirSync(dirname(to), { recursive: true });
|
|
74
|
+
cpSync(from, to);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// src/roles/build/nest/decorator-metadata.ts
|
|
80
|
+
import { existsSync as existsSync2 } from "fs";
|
|
81
|
+
import { createRequire } from "module";
|
|
82
|
+
import path from "path";
|
|
83
|
+
var TYPESCRIPT_SOURCE = /\.ts$/;
|
|
84
|
+
var TSCONFIG_CANDIDATES = ["tsconfig.app.json", "tsconfig.json"];
|
|
85
|
+
function loadCompiler(root) {
|
|
86
|
+
const require2 = createRequire(path.join(root, "noop.js"));
|
|
87
|
+
let compiler;
|
|
88
|
+
try {
|
|
89
|
+
compiler = require2("typescript");
|
|
90
|
+
} catch {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`sentinel build(nest): no \`typescript\` resolvable from ${root}. The build emits decorator metadata with the module's own compiler, so one has to be installed there.`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
if (typeof compiler.createProgram !== "function") {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`sentinel build(nest): the typescript resolved from ${root} (${compiler.version ?? "unknown"}) has no createProgram API. TypeScript 7 is the native port and exposes none; the emit API lives under @typescript/typescript6. Point this module's \`typescript\` at a compiler with an emit API, which is the one its \`typecheck\` target already uses.`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return compiler;
|
|
101
|
+
}
|
|
102
|
+
function resolveTsconfig(options) {
|
|
103
|
+
if (options.tsconfig) {
|
|
104
|
+
const explicit = path.resolve(options.root, options.tsconfig);
|
|
105
|
+
if (!existsSync2(explicit)) {
|
|
106
|
+
throw new Error(`sentinel build(nest): tsconfig not found at ${explicit}`);
|
|
107
|
+
}
|
|
108
|
+
return explicit;
|
|
109
|
+
}
|
|
110
|
+
for (const candidate of TSCONFIG_CANDIDATES) {
|
|
111
|
+
const found = path.join(options.root, candidate);
|
|
112
|
+
if (existsSync2(found)) return found;
|
|
113
|
+
}
|
|
114
|
+
throw new Error(
|
|
115
|
+
`sentinel build(nest): no ${TSCONFIG_CANDIDATES.join(" or ")} in ${options.root}. Pass \`tsconfig\` if this service keeps it elsewhere.`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
function readConfig(ts, options) {
|
|
119
|
+
const configPath = resolveTsconfig(options);
|
|
120
|
+
const parsed = ts.getParsedCommandLineOfConfigFile(configPath, {}, {
|
|
121
|
+
...ts.sys,
|
|
122
|
+
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`sentinel build(nest): could not read ${configPath}: ` + ts.flattenDiagnosticMessageText(diagnostic.messageText, " ")
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
fileNames: parsed?.fileNames ?? [],
|
|
130
|
+
compilerOptions: {
|
|
131
|
+
...parsed?.options,
|
|
132
|
+
// ESM out, so Rollup sees imports and exports rather than an opaque `require` it cannot
|
|
133
|
+
// follow. The service still SHIPS as CJS: that conversion is the bundler's, further down.
|
|
134
|
+
module: ts.ModuleKind.ESNext,
|
|
135
|
+
// Vite consumes the map; the tsconfig's own answer is about a different pipeline.
|
|
136
|
+
sourceMap: true,
|
|
137
|
+
inlineSourceMap: false,
|
|
138
|
+
inlineSources: false,
|
|
139
|
+
// Types are another target's job, and emitting them here would write into the source tree.
|
|
140
|
+
declaration: false,
|
|
141
|
+
declarationMap: false,
|
|
142
|
+
emitDeclarationOnly: false,
|
|
143
|
+
noEmit: false,
|
|
144
|
+
// `composite` projects refuse to emit without a `tsBuildInfoFile`, and there is no
|
|
145
|
+
// incremental build here to inform.
|
|
146
|
+
composite: false,
|
|
147
|
+
incremental: false
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function loadTransformers(root, program, declared) {
|
|
152
|
+
const require2 = createRequire(path.join(root, "noop.js"));
|
|
153
|
+
return declared.map(({ name, options }) => {
|
|
154
|
+
let loaded;
|
|
155
|
+
try {
|
|
156
|
+
loaded = require2(name);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`sentinel build(nest): the build target declares the transformer \`${name}\`, which cannot be loaded from ${root}: ${error instanceof Error ? error.message : String(error)}`,
|
|
160
|
+
{ cause: error }
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
if (typeof loaded.before !== "function") {
|
|
164
|
+
throw new Error(
|
|
165
|
+
`sentinel build(nest): \`${name}\` exports no \`before\` factory, so it cannot run as a TypeScript transformer. Building without it would drop whatever it contributes.`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
const factory = loaded.before;
|
|
169
|
+
return factory(options, program);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
var decoratorMetadata = (options) => {
|
|
173
|
+
const typeAware = (options.transformers ?? []).length > 0;
|
|
174
|
+
let ts;
|
|
175
|
+
let program;
|
|
176
|
+
let compilerOptions;
|
|
177
|
+
let before = [];
|
|
178
|
+
const withoutTypes = [];
|
|
179
|
+
return {
|
|
180
|
+
name: "sentinel:decorator-metadata",
|
|
181
|
+
// Before Vite's own transform, so esbuild never sees the decorators it cannot handle, and so
|
|
182
|
+
// the source this plugin reads from the Program is still the source on disk.
|
|
183
|
+
enforce: "pre",
|
|
184
|
+
buildStart() {
|
|
185
|
+
ts = loadCompiler(options.root);
|
|
186
|
+
const config = readConfig(ts, options);
|
|
187
|
+
compilerOptions = config.compilerOptions;
|
|
188
|
+
if (!typeAware) return;
|
|
189
|
+
program = ts.createProgram(config.fileNames, config.compilerOptions);
|
|
190
|
+
before = loadTransformers(options.root, program, options.transformers ?? []);
|
|
191
|
+
},
|
|
192
|
+
transform(code, id) {
|
|
193
|
+
if (!TYPESCRIPT_SOURCE.test(id) || id.includes("node_modules")) return null;
|
|
194
|
+
if (ts === void 0 || compilerOptions === void 0) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
`sentinel build(nest): the compiler was never prepared, so ${id} would be emitted with defaults rather than this module's tsconfig.`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const sourceFile = program?.getSourceFile(id);
|
|
200
|
+
if (program === void 0 || sourceFile === void 0 || sourceFile.text !== code) {
|
|
201
|
+
if (typeAware) withoutTypes.push(id);
|
|
202
|
+
const output = ts.transpileModule(code, { fileName: id, compilerOptions });
|
|
203
|
+
return { code: output.outputText, map: output.sourceMapText ?? null };
|
|
204
|
+
}
|
|
205
|
+
let emitted;
|
|
206
|
+
let map;
|
|
207
|
+
program.emit(
|
|
208
|
+
sourceFile,
|
|
209
|
+
(fileName, text) => {
|
|
210
|
+
if (fileName.endsWith(".map")) map = text;
|
|
211
|
+
else emitted = text;
|
|
212
|
+
},
|
|
213
|
+
void 0,
|
|
214
|
+
false,
|
|
215
|
+
{ before }
|
|
216
|
+
);
|
|
217
|
+
if (emitted === void 0) {
|
|
218
|
+
throw new Error(`sentinel build(nest): TypeScript emitted nothing for ${id}.`);
|
|
219
|
+
}
|
|
220
|
+
return { code: emitted, map: map ?? null };
|
|
221
|
+
},
|
|
222
|
+
closeBundle() {
|
|
223
|
+
if (withoutTypes.length === 0) return;
|
|
224
|
+
const shown = withoutTypes.slice(0, 5).map((file) => path.relative(options.root, file));
|
|
225
|
+
this.warn(
|
|
226
|
+
`sentinel build(nest): ${withoutTypes.length} file(s) were emitted without type information, so their decorator metadata may differ from what ts-loader produced (${shown.join(", ")}${withoutTypes.length > shown.length ? ", \u2026" : ""}). They are outside this module's tsconfig, or another plugin rewrote them first.`
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// src/roles/build/nest/node-manifest.ts
|
|
233
|
+
import { existsSync as existsSync3, writeFileSync } from "fs";
|
|
234
|
+
import { isBuiltin } from "module";
|
|
235
|
+
import { join as join2 } from "path";
|
|
236
|
+
var DEFAULT_PROVIDED_BY_IMAGE = ["@prisma"];
|
|
237
|
+
var externalPackages = (bundle) => {
|
|
238
|
+
const emitted = new Set(Object.keys(bundle));
|
|
239
|
+
const packages = /* @__PURE__ */ new Set();
|
|
240
|
+
for (const emittedFile of Object.values(bundle)) {
|
|
241
|
+
if (emittedFile.type !== "chunk") continue;
|
|
242
|
+
for (const imported of emittedFile.imports) {
|
|
243
|
+
if (emitted.has(imported)) continue;
|
|
244
|
+
if (imported.startsWith(".") || imported.startsWith("/")) continue;
|
|
245
|
+
if (isBuiltin(imported)) continue;
|
|
246
|
+
const parts = imported.split("/");
|
|
247
|
+
const name = imported.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
248
|
+
if (name) packages.add(name);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return packages;
|
|
252
|
+
};
|
|
253
|
+
var nodeManifest = (options) => {
|
|
254
|
+
let required;
|
|
255
|
+
return {
|
|
256
|
+
name: "sentinel:node-manifest",
|
|
257
|
+
apply: "build",
|
|
258
|
+
generateBundle(_outputOptions, bundle) {
|
|
259
|
+
required = externalPackages(bundle);
|
|
260
|
+
},
|
|
261
|
+
async closeBundle() {
|
|
262
|
+
const entry = options.entry ?? "main.js";
|
|
263
|
+
if (!existsSync3(join2(options.outDir, entry))) return;
|
|
264
|
+
const { createProjectGraphAsync } = await import("nx/src/devkit-exports.js");
|
|
265
|
+
const { createPackageJson, createLockFile, getLockFileName } = await import("@nx/js");
|
|
266
|
+
const graph = await createProjectGraphAsync({ exitOnError: false });
|
|
267
|
+
const manifest = createPackageJson(options.project, graph, {
|
|
268
|
+
root: options.workspaceRoot,
|
|
269
|
+
isProduction: true
|
|
270
|
+
});
|
|
271
|
+
manifest.main = entry;
|
|
272
|
+
if (required === void 0) {
|
|
273
|
+
throw new Error(
|
|
274
|
+
`sentinel build(nest): the bundle was written but never inspected, so the manifest could not be checked against what it requires. Refusing to write one that might not install.`
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
assertManifestCovers(required, manifest, options);
|
|
278
|
+
writeFileSync(join2(options.outDir, "package.json"), `${JSON.stringify(manifest, null, 2)}
|
|
279
|
+
`);
|
|
280
|
+
writeFileSync(
|
|
281
|
+
join2(options.outDir, getLockFileName("pnpm")),
|
|
282
|
+
createLockFile(manifest, graph, "pnpm")
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
var assertManifestCovers = (required, manifest, options) => {
|
|
288
|
+
const declared = new Set(Object.keys(manifest.dependencies ?? {}));
|
|
289
|
+
const provided = options.providedByImage ?? DEFAULT_PROVIDED_BY_IMAGE;
|
|
290
|
+
const missing = [...required].filter((name) => !declared.has(name)).filter((name) => !provided.some((prefix) => name === prefix || name.startsWith(`${prefix}/`))).sort();
|
|
291
|
+
if (missing.length === 0) return;
|
|
292
|
+
throw new Error(
|
|
293
|
+
`sentinel build(nest): the bundle requires ${missing.length} package(s) the generated manifest does not declare, so the image would not install them: ${missing.join(", ")}. Either declare them in the module's package.json, inline them with \`ssr.noExternal\`, or list them in \`providedByImage\` if the Dockerfile copies them in.`
|
|
294
|
+
);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// src/roles/build/nest/tsconfig-aliases.ts
|
|
298
|
+
import { readFileSync } from "fs";
|
|
299
|
+
import { join as join3 } from "path";
|
|
300
|
+
var stripLineComments = (json) => json.replace(/^\s*\/\/.*$/gm, "");
|
|
301
|
+
var toAlias = (workspaceRoot, pattern, target) => {
|
|
302
|
+
const escaped = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/\\\*/g, "(.*)");
|
|
303
|
+
return {
|
|
304
|
+
find: new RegExp(`^${escaped}$`),
|
|
305
|
+
replacement: join3(workspaceRoot, target.replace(/\*/g, "$1"))
|
|
306
|
+
};
|
|
307
|
+
};
|
|
308
|
+
var tsconfigAliases = (workspaceRoot, file = "tsconfig.base.json") => {
|
|
309
|
+
const raw = readFileSync(join3(workspaceRoot, file), "utf8");
|
|
310
|
+
const { compilerOptions } = JSON.parse(stripLineComments(raw));
|
|
311
|
+
return Object.entries(compilerOptions?.paths ?? {}).flatMap(([pattern, targets]) => {
|
|
312
|
+
const [target] = targets;
|
|
313
|
+
return target === void 0 ? [] : [toAlias(workspaceRoot, pattern, target)];
|
|
314
|
+
}).sort((a, b) => b.find.source.length - a.find.source.length);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// src/roles/build/nest/nest-service.ts
|
|
318
|
+
var baseConfig = (options) => {
|
|
319
|
+
const outDir = join4(options.workspaceRoot, options.outDir ?? `dist/${options.project}`);
|
|
320
|
+
const entry = join4(options.root, options.entry ?? "src/main.ts");
|
|
321
|
+
return {
|
|
322
|
+
plugins: [
|
|
323
|
+
decoratorMetadata({
|
|
324
|
+
root: options.root,
|
|
325
|
+
tsconfig: options.tsconfig,
|
|
326
|
+
transformers: options.transformers
|
|
327
|
+
}),
|
|
328
|
+
...options.assets?.length ? [copyAssets({ workspaceRoot: options.workspaceRoot, outDir, assets: options.assets })] : [],
|
|
329
|
+
nodeManifest({
|
|
330
|
+
project: options.project,
|
|
331
|
+
workspaceRoot: options.workspaceRoot,
|
|
332
|
+
outDir
|
|
333
|
+
})
|
|
334
|
+
],
|
|
335
|
+
resolve: { alias: tsconfigAliases(options.workspaceRoot) },
|
|
336
|
+
ssr: {
|
|
337
|
+
// `importHelpers: true` in the workspace tsconfig makes TypeScript emit `require("tslib")`
|
|
338
|
+
// for `__decorate` and `__metadata`, so every decorated file depends on it. Hoisted into
|
|
339
|
+
// one scope Rollup inlined it and nobody noticed; preserving modules left it external, and
|
|
340
|
+
// the manifest nx generates from the project graph does not list it, because no module
|
|
341
|
+
// DECLARES tslib. The image therefore did not install it and the service died on its first
|
|
342
|
+
// require, only in the image: locally and in CI the workspace root has tslib.
|
|
343
|
+
noExternal: ["tslib"]
|
|
344
|
+
},
|
|
345
|
+
build: {
|
|
346
|
+
// An SSR build targets Node and leaves real packages external, which is what
|
|
347
|
+
// `webpack-node-externals` did: the image installs them from the generated manifest.
|
|
348
|
+
ssr: entry,
|
|
349
|
+
outDir,
|
|
350
|
+
emptyOutDir: true,
|
|
351
|
+
sourcemap: true,
|
|
352
|
+
target: "node20",
|
|
353
|
+
// The bundle is read by humans when a stack trace points into it, and minifying a
|
|
354
|
+
// server bundle buys nothing: it is never downloaded.
|
|
355
|
+
minify: false,
|
|
356
|
+
rollupOptions: {
|
|
357
|
+
output: {
|
|
358
|
+
format: "cjs",
|
|
359
|
+
// One output file per source module, instead of hoisting everything into one scope.
|
|
360
|
+
//
|
|
361
|
+
// This is not a preference, it is the only shape that keeps the OpenAPI contract.
|
|
362
|
+
// Merging every module into one scope forces Rollup to rename duplicate class names,
|
|
363
|
+
// and `@nestjs/swagger` keys its schemas on `class.name`, so a renamed class silently
|
|
364
|
+
// becomes a renamed schema in the published API. Measured on `network`, which has two
|
|
365
|
+
// such collisions: single bundle renames `InvalidPermissionError` and
|
|
366
|
+
// `PermissionNotFoundError`; preserving modules renames nothing and reproduces the
|
|
367
|
+
// committed contract byte for byte.
|
|
368
|
+
//
|
|
369
|
+
// The cost is 4.5 MB across 2003 files against 2.9 MB in one, still well under
|
|
370
|
+
// webpack's 6.9 MB for the same service, so nothing regresses against what it replaces.
|
|
371
|
+
preserveModules: true,
|
|
372
|
+
// The image runs `./dist/main.js`, so the entry keeps that name at the root while
|
|
373
|
+
// every other module keeps its own path. Under `preserveModules` a plain string here
|
|
374
|
+
// would be applied to all of them, which numbers them (`main962.js`) and loses the
|
|
375
|
+
// entry.
|
|
376
|
+
entryFileNames: (chunk) => chunk.facadeModuleId === entry ? "main.js" : "[name].js"
|
|
377
|
+
},
|
|
378
|
+
// Nest registers metadata as an IMPORT SIDE EFFECT: a decorator writes into a catalog
|
|
379
|
+
// when its module loads, and nothing references that module afterwards. Rollup may
|
|
380
|
+
// drop such a module; webpack never did.
|
|
381
|
+
//
|
|
382
|
+
// Measured on the first migrated service this changes nothing, so it is insurance
|
|
383
|
+
// rather than a fix, and it is recorded as such rather than credited with the smaller
|
|
384
|
+
// bundle (that comes from barrel re-exports the service does not use).
|
|
385
|
+
treeshake: { moduleSideEffects: true }
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
var nestService = (options) => options.overrides === void 0 ? baseConfig(options) : mergeConfig(baseConfig(options), options.overrides);
|
|
391
|
+
|
|
392
|
+
// src/roles/build/nest/toolchain.ts
|
|
393
|
+
import { defineConfig, loadEnv, mergeConfig as mergeConfig2 } from "vite";
|
|
394
|
+
export {
|
|
395
|
+
decoratorMetadata,
|
|
396
|
+
defineConfig,
|
|
397
|
+
loadEnv,
|
|
398
|
+
mergeConfig2 as mergeConfig,
|
|
399
|
+
nestService,
|
|
400
|
+
nodeManifest,
|
|
401
|
+
tsconfigAliases
|
|
402
|
+
};
|
|
403
|
+
//# sourceMappingURL=toolchain.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hublo/sentinel",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.21",
|
|
4
4
|
"description": "One CLI that guards code health across Hublo repos: shared lint/typescript/build/test presets, static & dynamic analysis, and architecture checks.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,6 +41,14 @@
|
|
|
41
41
|
"./build/react": {
|
|
42
42
|
"types": "./dist/roles/build/toolchain.d.ts",
|
|
43
43
|
"import": "./dist/roles/build/toolchain.js"
|
|
44
|
+
},
|
|
45
|
+
"./build/nest": {
|
|
46
|
+
"types": "./dist/roles/build/nest/toolchain.d.ts",
|
|
47
|
+
"import": "./dist/roles/build/nest/toolchain.js"
|
|
48
|
+
},
|
|
49
|
+
"./test/react": {
|
|
50
|
+
"types": "./dist/roles/test/react/toolchain.d.ts",
|
|
51
|
+
"import": "./dist/roles/test/react/toolchain.js"
|
|
44
52
|
}
|
|
45
53
|
},
|
|
46
54
|
"files": [
|
|
@@ -66,8 +74,10 @@
|
|
|
66
74
|
"oxfmt": "0.63.0",
|
|
67
75
|
"oxlint": "1.77.0",
|
|
68
76
|
"oxlint-tsgolint": "7.0.2001",
|
|
69
|
-
"
|
|
70
|
-
"vite
|
|
77
|
+
"typescript": "5.9.3",
|
|
78
|
+
"vite": "8.2.2",
|
|
79
|
+
"vite-plugin-svgr": "5.2.0",
|
|
80
|
+
"vitest": "4.1.4"
|
|
71
81
|
},
|
|
72
82
|
"devDependencies": {
|
|
73
83
|
"@hublo/sentinel": "link:.",
|
|
@@ -96,6 +106,18 @@
|
|
|
96
106
|
}
|
|
97
107
|
}
|
|
98
108
|
},
|
|
109
|
+
"peerDependencies": {
|
|
110
|
+
"@nx/js": ">= 21",
|
|
111
|
+
"nx": ">= 21"
|
|
112
|
+
},
|
|
113
|
+
"peerDependenciesMeta": {
|
|
114
|
+
"nx": {
|
|
115
|
+
"optional": true
|
|
116
|
+
},
|
|
117
|
+
"@nx/js": {
|
|
118
|
+
"optional": true
|
|
119
|
+
}
|
|
120
|
+
},
|
|
99
121
|
"scripts": {
|
|
100
122
|
"build": "tsup && tsx scripts/build-presets.ts",
|
|
101
123
|
"dev": "tsup --watch",
|