@ng-native/tailwind 0.16.0 → 0.17.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.
Files changed (2) hide show
  1. package/config.cjs +75 -10
  2. package/package.json +3 -3
package/config.cjs CHANGED
@@ -37,13 +37,14 @@ const DEFAULT_OUTPUT = '.angular-native/app.tailwind.js';
37
37
 
38
38
  /**
39
39
  * @param {object} config a Metro config, after `withAngularNative`
40
- * @param {{ input: string, output?: string, watch?: boolean }} options
40
+ * @param {{ input: string, output?: string, watch?: boolean, browser?: string[] }} options
41
41
  * `input` is the app's Tailwind entry - the file with the `@import`s. `output` is the module the
42
42
  * app imports, and must be JavaScript: Expo's own transform worker claims every `.css` file
43
43
  * before ours is asked, and hands back an empty module on any platform but web, so a stylesheet
44
44
  * that arrived as CSS would silently contain nothing. `watch` is what makes a newly written
45
45
  * class appear without restarting the dev server; it defaults to on for a dev server and off
46
- * for a one-off build (see `watchesByDefault`).
46
+ * for a one-off build (see `watchesByDefault`). `browser` names the classes that components
47
+ * written for a browser sit under, where the app styles them from this sheet: see `asABrowsers`.
47
48
  */
48
49
  function withTailwind(config, options) {
49
50
  const projectRoot = config.projectRoot ?? process.cwd();
@@ -60,7 +61,7 @@ function withTailwind(config, options) {
60
61
 
61
62
  mkdirSync(path.dirname(output), { recursive: true });
62
63
  run([cliPath(projectRoot), '-i', input, '-o', css], projectRoot);
63
- generate(css, output, input);
64
+ generate(css, output, input, options.browser);
64
65
  // Without it TypeScript infers the sheet from its literal, which is not assignable to the type
65
66
  // `globalStyles` takes. `.js` -> `.d.ts`, `.mjs` -> `.d.mts`, `.cjs` -> `.d.cts`. A type import,
66
67
  // not an `import()` type, which typescript-eslint's `consistent-type-imports` forbids in an app.
@@ -68,7 +69,7 @@ function withTailwind(config, options) {
68
69
  output.replace(/\.([cm]?)js$/, '.d.$1ts'),
69
70
  "import type { StyleSheet } from '@ng-native/fabric';\n\ndeclare const sheet: StyleSheet;\nexport default sheet;\n",
70
71
  );
71
- if (options.watch ?? watchesByDefault()) watch(input, css, output, projectRoot);
72
+ if (options.watch ?? watchesByDefault()) watch(input, css, output, projectRoot, options.browser);
72
73
 
73
74
  return config;
74
75
  }
@@ -93,8 +94,68 @@ function watchesByDefault(argv = process.argv, env = process.env) {
93
94
  }
94
95
 
95
96
  /** The CLI's CSS, compiled into the module the app imports. */
96
- function generate(css, output, input) {
97
- writeFileSync(output, compileSheetModule(readFileSync(css, 'utf8'), css, { input, output }));
97
+ function generate(css, output, input, browser) {
98
+ const paths = { input, output };
99
+ writeFileSync(output, compileSheetModule(readFileSync(css, 'utf8'), css, paths, browser));
100
+ }
101
+
102
+ const FLEX = new Set(['flex', 'inline-flex']);
103
+ const names = (rule, prop) =>
104
+ prop in rule.declarations ||
105
+ prop in (rule.important ?? {}) ||
106
+ (rule.deferred ?? []).some((deferred) => deferred.props.includes(prop));
107
+
108
+ /**
109
+ * The classes of a `browser` option, without their dots: each is one class and nothing more.
110
+ * Nothing where the app gave none.
111
+ */
112
+ function scopeOf(browser) {
113
+ if (browser === undefined) return undefined;
114
+ const classes = Array.isArray(browser)
115
+ ? browser.map((each) => /^\.([\w-]+)$/.exec(each)?.[1])
116
+ : [];
117
+ if (!classes.length || classes.some((each) => each === undefined)) {
118
+ throw new Error(
119
+ '[angular-native] the Tailwind `browser` option is the classes that components written ' +
120
+ `for a browser sit under, such as [".ui"]; got ${JSON.stringify(browser)}.`,
121
+ );
122
+ }
123
+ return classes;
124
+ }
125
+
126
+ /**
127
+ * A sheet with its flex containers rows under the classes named, as the browser those
128
+ * components were written for lays them out. A library copied into the app, as Spartan UI's
129
+ * styled layer is, is styled by the app's own Tailwind sheet, where a flex container is a
130
+ * column: under an element with one of these classes it is a row unless a rule names a
131
+ * direction. Everywhere else in the app it stays a column.
132
+ *
133
+ * Each rule that makes a flex container and names no direction gets a rule ahead of it for the
134
+ * same elements under the scope, with no specificity at all, so a direction named by any rule
135
+ * still wins, as the initial value it stands in for would lose to any.
136
+ */
137
+ function asABrowsers(sheet, classes) {
138
+ if (!classes) return sheet;
139
+ const under = { classes: [], is: [classes.map((name) => ({ classes: [name] }))] };
140
+ const rules = [];
141
+ for (const rule of sheet.rules) {
142
+ const flex = FLEX.has(rule.declarations.display) || FLEX.has(rule.important?.display);
143
+ if (flex && !names(rule, 'flexDirection')) {
144
+ const { tokens: _tokens, deferred: _deferred, important: _important, ...matched } = rule;
145
+ // Under the scope wherever it is above the element: asked of the element itself, since
146
+ // the rule's own first compound may be the very box that carries the scope's class.
147
+ const subject = rule.compounds.at(-1);
148
+ const scoped = { ...subject, ancestors: [...(subject.ancestors ?? []), under] };
149
+ rules.push({
150
+ ...matched,
151
+ compounds: [...rule.compounds.slice(0, -1), scoped],
152
+ specificity: 0,
153
+ declarations: { flexDirection: 'row' },
154
+ });
155
+ }
156
+ rules.push(rule);
157
+ }
158
+ return { ...sheet, rules };
98
159
  }
99
160
 
100
161
  const warnedSettled = new Set();
@@ -149,7 +210,8 @@ function selectorAt(lines, index) {
149
210
  * `paths` is the entry and the module written from it. A relative `url()` in an `@font-face` is
150
211
  * relative to the entry, and the module lives elsewhere, so its `require` is re-pointed from there.
151
212
  */
152
- function compileSheetModule(css, context = 'tailwind', paths) {
213
+ function compileSheetModule(css, context = 'tailwind', paths, browser) {
214
+ const scope = scopeOf(browser);
153
215
  const dropped = [];
154
216
  const settled = new Set();
155
217
  const flat = flattenTailwind(css, { onSettled: (declaration) => settled.add(declaration) });
@@ -165,13 +227,16 @@ function compileSheetModule(css, context = 'tailwind', paths) {
165
227
  // A refusal names the rule's selector, which is what to search the app for, rather than a line
166
228
  // of this generated sheet, which moves as classes are added.
167
229
  const lines = flat.split('\n');
168
- const sheet = compileCss(flat, context, {
230
+ const compiled = compileCss(flat, context, {
231
+ // Read as a browser reads it, where the app named what the browser's components sit under.
232
+ generated: scope !== undefined,
169
233
  onUnsupported: (message) => dropped.push(message),
170
234
  locate: (line) => {
171
235
  const selector = selectorAt(lines, line - 1);
172
236
  return selector && `${selector} (Tailwind)`;
173
237
  },
174
238
  });
239
+ const sheet = asABrowsers(compiled, scope);
175
240
  for (const message of dropped) {
176
241
  // A leftover custom property was already substituted; anything else is a utility the app used
177
242
  // and native cannot express, which is worth saying while it is still being written.
@@ -282,7 +347,7 @@ function run(args, cwd) {
282
347
  * Metro watches the module, so a class written in a template reaches the device the way an edited
283
348
  * template does: the CLI notices the source, rewrites the CSS, this rewrites the module.
284
349
  */
285
- function watch(input, css, output, cwd) {
350
+ function watch(input, css, output, cwd, browser) {
286
351
  let stopping = false;
287
352
  const child = spawn(process.execPath, [cliPath(cwd), '-i', input, '-o', css, '--watch'], {
288
353
  cwd,
@@ -322,7 +387,7 @@ function watch(input, css, output, cwd) {
322
387
  // Off a file event, where a throw would take Metro down: the sheet on disk stays the last good
323
388
  // one, and the next change tries again.
324
389
  try {
325
- generate(css, output, input);
390
+ generate(css, output, input, browser);
326
391
  } catch (error) {
327
392
  console.error(`[angular-native] could not rebuild the Tailwind sheet: ${error.message}`);
328
393
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ng-native/tailwind",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Tailwind CSS preset for Angular Native: Tailwind 4 and Tailwind 3, on native and on the web.",
5
5
  "keywords": [
6
6
  "angular",
@@ -17,7 +17,7 @@
17
17
  "type": "commonjs",
18
18
  "main": "flatten.cjs",
19
19
  "dependencies": {
20
- "@ng-native/metro": "0.16.0",
20
+ "@ng-native/metro": "0.17.0",
21
21
  "lightningcss": "^1.30.2"
22
22
  },
23
23
  "nx": {
@@ -32,7 +32,7 @@
32
32
  "url": "https://github.com/ng-native/ng-native/issues"
33
33
  },
34
34
  "peerDependencies": {
35
- "@ng-native/metro": "0.16.0",
35
+ "@ng-native/metro": "0.17.0",
36
36
  "@tailwindcss/cli": "^4.3.3",
37
37
  "tailwindcss": "^3.4.1 || ^4.3.3"
38
38
  },