@styloviz/schematics 2.0.1 → 2.1.0

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/README.md CHANGED
@@ -17,12 +17,74 @@ ng add @styloviz/core
17
17
  - creates `.postcssrc.json` for the Tailwind 4 PostCSS plugin, unless you have a
18
18
  PostCSS config;
19
19
  - prepends `@import "tailwindcss"`, the kit stylesheet and an `@source` for the
20
- kit's templates to your global stylesheet;
20
+ kit's templates to your global stylesheet (`@styloviz-pro` too, when Pro is
21
+ installed);
21
22
  - adds `provideStyloviz()` to `app.config.ts`;
22
- - adds a demo page at `/styloviz` (pass `--demo=false` to skip it).
23
+ - adds a demo page at `/styloviz` (pass `--demo=false` to skip it);
24
+ - under pnpm, adds two lines to `.npmrc` that hoist the `@styloviz/*` and
25
+ `@styloviz-pro/*` packages. pnpm links only your direct dependencies, so
26
+ without them nothing that `@styloviz/all` or `@styloviz-pro/all` brings in
27
+ resolves. pnpm then asks to rebuild `node_modules` once: answer Y;
28
+ - runs `sources` (below), after the install: Tailwind then reads only the kit
29
+ files your app uses, and the first build of a new app fits Angular's default
30
+ budgets. Where that cannot work yet — a kit package in `package.json` that
31
+ is not installed (`--skip-install`), or a source root that contains
32
+ `node_modules` — the broad `@source` lines stay and `ng add` says what to do.
33
+ `--sources=false` keeps the broad lines.
23
34
 
24
35
  Running it again changes nothing. The same schematic is `ng add @styloviz/schematics`.
25
36
 
37
+ ## `ng g @styloviz/core:sources`
38
+
39
+ Makes an app pay, in stylesheet size, only for the kit components it uses:
40
+
41
+ ```sh
42
+ ng g @styloviz/core:sources # write the list
43
+ ng g @styloviz/core:sources --check # in CI: fail when the list is out of date
44
+ ng g @styloviz/core:sources --all # go back to scanning every kit package
45
+
46
+ ng build --stats-json # exact: what the production build ships
47
+ ng g @styloviz/core:sources --from-build
48
+ ```
49
+
50
+ A broad `@source` for `node_modules/@styloviz` scans every installed package. With an install
51
+ bundle that is every package, so even an empty app builds a 434 kB stylesheet
52
+ and an initial bundle over Angular's 500 kB budget warning. `ng add` runs this
53
+ schematic for you; run it yourself after you change your kit imports. It follows the app's
54
+ imports into the installed packages — by name through each package's barrel, so
55
+ one checkbox from `@styloviz/forms` reaches the checkbox file and nothing else —
56
+ and writes one `@source` per kit file reached into a marked block of the global
57
+ stylesheet. The `ng add` demo app goes from 434 kB to 86 kB of CSS.
58
+
59
+ It also adds `source(none)` to `@import "tailwindcss"`. That is needed: Tailwind
60
+ widens a file `@source` inside `node_modules` to its whole package whenever
61
+ another source contains `node_modules`, and the default import scans the whole
62
+ workspace. So the block also lists what the automatic scan used to find: the
63
+ app's source root, every library project's source root, the build's asset
64
+ folders, and any app file imported from outside them.
65
+
66
+ It also imports the kit stylesheet in parts. `@styloviz/core/styles/styloviz.css`
67
+ is shipped cut into `styles/parts/*.css` too: the tokens and the bare-border
68
+ default every app needs, and the rest — the glow, each preset theme, each
69
+ component's animations — as parts of their own. The single import becomes a
70
+ second marked block of `@import`s for the parts your code and the listed kit
71
+ files mention, in styloviz.css's order, so the cascade is unchanged. The `ng
72
+ add` demo app goes from 86 kB to 47 kB of CSS this way. A preset whose class
73
+ name your code builds at run time (`'sv-theme-' + name`) brings all four.
74
+
75
+ By default the list is every kit file the imports reach, which is a safe
76
+ superset: a component you import but never put in a template is still listed.
77
+ `--from-build` reads the kit files a production build actually shipped from its
78
+ `--stats-json` output instead (lazy chunks and the server build included), and
79
+ refuses stats older than your code or your install. `--stats <file>` names the
80
+ stats file when it is not in the app's output folder.
81
+
82
+ Run it again whenever you import another kit component; `--check` catches a
83
+ forgotten run (with `--from-build`, after a fresh `ng build --stats-json`). In a
84
+ development build, a component whose file is missing from the list says so in
85
+ the console, with this command. A class name built at run time from pieces (`'bg-' + tone`)
86
+ was never found by any scan; list it with `@source inline("…")`.
87
+
26
88
  ## `ng g @styloviz/schematics:migrate-primeng`
27
89
 
28
90
  Moves a PrimeNG 21 app onto Styloviz, applying the selector map that
package/collection.json CHANGED
@@ -6,6 +6,18 @@
6
6
  "factory": "./ng-add/index#ngAdd",
7
7
  "schema": "./ng-add/schema.json"
8
8
  },
9
+ "ng-add-sources": {
10
+ "description": "ng add, step 8: run sources once the packages are installed, keeping the broad lines where an exact list cannot be written.",
11
+ "factory": "./ng-add/index#ngAddSources",
12
+ "schema": "./ng-add/sources-schema.json",
13
+ "private": true,
14
+ "hidden": true
15
+ },
16
+ "sources": {
17
+ "description": "Point Tailwind at only the kit files the app imports, in one marked block of the global stylesheet. --check fails when it is out of date; --all scans every installed kit package again.",
18
+ "factory": "./sources/index#sources",
19
+ "schema": "./sources/schema.json"
20
+ },
9
21
  "migrate-primeng": {
10
22
  "description": "Rewrite PrimeNG 21 component selectors and imports to their Styloviz equivalents, and report what could not be mapped.",
11
23
  "factory": "./migrate-primeng/index#migratePrimeng",
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+ /**
3
+ * The marked block `ng g @styloviz/core:sources` owns in an app's global
4
+ * stylesheet. `ng add` reads it too: once the block is there, the block decides
5
+ * what Tailwind scans, and `ng add` must not put the broad line back.
6
+ */
7
+ const BLOCK_START =
8
+ '/* styloviz:sources start — written by `ng g @styloviz/core:sources` from what this app imports. ' +
9
+ 'Run it again after importing another kit component; `--check` fails when this list is out of date. ' +
10
+ 'Do not edit between the two markers. */';
11
+ const BLOCK_END = '/* styloviz:sources end */';
12
+
13
+ const START_MARK = 'styloviz:sources start';
14
+ const END_MARK = 'styloviz:sources end';
15
+
16
+ /**
17
+ * The second block: the parts of the kit stylesheet the app uses, in place of
18
+ * the single `@import "@styloviz/core/styles/styloviz.css"` (M-1, step A3).
19
+ */
20
+ const PARTS_START =
21
+ '/* styloviz:parts start — written by `ng g @styloviz/core:sources`: the parts of the kit stylesheet this app uses, ' +
22
+ 'in the order styloviz.css has them. `--all` puts the single styloviz.css import back. Do not edit between the two markers. */';
23
+ const PARTS_END = '/* styloviz:parts end */';
24
+ const PARTS_START_MARK = 'styloviz:parts start';
25
+ const PARTS_END_MARK = 'styloviz:parts end';
26
+
27
+ /** Whether a stylesheet already carries the @source block. */
28
+ function hasSourcesBlock(css) {
29
+ return css.includes(START_MARK);
30
+ }
31
+
32
+ /** Whether a stylesheet imports the kit stylesheet in parts. */
33
+ function hasPartsBlock(css) {
34
+ return css.includes(PARTS_START_MARK);
35
+ }
36
+
37
+ module.exports = {
38
+ BLOCK_START,
39
+ BLOCK_END,
40
+ START_MARK,
41
+ END_MARK,
42
+ PARTS_START,
43
+ PARTS_END,
44
+ PARTS_START_MARK,
45
+ PARTS_END_MARK,
46
+ hasSourcesBlock,
47
+ hasPartsBlock,
48
+ };
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+ /**
3
+ * Reading angular.json — shared by `ng add` and `sources`, so both agree on
4
+ * which project is an app, where its code lives and which stylesheet is global.
5
+ */
6
+ const { SchematicsException } = require('@angular-devkit/schematics');
7
+ const { posix } = require('node:path');
8
+
9
+ function readJson(tree, path) {
10
+ const buf = tree.read(path);
11
+ if (!buf) return null;
12
+ try {
13
+ return JSON.parse(buf.toString('utf8'));
14
+ } catch {
15
+ throw new SchematicsException(`${path} is not valid JSON.`);
16
+ }
17
+ }
18
+
19
+ function readWorkspace(tree) {
20
+ const workspace = readJson(tree, '/angular.json');
21
+ if (!workspace) throw new SchematicsException('No angular.json — run this inside an Angular workspace.');
22
+ return workspace;
23
+ }
24
+
25
+ /** The project's build target, under either spelling angular.json allows. */
26
+ function buildTarget(project) {
27
+ return project.architect?.build ?? project.targets?.build;
28
+ }
29
+
30
+ /** Where a project's files are: its root, and its source root (default `<root>/src`). */
31
+ function projectPaths(project) {
32
+ const root = project.root || '';
33
+ return { root, sourceRoot: project.sourceRoot || posix.join(root, 'src') };
34
+ }
35
+
36
+ /** The first global `.css` stylesheet the project's build lists, or null. */
37
+ function globalCss(project) {
38
+ const styles = buildTarget(project)?.options?.styles ?? [];
39
+ return styles.map((s) => (typeof s === 'string' ? s : s.input)).find((s) => /\.css$/.test(s)) ?? null;
40
+ }
41
+
42
+ /** The app `ng add` sets up, and where its files are. */
43
+ function resolveProject(tree, options) {
44
+ const workspace = readWorkspace(tree);
45
+ const name =
46
+ options.project ||
47
+ Object.keys(workspace.projects).find((p) => workspace.projects[p].projectType === 'application');
48
+ const project = name && workspace.projects[name];
49
+ if (!project) throw new SchematicsException(`No application project ${options.project ? `"${options.project}" ` : ''}in angular.json.`);
50
+ const { root, sourceRoot } = projectPaths(project);
51
+ return { name, root, sourceRoot, styles: globalCss(project) ?? posix.join(sourceRoot, 'styles.css') };
52
+ }
53
+
54
+ module.exports = { readJson, readWorkspace, buildTarget, projectPaths, globalCss, resolveProject };
@@ -135,7 +135,7 @@ function migratePrimeng(options = {}) {
135
135
  if (used.size) {
136
136
  src = dropPrimeng(src, report.removed, new Set([...used].map((r) => r.primeng.slice(2))));
137
137
  const rows = [...used];
138
- for (const r of rows) src = addImport(src, r.className, r.package);
138
+ for (const r of rows) src = addImport(src, r.className, r.importFrom || r.package);
139
139
  src = addToComponentImports(src, [...new Set(rows.map((r) => r.className))]);
140
140
  }
141
141
  if (src !== before || used.size) {