@ngrs/build 0.3.2 → 0.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngrs/build",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "1:1 Angular CLI compatible Application Builder, Vite Dev-Server & CLI powered by @ngrs/compiler and @ngrs/vite",
5
5
  "type": "module",
6
6
  "builders": "./builders.json",
@@ -52,8 +52,8 @@
52
52
  "src"
53
53
  ],
54
54
  "dependencies": {
55
- "@ngrs/compiler": "0.3.2",
56
- "@ngrs/vite": "0.3.2",
55
+ "@ngrs/compiler": "0.3.3",
56
+ "@ngrs/vite": "0.3.3",
57
57
  "happy-dom": "^20.0.0",
58
58
  "jsonc-parser": "^3.3.1",
59
59
  "mrmime": "^2.0.1",
@@ -1,10 +1,14 @@
1
1
  'use strict';
2
2
 
3
+ const path = require('node:path');
4
+ const { createRequire } = require('node:module');
3
5
  const { applyEdits, modify, parse, printParseErrorCode } = require('jsonc-parser');
6
+ const ownPkg = require('../../package.json');
4
7
 
5
8
  const WORKSPACE_CANDIDATES = ['/angular.json', '/workspace.json'];
6
9
  const PACKAGE_JSON_CANDIDATES = ['/package.json', 'package.json'];
7
10
  const HAPPY_DOM_VERSION = '^20.0.0';
11
+ const NGRS_BUILD_VERSION = `^${ownPkg.version}`;
8
12
 
9
13
  const BUILDER_MIGRATIONS = Object.freeze({
10
14
  '@angular/build:application': '@ngrs/build:application',
@@ -108,19 +112,22 @@ function migrateWorkspaceJsoncText(rawText, options = {}) {
108
112
  }
109
113
 
110
114
  /**
111
- * Updates `package.json` to standardize unit test DOM environment on `happy-dom` (adding
112
- * `"happy-dom"` to `devDependencies` if absent and removing legacy `"jsdom"` / `"@types/jsdom"`).
115
+ * Updates `package.json` to ensure `@ngrs/build` and `happy-dom` are declared in `devDependencies`
116
+ * (even when `ng add` skipped `npm install` due to a parent `node_modules` match) and removes
117
+ * legacy `"jsdom"` / `"@types/jsdom"`.
113
118
  *
114
119
  * @param {string} rawPkgText
115
- * @returns {{ updatedText: string, changed: boolean, addedHappyDom: boolean, removedJsdom: boolean }}
120
+ * @param {{ ensureNgrsBuild?: boolean }} [options]
121
+ * @returns {{ updatedText: string, changed: boolean, addedNgrsBuild: boolean, addedHappyDom: boolean, removedJsdom: boolean }}
116
122
  */
117
- function migratePackageJsonForHappyDom(rawPkgText) {
123
+ function migratePackageJsonForHappyDom(rawPkgText, options = {}) {
118
124
  const parseErrors = [];
119
125
  const pkg = parse(rawPkgText, parseErrors, { allowTrailingComma: true });
120
126
  if (parseErrors.length > 0 || !pkg || typeof pkg !== 'object') {
121
127
  return {
122
128
  updatedText: rawPkgText,
123
129
  changed: false,
130
+ addedNgrsBuild: false,
124
131
  addedHappyDom: false,
125
132
  removedJsdom: false,
126
133
  };
@@ -128,6 +135,7 @@ function migratePackageJsonForHappyDom(rawPkgText) {
128
135
 
129
136
  let updatedText = rawPkgText;
130
137
  let removedJsdom = false;
138
+ let addedNgrsBuild = false;
131
139
  let addedHappyDom = false;
132
140
 
133
141
  for (const section of ['devDependencies', 'dependencies']) {
@@ -144,6 +152,19 @@ function migratePackageJsonForHappyDom(rawPkgText) {
144
152
  }
145
153
  }
146
154
 
155
+ const ensureNgrsBuild = options.ensureNgrsBuild !== false;
156
+ const hasNgrsBuild =
157
+ (pkg.devDependencies && '@ngrs/build' in pkg.devDependencies) ||
158
+ (pkg.dependencies && '@ngrs/build' in pkg.dependencies);
159
+
160
+ if (ensureNgrsBuild && !hasNgrsBuild) {
161
+ const edits = modify(updatedText, ['devDependencies', '@ngrs/build'], NGRS_BUILD_VERSION, {
162
+ formattingOptions: { tabSize: 2, insertSpaces: true },
163
+ });
164
+ updatedText = applyEdits(updatedText, edits);
165
+ addedNgrsBuild = true;
166
+ }
167
+
147
168
  const hasHappyDom =
148
169
  (pkg.devDependencies && 'happy-dom' in pkg.devDependencies) ||
149
170
  (pkg.dependencies && 'happy-dom' in pkg.dependencies);
@@ -159,11 +180,33 @@ function migratePackageJsonForHappyDom(rawPkgText) {
159
180
  return {
160
181
  updatedText,
161
182
  changed: updatedText !== rawPkgText,
183
+ addedNgrsBuild,
162
184
  addedHappyDom,
163
185
  removedJsdom,
164
186
  };
165
187
  }
166
188
 
189
+ function trySchedulePackageInstallTask(context) {
190
+ if (typeof context?.addTask !== 'function') {
191
+ return;
192
+ }
193
+ const loaders = [
194
+ () => createRequire(path.join(process.cwd(), 'package.json'))('@angular-devkit/schematics/tasks'),
195
+ () => require('@angular-devkit/schematics/tasks'),
196
+ ];
197
+ for (const load of loaders) {
198
+ try {
199
+ const tasksMod = load();
200
+ if (tasksMod && typeof tasksMod.NodePackageInstallTask === 'function') {
201
+ context.addTask(new tasksMod.NodePackageInstallTask());
202
+ return;
203
+ }
204
+ } catch {
205
+ // Optional in standalone unit tests
206
+ }
207
+ }
208
+ }
209
+
167
210
  /**
168
211
  * Official Angular CLI `ng add @ngrs/build` Schematic Rule factory.
169
212
  *
@@ -211,9 +254,15 @@ function ngAdd(options = {}) {
211
254
  if (pkgRes.removedJsdom) {
212
255
  context?.logger?.info?.('Removed legacy jsdom dependency from package.json');
213
256
  }
257
+ if (pkgRes.addedNgrsBuild) {
258
+ context?.logger?.info?.(`Added @ngrs/build (${NGRS_BUILD_VERSION}) to devDependencies`);
259
+ }
214
260
  if (pkgRes.addedHappyDom) {
215
261
  context?.logger?.info?.(`Added happy-dom (${HAPPY_DOM_VERSION}) to devDependencies`);
216
262
  }
263
+ if (pkgRes.addedNgrsBuild || pkgRes.addedHappyDom) {
264
+ trySchedulePackageInstallTask(context);
265
+ }
217
266
  }
218
267
  }
219
268
  }
@@ -227,5 +276,6 @@ module.exports.default = ngAdd;
227
276
  module.exports.ngAdd = ngAdd;
228
277
  module.exports.BUILDER_MIGRATIONS = BUILDER_MIGRATIONS;
229
278
  module.exports.HAPPY_DOM_VERSION = HAPPY_DOM_VERSION;
279
+ module.exports.NGRS_BUILD_VERSION = NGRS_BUILD_VERSION;
230
280
  module.exports.migrateWorkspaceJsoncText = migrateWorkspaceJsoncText;
231
281
  module.exports.migratePackageJsonForHappyDom = migratePackageJsonForHappyDom;
@@ -444,7 +444,7 @@ export async function executeApplicationBuild(
444
444
  find: r.replace,
445
445
  replacement: r.with,
446
446
  })),
447
- ...(appOptions.conditions ? { conditions: appOptions.conditions } : {}),
447
+ ...(appOptions.conditions?.length ? { conditions: appOptions.conditions } : {}),
448
448
  },
449
449
  css: {
450
450
  preprocessorOptions: {
@@ -747,7 +747,7 @@ export async function startDevServer(
747
747
  find: r.replace,
748
748
  replacement: r.with,
749
749
  })),
750
- ...(appOptions.conditions ? { conditions: appOptions.conditions } : {}),
750
+ ...(appOptions.conditions?.length ? { conditions: appOptions.conditions } : {}),
751
751
  },
752
752
  css: {
753
753
  devSourcemap: Boolean(appOptions.sourceMap.styles ?? true),
@@ -93,6 +93,19 @@ function findExternalVitestConfig(dirs) {
93
93
  return false;
94
94
  }
95
95
 
96
+ function normalizeNonEmptyReporters(reporters) {
97
+ if (!Array.isArray(reporters) || reporters.length === 0) {
98
+ return undefined;
99
+ }
100
+ const cleaned = reporters.filter(
101
+ (entry) =>
102
+ (typeof entry === 'string' && entry.trim().length > 0) ||
103
+ (Array.isArray(entry) && entry.length > 0) ||
104
+ (entry && typeof entry === 'object' && !Array.isArray(entry)),
105
+ );
106
+ return cleaned.length > 0 ? cleaned : undefined;
107
+ }
108
+
96
109
  /**
97
110
  * Architect-compatible `@ngrs/build:unit-test` async generator powered by Vitest + `@ngrs/vite/testing`.
98
111
  * 1:1 drop-in replacement for `@angular/build:unit-test` (`ng test` / `ngrs test`).
@@ -134,31 +147,49 @@ export async function* executeUnitTestBuilder(options = {}, context = {}) {
134
147
  : toPosixPath(relProjectRoot));
135
148
  const projectSourceRoot = path.resolve(workspaceRoot, relSourceRoot);
136
149
 
137
- // Merge inherited options from `buildTarget` (`::development` by default)
150
+ // Merge inherited options from `buildTarget` (`::development` by default, falling back to base
151
+ // `build` options if the project does not define a named `development` configuration)
152
+ const hasExplicitBuildTarget = Boolean(options.buildTarget);
138
153
  const buildTargetSpec = parseTargetString(
139
154
  options.buildTarget || '::development',
140
155
  projectName || 'app',
141
156
  'build',
142
157
  );
158
+ const fallbackBaseSpec = {
159
+ project: buildTargetSpec.project,
160
+ target: buildTargetSpec.target,
161
+ };
143
162
  let baseBuildOpts = {};
144
163
  if (context?.getTargetOptions) {
145
164
  try {
146
165
  baseBuildOpts = (await context.getTargetOptions(buildTargetSpec)) || {};
147
166
  } catch {
148
- baseBuildOpts = {};
167
+ if (!hasExplicitBuildTarget) {
168
+ try {
169
+ baseBuildOpts = (await context.getTargetOptions(fallbackBaseSpec)) || {};
170
+ } catch {
171
+ baseBuildOpts = {};
172
+ }
173
+ }
149
174
  }
150
175
  } else if (loadedWorkspace) {
151
176
  try {
152
177
  baseBuildOpts =
153
178
  resolveTargetOptions(loadedWorkspace, buildTargetSpec, 'development') || {};
154
179
  } catch {
155
- baseBuildOpts = {};
180
+ if (!hasExplicitBuildTarget) {
181
+ try {
182
+ baseBuildOpts = resolveTargetOptions(loadedWorkspace, fallbackBaseSpec) || {};
183
+ } catch {
184
+ baseBuildOpts = {};
185
+ }
186
+ }
156
187
  }
157
188
  }
158
189
 
159
190
  const searchDirs = [projectRoot, workspaceRoot, process.cwd()];
160
- const includePatterns = options.include ?? ['**/*.spec.ts'];
161
- const excludePatterns = options.exclude ?? [];
191
+ const includePatterns = options.include?.length ? options.include : ['**/*.spec.ts'];
192
+ const excludePatterns = options.exclude?.length ? options.exclude : [];
162
193
 
163
194
  const scanRoot = fs.existsSync(projectSourceRoot) ? projectSourceRoot : projectRoot;
164
195
  const discoveredTestFiles = await discoverSpecFiles(
@@ -229,8 +260,12 @@ export async function* executeUnitTestBuilder(options = {}, context = {}) {
229
260
  }
230
261
  }
231
262
 
263
+ const explicitPolyfills =
264
+ Array.isArray(options.polyfills) && options.polyfills.length > 0
265
+ ? options.polyfills
266
+ : undefined;
232
267
  const rawPolyfills =
233
- options.polyfills ??
268
+ explicitPolyfills ??
234
269
  (Array.isArray(baseBuildOpts.polyfills)
235
270
  ? baseBuildOpts.polyfills
236
271
  : typeof baseBuildOpts.polyfills === 'string'
@@ -240,7 +275,13 @@ export async function* executeUnitTestBuilder(options = {}, context = {}) {
240
275
  const isInteractiveTty = Boolean(
241
276
  process.stdout.isTTY && process.stdin.isTTY && !process.env.CI,
242
277
  );
243
- const watch = Boolean(options.watch ?? (options.ui ? true : isInteractiveTty && context?.isCliInteractive === true));
278
+ let watch = Boolean(
279
+ options.watch ?? (options.ui ? true : isInteractiveTty && context?.isCliInteractive !== false),
280
+ );
281
+ if (options.ui && options.watch === false) {
282
+ logger.warn("The '--ui' option requires watch mode. The '--no-watch' flag will be ignored.");
283
+ watch = true;
284
+ }
244
285
 
245
286
  let externalConfigPath = false;
246
287
  if (typeof options.runnerConfig === 'string' && options.runnerConfig.length > 0) {
@@ -267,7 +308,7 @@ export async function* executeUnitTestBuilder(options = {}, context = {}) {
267
308
  tsconfig: explicitTsconfig,
268
309
  polyfills: rawPolyfills,
269
310
  providersFile: options.providersFile,
270
- setupFiles: options.setupFiles,
311
+ setupFiles: options.setupFiles?.length ? options.setupFiles : undefined,
271
312
  inlineStyleLanguage: baseBuildOpts.inlineStyleLanguage ?? 'css',
272
313
  stylePreprocessorOptions: baseBuildOpts.stylePreprocessorOptions,
273
314
  fileReplacements,
@@ -278,6 +319,9 @@ export async function* executeUnitTestBuilder(options = {}, context = {}) {
278
319
  exclude: excludePatterns,
279
320
  });
280
321
 
322
+ const normalizedReporters = normalizeNonEmptyReporters(options.reporters);
323
+ const normalizedCoverageReporters = normalizeNonEmptyReporters(options.coverageReporters);
324
+
281
325
  const cliOptions = {
282
326
  config: externalConfigPath,
283
327
  root: projectRoot,
@@ -285,16 +329,18 @@ export async function* executeUnitTestBuilder(options = {}, context = {}) {
285
329
  passWithNoTests: false,
286
330
  ...(options.filter !== undefined ? { testNamePattern: options.filter } : {}),
287
331
  ...(options.outputFile !== undefined ? { outputFile: options.outputFile } : {}),
288
- ...(options.reporters ? { reporters: options.reporters } : {}),
332
+ ...(normalizedReporters ? { reporters: normalizedReporters } : {}),
289
333
  ...(typeof options.ui === 'boolean' ? { ui: options.ui } : {}),
290
334
  ...(options.coverage
291
335
  ? {
292
336
  coverage: {
293
337
  enabled: true,
294
- ...(options.coverageReporters ? { reporter: options.coverageReporters } : {}),
295
- ...(options.coverageInclude ? { include: options.coverageInclude } : {}),
296
- ...(options.coverageExclude ? { exclude: options.coverageExclude } : {}),
297
- ...(options.coverageThresholds ? { thresholds: options.coverageThresholds } : {}),
338
+ ...(normalizedCoverageReporters ? { reporter: normalizedCoverageReporters } : {}),
339
+ ...(options.coverageInclude?.length ? { include: options.coverageInclude } : {}),
340
+ ...(options.coverageExclude?.length ? { exclude: options.coverageExclude } : {}),
341
+ ...(options.coverageThresholds && Object.keys(options.coverageThresholds).length > 0
342
+ ? { thresholds: options.coverageThresholds }
343
+ : {}),
298
344
  },
299
345
  }
300
346
  : {}),
package/src/ng-add.mjs CHANGED
@@ -7,6 +7,7 @@ const localRequire = createRequire(import.meta.url);
7
7
  const {
8
8
  BUILDER_MIGRATIONS,
9
9
  HAPPY_DOM_VERSION,
10
+ NGRS_BUILD_VERSION,
10
11
  migratePackageJsonForHappyDom,
11
12
  migrateWorkspaceJsoncText,
12
13
  ngAdd,
@@ -15,6 +16,7 @@ const {
15
16
  export {
16
17
  BUILDER_MIGRATIONS,
17
18
  HAPPY_DOM_VERSION,
19
+ NGRS_BUILD_VERSION,
18
20
  migratePackageJsonForHappyDom,
19
21
  migrateWorkspaceJsoncText,
20
22
  ngAdd,
@@ -56,6 +58,9 @@ export async function initWorkspaceWithNgrsBuild(options = {}) {
56
58
  if (pkgRes.removedJsdom) {
57
59
  options.logger?.info?.('Removed legacy jsdom dependency from package.json');
58
60
  }
61
+ if (pkgRes.addedNgrsBuild) {
62
+ options.logger?.info?.(`Added @ngrs/build (${NGRS_BUILD_VERSION}) to devDependencies`);
63
+ }
59
64
  if (pkgRes.addedHappyDom) {
60
65
  options.logger?.info?.(`Added happy-dom (${HAPPY_DOM_VERSION}) to devDependencies`);
61
66
  }
package/src/workspace.mjs CHANGED
@@ -760,7 +760,7 @@ export function normalizeApplicationOptions(
760
760
  deployUrl: rawOptions.deployUrl,
761
761
  define: rawOptions.define ? { ...rawOptions.define } : undefined,
762
762
  externalDependencies: normalizeExternals(rawOptions.externalDependencies),
763
- conditions: rawOptions.conditions,
763
+ conditions: rawOptions.conditions?.length ? rawOptions.conditions : undefined,
764
764
  budgets: Array.isArray(rawOptions.budgets) ? rawOptions.budgets : [],
765
765
  namedChunks: Boolean(rawOptions.namedChunks ?? false),
766
766
  subresourceIntegrity: Boolean(rawOptions.subresourceIntegrity ?? false),