@ng-native/tailwind 0.1.2 → 0.2.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 +12 -1
- package/config.cjs +73 -10
- package/flatten.cjs +180 -15
- package/native.css +12 -7
- package/package.json +4 -3
- package/preset.cjs +29 -8
- package/preset.d.cts +10 -0
package/README.md
CHANGED
|
@@ -68,6 +68,17 @@ module.exports = {
|
|
|
68
68
|
`metro.config.js` and `index.ts` are the same as above: `withTailwind` sees Tailwind 3 and runs its
|
|
69
69
|
own CLI, so there is no `@tailwindcss/cli` to install. The preset turns preflight off.
|
|
70
70
|
|
|
71
|
+
Tailwind 3 adds its default configuration beneath every preset with no `presets` key, and a later
|
|
72
|
+
preset's defaults override an earlier preset's theme. Beside a preset of the app's own, list this
|
|
73
|
+
one after it with `presets: []`, so only the first preset brings the defaults:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
presets: [
|
|
77
|
+
require('./design-system.preset.js'),
|
|
78
|
+
{ ...require('@ng-native/tailwind/preset.cjs'), presets: [] },
|
|
79
|
+
],
|
|
80
|
+
```
|
|
81
|
+
|
|
71
82
|
## What's in the package
|
|
72
83
|
|
|
73
84
|
- `./native.css` - the preset: platform variants, safe-area and hairline utilities, and
|
|
@@ -84,7 +95,7 @@ own CLI, so there is no `@tailwindcss/cli` to install. The preset turns prefligh
|
|
|
84
95
|
- [Variants](https://ng-native.com/packages/tailwind/variants) and
|
|
85
96
|
[safe area and hairlines](https://ng-native.com/packages/tailwind/utilities)
|
|
86
97
|
- [Root README](https://github.com/ng-native/ng-native/blob/main/README.md) and
|
|
87
|
-
[ARCHITECTURE.md](https://github.com/ng-native/ng-native/blob/main/ARCHITECTURE.md)
|
|
98
|
+
[ARCHITECTURE.md](https://github.com/ng-native/ng-native/blob/main/docs/ARCHITECTURE.md)
|
|
88
99
|
|
|
89
100
|
## License
|
|
90
101
|
|
package/config.cjs
CHANGED
|
@@ -59,12 +59,13 @@ function withTailwind(config, options) {
|
|
|
59
59
|
|
|
60
60
|
mkdirSync(path.dirname(output), { recursive: true });
|
|
61
61
|
run([cliPath(projectRoot), '-i', input, '-o', css], projectRoot);
|
|
62
|
-
generate(css, output);
|
|
62
|
+
generate(css, output, input);
|
|
63
63
|
// Without it TypeScript infers the sheet from its literal, which is not assignable to the type
|
|
64
|
-
// `globalStyles` takes. `.js` -> `.d.ts`, `.mjs` -> `.d.mts`, `.cjs` -> `.d.cts`.
|
|
64
|
+
// `globalStyles` takes. `.js` -> `.d.ts`, `.mjs` -> `.d.mts`, `.cjs` -> `.d.cts`. A type import,
|
|
65
|
+
// not an `import()` type, which typescript-eslint's `consistent-type-imports` forbids in an app.
|
|
65
66
|
writeFileSync(
|
|
66
67
|
output.replace(/\.([cm]?)js$/, '.d.$1ts'),
|
|
67
|
-
"
|
|
68
|
+
"import type { StyleSheet } from '@ng-native/fabric';\n\ndeclare const sheet: StyleSheet;\nexport default sheet;\n",
|
|
68
69
|
);
|
|
69
70
|
if (options.watch ?? watchesByDefault()) watch(input, css, output, projectRoot);
|
|
70
71
|
|
|
@@ -91,19 +92,35 @@ function watchesByDefault(argv = process.argv, env = process.env) {
|
|
|
91
92
|
}
|
|
92
93
|
|
|
93
94
|
/** The CLI's CSS, compiled into the module the app imports. */
|
|
94
|
-
function generate(css, output) {
|
|
95
|
-
writeFileSync(output, compileSheetModule(readFileSync(css, 'utf8'), css));
|
|
95
|
+
function generate(css, output, input) {
|
|
96
|
+
writeFileSync(output, compileSheetModule(readFileSync(css, 'utf8'), css, { input, output }));
|
|
96
97
|
}
|
|
97
98
|
|
|
99
|
+
const warnedSettled = new Set();
|
|
100
|
+
|
|
98
101
|
/**
|
|
99
102
|
* Tailwind's CSS as a module exporting the compiled stylesheet.
|
|
100
103
|
*
|
|
101
104
|
* Exported because it is the whole of the build step worth testing: everything else here is
|
|
102
105
|
* process wrangling.
|
|
106
|
+
*
|
|
107
|
+
* `paths` is the entry and the module written from it. A relative `url()` in an `@font-face` is
|
|
108
|
+
* relative to the entry, and the module lives elsewhere, so its `require` is re-pointed from there.
|
|
103
109
|
*/
|
|
104
|
-
function compileSheetModule(css, context = 'tailwind') {
|
|
110
|
+
function compileSheetModule(css, context = 'tailwind', paths) {
|
|
105
111
|
const dropped = [];
|
|
106
|
-
const
|
|
112
|
+
const settled = new Set();
|
|
113
|
+
const flat = flattenTailwind(css, { onSettled: (declaration) => settled.add(declaration) });
|
|
114
|
+
for (const declaration of settled) {
|
|
115
|
+
// Once a process: the sheet is rebuilt on every class written, and the warning would be too.
|
|
116
|
+
if (warnedSettled.has(declaration)) continue;
|
|
117
|
+
warnedSettled.add(declaration);
|
|
118
|
+
console.warn(
|
|
119
|
+
`[angular-native] '${declaration}' keeps its theme value: native cannot read the token in ` +
|
|
120
|
+
`it on device, so it does not follow where the app sets it.`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const sheet = compileCss(flat, context, {
|
|
107
124
|
onUnsupported: (message) => dropped.push(message),
|
|
108
125
|
});
|
|
109
126
|
for (const message of dropped) {
|
|
@@ -111,7 +128,22 @@ function compileSheetModule(css, context = 'tailwind') {
|
|
|
111
128
|
// and native cannot express, which is worth saying while it is still being written.
|
|
112
129
|
if (!message.includes("dropped '--")) console.warn(`[angular-native] ${message}`);
|
|
113
130
|
}
|
|
114
|
-
|
|
131
|
+
for (const face of sheet.fonts ?? []) {
|
|
132
|
+
if (paths && /^\.\.?\//.test(face.source.asset)) {
|
|
133
|
+
const file = path.resolve(path.dirname(paths.input), face.source.asset);
|
|
134
|
+
const relative = path.relative(path.dirname(paths.output), file).split(path.sep).join('/');
|
|
135
|
+
face.source.asset = /^\.\.?\//.test(relative) ? relative : `./${relative}`;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// A font file is a module the bundler has to see, so the compiler's marker becomes an import:
|
|
139
|
+
// left as a path, the file is never bundled and the face is missing on the device. An import
|
|
140
|
+
// rather than a `require`, which an app's own lint rules would flag in a file it cannot edit.
|
|
141
|
+
const imports = [];
|
|
142
|
+
const literal = JSON.stringify(sheet).replace(/\{"asset":("(?:[^"\\]|\\.)*")\}/g, (_, file) => {
|
|
143
|
+
imports.push(`import font${imports.length} from ${file};\n`);
|
|
144
|
+
return `font${imports.length - 1}`;
|
|
145
|
+
});
|
|
146
|
+
return `// Generated from ${path.basename(context)}. Edit the Tailwind entry, not this.\n${imports.join('')}export default ${literal};\n`;
|
|
115
147
|
}
|
|
116
148
|
|
|
117
149
|
/**
|
|
@@ -148,6 +180,29 @@ function localImports(css, cwd) {
|
|
|
148
180
|
return found;
|
|
149
181
|
}
|
|
150
182
|
|
|
183
|
+
/**
|
|
184
|
+
* The entry's local imports, and theirs: every sheet of the app's own that Tailwind reads. Empty
|
|
185
|
+
* when the entry cannot be read, as mid-save, rather than throwing out of Metro.
|
|
186
|
+
*/
|
|
187
|
+
function themeFiles(input) {
|
|
188
|
+
const found = new Set();
|
|
189
|
+
const visit = (file) => {
|
|
190
|
+
let css;
|
|
191
|
+
try {
|
|
192
|
+
css = readFileSync(file, 'utf8');
|
|
193
|
+
} catch {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
for (const imported of localImports(css, path.dirname(file))) {
|
|
197
|
+
if (found.has(imported)) continue;
|
|
198
|
+
found.add(imported);
|
|
199
|
+
visit(imported);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
visit(input);
|
|
203
|
+
return [...found];
|
|
204
|
+
}
|
|
205
|
+
|
|
151
206
|
/** Build once, synchronously, so the module exists before Metro resolves the first import. */
|
|
152
207
|
function run(args, cwd) {
|
|
153
208
|
execFileSync(process.execPath, args, { cwd, stdio: 'pipe' });
|
|
@@ -195,11 +250,19 @@ function watch(input, css, output, cwd) {
|
|
|
195
250
|
);
|
|
196
251
|
});
|
|
197
252
|
|
|
198
|
-
watchFile(css, { interval: 200, persistent: false }, () =>
|
|
253
|
+
watchFile(css, { interval: 200, persistent: false }, () => {
|
|
254
|
+
// Off a file event, where a throw would take Metro down: the sheet on disk stays the last good
|
|
255
|
+
// one, and the next change tries again.
|
|
256
|
+
try {
|
|
257
|
+
generate(css, output, input);
|
|
258
|
+
} catch (error) {
|
|
259
|
+
console.error(`[angular-native] could not rebuild the Tailwind sheet: ${error.message}`);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
199
262
|
|
|
200
263
|
// A change inside an imported sheet is invisible to the CLI's cache. Touching the entry is what
|
|
201
264
|
// makes it read them all again.
|
|
202
|
-
for (const file of
|
|
265
|
+
for (const file of themeFiles(input)) {
|
|
203
266
|
watchFile(file, { interval: 200, persistent: false }, () => {
|
|
204
267
|
const now = new Date();
|
|
205
268
|
utimesSync(input, now, now);
|
package/flatten.cjs
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* if it does arrive, by dropping what it cannot express, but the warnings are noise nobody needs.
|
|
16
16
|
*/
|
|
17
17
|
const { transform, Features } = require('lightningcss');
|
|
18
|
-
const { markUnitless } = require('@ng-native/metro/css/compile.cjs');
|
|
18
|
+
const { compileCss, markUnitless } = require('@ng-native/metro/css/compile.cjs');
|
|
19
19
|
|
|
20
20
|
/** `@layer a, b;` - the statement that orders layers, which is meaningless once they are gone. */
|
|
21
21
|
const LAYER_STATEMENT = /@layer\s+[^;{]+;/g;
|
|
@@ -58,9 +58,7 @@ function withoutImportant(value) {
|
|
|
58
58
|
* The custom properties a sheet declares, and what they resolve to.
|
|
59
59
|
*
|
|
60
60
|
* Two sources: ordinary declarations, wherever they appear, and `@property`'s `initial-value`,
|
|
61
|
-
* which is how Tailwind gives `--tw-border-style` a default of `solid` on the web.
|
|
62
|
-
* static by the time this runs; a custom property that changes at runtime is the app's business
|
|
63
|
-
* and is left alone, because it is never one of these.
|
|
61
|
+
* which is how Tailwind gives `--tw-border-style` a default of `solid` on the web.
|
|
64
62
|
*/
|
|
65
63
|
function collectVariables(css) {
|
|
66
64
|
const values = new Map();
|
|
@@ -79,15 +77,57 @@ function collectVariables(css) {
|
|
|
79
77
|
// Substituting picks whichever was written last and paints the dark palette in daylight, so
|
|
80
78
|
// these are left for the engine, which resolves `var()` per node against the tokens in scope.
|
|
81
79
|
//
|
|
80
|
+
// So is one declared only where a class, a platform or a media query says: `--brand` only under
|
|
81
|
+
// `.dark` has no value in light mode, and substituting its one value paints it there anyway.
|
|
82
|
+
//
|
|
82
83
|
// Tailwind's own `--tw-*` plumbing is exempt. Those are set by one utility and read by another
|
|
83
84
|
// on the same node, and every one of them also has a reset value, so they all look themed and
|
|
84
85
|
// none of them is. They stay substituted, which is what they have always been.
|
|
86
|
+
const everywhere = globalVariables(css);
|
|
87
|
+
for (const name of values.keys()) {
|
|
88
|
+
if (!everywhere.has(name)) conflicting.add(name);
|
|
89
|
+
}
|
|
85
90
|
for (const name of conflicting) {
|
|
86
|
-
if (!name.startsWith('--tw-')) values.
|
|
91
|
+
if (!name.startsWith('--tw-')) values.set(name, THEMED);
|
|
87
92
|
}
|
|
88
93
|
return values;
|
|
89
94
|
}
|
|
90
95
|
|
|
96
|
+
/** A selector that applies to the root whatever it wears: `:root`, `:host`, `html` or `*`. */
|
|
97
|
+
const GLOBAL_SELECTOR = /^(:root|:host|html|\*)$/;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The custom properties a sheet declares on the root unconditionally, outside any at-rule, and
|
|
101
|
+
* those with an `@property` initial value.
|
|
102
|
+
*/
|
|
103
|
+
function globalVariables(css) {
|
|
104
|
+
const names = new Set();
|
|
105
|
+
let depth = 0;
|
|
106
|
+
let last = 0;
|
|
107
|
+
for (const match of css.matchAll(/([^{}]*)\{([^{}]*)\}/g)) {
|
|
108
|
+
for (const c of css.slice(last, match.index)) {
|
|
109
|
+
if (c === '{') depth++;
|
|
110
|
+
else if (c === '}') depth--;
|
|
111
|
+
}
|
|
112
|
+
last = match.index + match[0].length;
|
|
113
|
+
const [, commented, body] = match;
|
|
114
|
+
const prelude = commented.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
115
|
+
const property = /@property\s+(--[\w-]+)/.exec(prelude);
|
|
116
|
+
if (property && /initial-value\s*:/.test(body)) names.add(property[1]);
|
|
117
|
+
if (depth !== 0 || !selectorList(prelude).some((one) => GLOBAL_SELECTOR.test(one.trim()))) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
for (const [, name] of body.matchAll(/(--[\w-]+)\s*:/g)) names.add(name);
|
|
121
|
+
}
|
|
122
|
+
return names;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* What `collectVariables` holds for a themed property: known to be declared, so a `var()` of it is
|
|
127
|
+
* left whole, fallback and all, rather than collapsed to the fallback as an undeclared one is.
|
|
128
|
+
*/
|
|
129
|
+
const THEMED = Symbol('themed');
|
|
130
|
+
|
|
91
131
|
/**
|
|
92
132
|
* Find the next `var(` and return the whole call, its name and its fallback.
|
|
93
133
|
*
|
|
@@ -275,6 +315,10 @@ function substituteVariables(css, values, runtime) {
|
|
|
275
315
|
// "nobody asked for a shadow colour" - and substituting the word itself produced a shadow
|
|
276
316
|
// painted the colour `initial`, which is to say no shadow at all.
|
|
277
317
|
const declared = values.get(found.name);
|
|
318
|
+
if (declared === THEMED) {
|
|
319
|
+
from = found.end;
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
278
322
|
const value = declared === undefined || declared === 'initial' ? found.fallback : declared;
|
|
279
323
|
if (value === undefined || selfReferential(value, found.name)) {
|
|
280
324
|
from = found.end;
|
|
@@ -479,14 +523,23 @@ function resolveUnitlessLineHeight(css) {
|
|
|
479
523
|
if (!ratio) return whole;
|
|
480
524
|
const size = /(^|;)\s*font-size\s*:\s*([0-9]*\.?[0-9]+)(rem|px)\s*(?=;|$)/.exec(body);
|
|
481
525
|
if (!size) return whole;
|
|
482
|
-
|
|
483
|
-
|
|
526
|
+
// In points, rounded there: a ratio rounded at build time and then multiplied by 16 again,
|
|
527
|
+
// as a height in rem is, came out at 31.99984 where a browser draws 32.
|
|
528
|
+
const points = Number(size[2]) * Number(ratio[2]) * (size[3] === 'rem' ? REM : 1);
|
|
529
|
+
return whole.replace(
|
|
530
|
+
ratio[0],
|
|
531
|
+
`${ratio[1]} line-height: ${Math.round(points * 1000) / 1000}px`,
|
|
532
|
+
);
|
|
484
533
|
});
|
|
485
534
|
}
|
|
486
535
|
|
|
487
|
-
/**
|
|
536
|
+
/**
|
|
537
|
+
* Five decimal places, as the compiler keeps. Three is finer than a point on any screen, but not
|
|
538
|
+
* for a line height that is a factor a font size multiplies back up: `calc(1.25 / .875)` at three
|
|
539
|
+
* made `text-sm` 20.006 points tall rather than 20.
|
|
540
|
+
*/
|
|
488
541
|
function round(value) {
|
|
489
|
-
return Math.round(value *
|
|
542
|
+
return Math.round(value * 1e5) / 1e5;
|
|
490
543
|
}
|
|
491
544
|
|
|
492
545
|
/**
|
|
@@ -613,6 +666,9 @@ function normalizeV3Gradients(css) {
|
|
|
613
666
|
const OPACITY_COLOUR =
|
|
614
667
|
/([\w-]+)\s*:\s*(rgb|hsl)a?\(\s*([\d.]+)(?:deg)?[\s,]+([\d.]+)%?[\s,]+([\d.]+)%?\s*\/\s*var\(\s*(--tw-[\w-]+-opacity)\s*,\s*1\s*\)\s*\)/g;
|
|
615
668
|
|
|
669
|
+
const TOKEN_CHANNELS_OPACITY =
|
|
670
|
+
/(?:rgb|hsl)a?\(\s*var\(\s*--[\w-]+\s*\)\s*\/\s*var\(\s*(--tw-[\w-]+-opacity)\s*,\s*1\s*\)\s*\)/g;
|
|
671
|
+
|
|
616
672
|
/**
|
|
617
673
|
* An `hsl()` colour's channels in sRGB, as a browser gives them when it prints the colour: whole
|
|
618
674
|
* numbers, since the channels token that carries them is read as an `rgb()`.
|
|
@@ -650,6 +706,11 @@ function opacityChannels(css) {
|
|
|
650
706
|
for (const [, name] of body.matchAll(/(--tw-[\w-]+-opacity)\s*:/g)) setters.add(name);
|
|
651
707
|
}
|
|
652
708
|
const runtime = new Set();
|
|
709
|
+
// Channels from a token, `hsl(var(--primary) / <alpha-value>)`: the alpha stays for the engine,
|
|
710
|
+
// and the channels are rewritten on the pass after substitution, or read on device if live.
|
|
711
|
+
for (const [, alpha] of css.matchAll(TOKEN_CHANNELS_OPACITY)) {
|
|
712
|
+
if (setters.has(alpha)) runtime.add(alpha);
|
|
713
|
+
}
|
|
653
714
|
const out = css.replace(OPACITY_COLOUR, (whole, property, space, a, b2, c, alpha) => {
|
|
654
715
|
if (!setters.has(alpha)) return whole;
|
|
655
716
|
const [r, g, b] = space === 'hsl' ? hslChannels(+a, +b2, +c) : [a, b2, c];
|
|
@@ -702,6 +763,27 @@ const STACKED_PLAIN_ONE = new RegExp(
|
|
|
702
763
|
);
|
|
703
764
|
const STACKED_PLAIN = new RegExp(String.raw`(^|,)\s*${ANCESTOR}\s+${ANCESTOR}\s`);
|
|
704
765
|
|
|
766
|
+
/** Where the Tailwind 3 preset records an app's `prefix`: `--ng-native-tailwind-prefix: "tw-"`. */
|
|
767
|
+
const PREFIX_RECORD = /--ng-native-tailwind-prefix\s*:\s*"((?:\\.|[^"\\])*)"\s*;?/;
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* The preset's ancestor classes with an app's Tailwind 3 `prefix` taken back off.
|
|
771
|
+
*
|
|
772
|
+
* Tailwind 3 prefixes every class in a variant's selector, so `ios:` and `dark:` come out as
|
|
773
|
+
* `.tw-platform-ios .x` and `.tw-dark .x`, and the root wears `platform-ios` and `dark`. Only
|
|
774
|
+
* those classes, only in a sheet the preset recorded a prefix in: `.tw-group` is the app's own.
|
|
775
|
+
*/
|
|
776
|
+
function unprefixAncestors(css) {
|
|
777
|
+
const recorded = PREFIX_RECORD.exec(css);
|
|
778
|
+
if (!recorded) return css;
|
|
779
|
+
const prefix = JSON.parse(`"${recorded[1]}"`).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
780
|
+
const prefixed = new RegExp(
|
|
781
|
+
String.raw`\.${prefix}(dark|platform-(?:ios|android|web))(?![\w-]|\\)`,
|
|
782
|
+
'g',
|
|
783
|
+
);
|
|
784
|
+
return css.replace(PREFIX_RECORD, '').replace(prefixed, '.$1');
|
|
785
|
+
}
|
|
786
|
+
|
|
705
787
|
/**
|
|
706
788
|
* A stacked variant, rewritten as the ancestor tests it means.
|
|
707
789
|
*
|
|
@@ -769,10 +851,76 @@ function resetWithoutReverseSlots(css) {
|
|
|
769
851
|
);
|
|
770
852
|
}
|
|
771
853
|
|
|
772
|
-
|
|
854
|
+
/**
|
|
855
|
+
* A declaration that reads a theme token, where the compiler cannot defer it, with the token's theme
|
|
856
|
+
* value in its place: `drop-shadow(var(--drop-shadow-xs))`, or a token defined as
|
|
857
|
+
* `calc(var(--spacing) * 24)`. Left live, it would be dropped and the utility would draw nothing;
|
|
858
|
+
* settled, it draws at its theme value, and only that declaration stops following the override.
|
|
859
|
+
*/
|
|
860
|
+
function settleWhereRefused(css, theme, settled, runtime, onSettled) {
|
|
861
|
+
const names = [...theme].filter((name) => settled.has(name));
|
|
862
|
+
if (names.length === 0) return css;
|
|
863
|
+
const reads = new RegExp(`var\\(\\s*(${names.join('|')})\\s*[,)]`);
|
|
864
|
+
return css.replace(/([^{}]*)\{([^{}]*)\}/g, (_, selector, body) => {
|
|
865
|
+
// In its own rule's selector, which can decide what the compiler takes: a platform's.
|
|
866
|
+
const where = /^\s*(from|to|[\d.]+%)\s*$/i.test(selector) ? 'a' : selector;
|
|
867
|
+
const declarations = declarationsOf(body).map((declaration) => {
|
|
868
|
+
if (!reads.test(declaration) || compiles(where, declaration)) return declaration;
|
|
869
|
+
const settledDeclaration = foldSimpleCalc(substituteVariables(declaration, settled, runtime));
|
|
870
|
+
// Refused settled as well, it is a property the compiler reports on its own.
|
|
871
|
+
if (compiles(where, settledDeclaration)) onSettled(declaration.trim());
|
|
872
|
+
return settledDeclaration;
|
|
873
|
+
});
|
|
874
|
+
return `${selector}{${declarations.join(';')}}`;
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
/** A block's declarations, split on the semicolons between them and not those in a string. */
|
|
879
|
+
function declarationsOf(body) {
|
|
880
|
+
const declarations = [];
|
|
881
|
+
let depth = 0;
|
|
882
|
+
let quote = null;
|
|
883
|
+
let start = 0;
|
|
884
|
+
for (let i = 0; i < body.length; i++) {
|
|
885
|
+
const c = body[i];
|
|
886
|
+
if (quote) {
|
|
887
|
+
if (c === '\\') i++;
|
|
888
|
+
else if (c === quote) quote = null;
|
|
889
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
890
|
+
else if (c === '(') depth++;
|
|
891
|
+
else if (c === ')') depth--;
|
|
892
|
+
else if (c === ';' && depth === 0) {
|
|
893
|
+
declarations.push(body.slice(start, i));
|
|
894
|
+
start = i + 1;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
declarations.push(body.slice(start));
|
|
898
|
+
return declarations;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/** Whether the compiler takes one declaration as written, in a rule of `selector`. */
|
|
902
|
+
function compiles(selector, declaration) {
|
|
903
|
+
let taken = true;
|
|
904
|
+
try {
|
|
905
|
+
compileCss(`${selector}{${declaration}}`, 'live', { onUnsupported: () => (taken = false) });
|
|
906
|
+
} catch {
|
|
907
|
+
taken = false;
|
|
908
|
+
}
|
|
909
|
+
return taken;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* @param {string} css the Tailwind CLI's output
|
|
914
|
+
* @param {{ onSettled?: (declaration: string) => void }} options `onSettled` hears of each
|
|
915
|
+
* declaration that reads a theme token and was given the token's value anyway, the compiler not
|
|
916
|
+
* taking it as written
|
|
917
|
+
*/
|
|
918
|
+
function flattenTailwind(css, { onSettled = () => {} } = {}) {
|
|
773
919
|
// Before any pass of lightningcss here, which reads `m-[3]`'s bare 3 as 3px and hides it from
|
|
774
920
|
// the compiler: see `markUnitless`.
|
|
775
|
-
let out = childrenOfWhere(
|
|
921
|
+
let out = childrenOfWhere(
|
|
922
|
+
markUnitlessReverse(markUnitless(unprefixAncestors(css))).replace(LAYER_STATEMENT, ''),
|
|
923
|
+
);
|
|
776
924
|
out = rewriteAtRule(out, '@layer', true);
|
|
777
925
|
out = rewriteAtRule(out, '@supports', true);
|
|
778
926
|
out = resetWithoutReverseSlots(out);
|
|
@@ -794,7 +942,13 @@ function flattenTailwind(css) {
|
|
|
794
942
|
out = out.replace(/(^|[;{])\s*-(?:moz|ms|o)-[\w-]+\s*:[^;{}]*;?/gm, '$1');
|
|
795
943
|
const opacity = opacityChannels(out);
|
|
796
944
|
out = opacity.css;
|
|
797
|
-
|
|
945
|
+
// The theme is left for the engine, which resolves `var()` per node against the tokens in scope,
|
|
946
|
+
// so an element that sets `--color-brand` recolours what is inside it, as on the web. Only
|
|
947
|
+
// Tailwind's own `--tw-*` plumbing is substituted: set by one utility and read by another on the
|
|
948
|
+
// same node, it is no one's to set from outside.
|
|
949
|
+
const settled = collectVariables(out);
|
|
950
|
+
const theme = new Set([...settled.keys()].filter((name) => !name.startsWith('--tw-')));
|
|
951
|
+
const values = new Map([...settled].filter(([name]) => !theme.has(name)));
|
|
798
952
|
const runtime = new Set([...crossRuleVariables(out), ...opacity.runtime]);
|
|
799
953
|
out = rewriteAtRule(out, '@property', false);
|
|
800
954
|
out = expandGradients(out, values);
|
|
@@ -817,14 +971,25 @@ function flattenTailwind(css) {
|
|
|
817
971
|
out = out.replace(/--tw-gradient-via\s*:\s*(#0000|transparent|initial)\s*;?/g, '');
|
|
818
972
|
// Rule-local first: a property a rule declares and reads is that rule's business, and the
|
|
819
973
|
// file-wide map holds the `*` reset that would otherwise win.
|
|
820
|
-
|
|
821
|
-
|
|
974
|
+
// A theme token is left exactly as written, as a runtime one is: not in `values`, a var() of it
|
|
975
|
+
// with a fallback would otherwise be substituted by the fallback.
|
|
976
|
+
const kept = new Set([...runtime, ...theme]);
|
|
977
|
+
out = substituteInRules(out, values, kept);
|
|
978
|
+
out = substituteVariables(out, values, kept);
|
|
979
|
+
// Again, for the colours whose channels were a token until now.
|
|
980
|
+
out = opacityChannels(out).css;
|
|
822
981
|
out = dropUnreadSlots(out);
|
|
823
982
|
out = dropPseudoElementRules(out);
|
|
824
983
|
out = expandStackedVariants(out);
|
|
825
984
|
out = dropRedundantBorderStyles(out);
|
|
826
985
|
out = dropEmptyDeclarations(out);
|
|
827
|
-
return
|
|
986
|
+
return settleWhereRefused(
|
|
987
|
+
resolveUnitlessLineHeight(fold(out)),
|
|
988
|
+
theme,
|
|
989
|
+
settled,
|
|
990
|
+
runtime,
|
|
991
|
+
onSettled,
|
|
992
|
+
);
|
|
828
993
|
}
|
|
829
994
|
|
|
830
995
|
module.exports = { flattenTailwind };
|
package/native.css
CHANGED
|
@@ -66,19 +66,24 @@
|
|
|
66
66
|
* silent failure in a new shape.
|
|
67
67
|
*
|
|
68
68
|
* The two platform rules then upgrade it to the font each platform actually uses for code -
|
|
69
|
-
* Menlo on iOS, and Android's own `monospace` alias, which maps to Roboto Mono. They
|
|
70
|
-
*
|
|
69
|
+
* Menlo on iOS, and Android's own `monospace` alias, which maps to Roboto Mono. They set a custom
|
|
70
|
+
* property on the root rather than `font-family` on `.font-mono`, so an app's own
|
|
71
|
+
* `@theme { --font-mono: ... }` replaces the whole chain and keeps its font on every platform, and
|
|
72
|
+
* a `prefix()` on Tailwind's imports leaves them working.
|
|
71
73
|
*
|
|
72
74
|
* The web keeps Tailwind's full stack: `web.css` does not import this, and a browser resolves
|
|
73
75
|
* `ui-monospace` correctly on its own.
|
|
74
76
|
*/
|
|
75
77
|
@theme {
|
|
76
|
-
--font-mono:
|
|
78
|
+
--font-mono: var(--platform-font-mono);
|
|
77
79
|
}
|
|
78
80
|
|
|
79
|
-
|
|
80
|
-
font-
|
|
81
|
+
:root {
|
|
82
|
+
--platform-font-mono: 'Courier New';
|
|
81
83
|
}
|
|
82
|
-
.platform-
|
|
83
|
-
font-
|
|
84
|
+
.platform-ios {
|
|
85
|
+
--platform-font-mono: Menlo;
|
|
86
|
+
}
|
|
87
|
+
.platform-android {
|
|
88
|
+
--platform-font-mono: monospace;
|
|
84
89
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ng-native/tailwind",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Tailwind CSS preset for Angular Native: Tailwind 4 on native and web, Tailwind 3 on native.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"angular",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"main": "flatten.cjs",
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"lightningcss": "^1.30.2",
|
|
21
|
-
"@ng-native/metro": "0.
|
|
21
|
+
"@ng-native/metro": "0.2.0"
|
|
22
22
|
},
|
|
23
23
|
"nx": {
|
|
24
24
|
"tags": [
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"@tailwindcss/cli": "^4.3.3",
|
|
36
36
|
"tailwindcss": "^3.4.1 || ^4.3.3",
|
|
37
|
-
"@ng-native/metro": "0.
|
|
37
|
+
"@ng-native/metro": "0.2.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependenciesMeta": {
|
|
40
40
|
"@tailwindcss/cli": {
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
},
|
|
49
49
|
"files": [
|
|
50
50
|
"*.cjs",
|
|
51
|
+
"*.d.cts",
|
|
51
52
|
"*.css"
|
|
52
53
|
],
|
|
53
54
|
"publishConfig": {
|
package/preset.cjs
CHANGED
|
@@ -7,6 +7,11 @@
|
|
|
7
7
|
* content: ['./src/**\/*.{ts,html}'],
|
|
8
8
|
* };
|
|
9
9
|
*
|
|
10
|
+
* There is no `presets` key here, so Tailwind 3 adds its default configuration beneath this preset,
|
|
11
|
+
* which is what an app using it alone needs. Beside another preset that also brings the defaults,
|
|
12
|
+
* the later one's defaults override the earlier one's theme, so an app lists this one after its own
|
|
13
|
+
* preset as `{ ...require('@ng-native/tailwind/preset.cjs'), presets: [] }`.
|
|
14
|
+
*
|
|
10
15
|
* The same vocabulary as `native.css`, which is the Tailwind 4 preset, in the JavaScript form
|
|
11
16
|
* Tailwind 3 takes: read the two side by side, and change them together. Every choice is explained
|
|
12
17
|
* there and in `shared.css`; this file only says how each one is spelt here.
|
|
@@ -23,7 +28,13 @@ const inset = (edge) => `var(--safe-area-inset-${edge}, 0px)`;
|
|
|
23
28
|
|
|
24
29
|
const HAIRLINE = 'var(--hairline, 1px)';
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
/** The first family a `fontFamily` theme value names, in any of the forms Tailwind 3 takes. */
|
|
32
|
+
const firstFamily = (value) =>
|
|
33
|
+
String([value].flat(2)[0] ?? '')
|
|
34
|
+
.split(',')[0]
|
|
35
|
+
.trim();
|
|
36
|
+
|
|
37
|
+
function nativePreset({ addBase, addUtilities, addVariant, config, matchUtilities, theme }) {
|
|
27
38
|
// `hover:` is the pressed state, or a real hover where there is a pointer.
|
|
28
39
|
addVariant('hover', ['&:active', '&[data-hover]']);
|
|
29
40
|
addVariant('press', '&:active');
|
|
@@ -57,6 +68,12 @@ function nativePreset({ addUtilities, addVariant, matchUtilities, theme }) {
|
|
|
57
68
|
addVariant('android', '.platform-android &');
|
|
58
69
|
addVariant('web', '.platform-web &');
|
|
59
70
|
addVariant('native', ['.platform-ios &', '.platform-android &']);
|
|
71
|
+
// Tailwind 3 puts an app's `prefix` on every class in these, `.tw-platform-ios &` and `.tw-dark &`,
|
|
72
|
+
// and nothing sets those on the root. The prefix is recorded for `flattenTailwind`, which takes
|
|
73
|
+
// it back off the root classes.
|
|
74
|
+
if (config('prefix')) {
|
|
75
|
+
addBase({ ':root': { '--ng-native-tailwind-prefix': JSON.stringify(config('prefix')) } });
|
|
76
|
+
}
|
|
60
77
|
|
|
61
78
|
const safe = {
|
|
62
79
|
'.p-safe': Object.fromEntries(
|
|
@@ -104,12 +121,17 @@ function nativePreset({ addUtilities, addVariant, matchUtilities, theme }) {
|
|
|
104
121
|
}
|
|
105
122
|
addUtilities(hairlines);
|
|
106
123
|
|
|
107
|
-
// `font-mono`,
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
124
|
+
// `font-mono`, when it is still Tailwind's own stack, whose first family, `ui-monospace`, is on
|
|
125
|
+
// neither platform: `Courier New` on both, upgraded to the font each platform uses for code. An
|
|
126
|
+
// app that names its own monospace font keeps it. Utilities rather than base rules, so an app's
|
|
127
|
+
// `important: true` makes them important too, and the platform rules still win.
|
|
128
|
+
if (firstFamily(theme('fontFamily.mono')) === 'ui-monospace') {
|
|
129
|
+
addUtilities({
|
|
130
|
+
'.font-mono': { 'font-family': 'Courier New' },
|
|
131
|
+
'.platform-ios .font-mono': { 'font-family': 'Menlo' },
|
|
132
|
+
'.platform-android .font-mono': { 'font-family': 'monospace' },
|
|
133
|
+
});
|
|
134
|
+
}
|
|
113
135
|
}
|
|
114
136
|
|
|
115
137
|
module.exports = {
|
|
@@ -117,6 +139,5 @@ module.exports = {
|
|
|
117
139
|
darkMode: ['variant', '.dark &'],
|
|
118
140
|
// A browser reset in terms of `html` and `::before`, none of which means anything on a phone.
|
|
119
141
|
corePlugins: { preflight: false },
|
|
120
|
-
theme: { extend: { fontFamily: { mono: ['Courier New'] } } },
|
|
121
142
|
plugins: [nativePreset],
|
|
122
143
|
};
|
package/preset.d.cts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The native preset for Tailwind 3, for a `tailwind.config.ts`. Spelt out rather than typed as
|
|
3
|
+
* `Config` from `tailwindcss`, so it holds whichever major of Tailwind the import resolves to.
|
|
4
|
+
*/
|
|
5
|
+
declare const preset: {
|
|
6
|
+
darkMode: ['variant', string];
|
|
7
|
+
corePlugins: { preflight: false };
|
|
8
|
+
plugins: ((api: unknown) => void)[];
|
|
9
|
+
};
|
|
10
|
+
export = preset;
|