@gjsify/nativescript-vite 0.64.15-next.287 → 0.64.15

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.
@@ -0,0 +1,17 @@
1
+ import type { UserConfig } from 'vite';
2
+ /**
3
+ * Add `postcss-nesting` and the `px` rewrite after the app's own PostCSS plugins.
4
+ *
5
+ * - `css.postcss` unset: the app's `postcss.config.*` (searched upward from `root`, like Vite)
6
+ * is loaded and kept, ours follow it.
7
+ * - `css.postcss` a path: that directory is searched the same way.
8
+ * - `css.postcss` inline: its plugins stay, ours are appended.
9
+ *
10
+ * Idempotent: a plugin that is already there (an app that runs `postcss-nesting` itself) is not
11
+ * added a second time, and the config is returned as it came when nothing is missing.
12
+ *
13
+ * @param config the composed config, user layer included.
14
+ * @param root the app root; `config.root` wins, then this, then the working directory.
15
+ * @param mode the Vite mode, handed to a config FUNCTION the way `postcss-load-config` does.
16
+ */
17
+ export declare function nativescriptCssNesting(config: UserConfig, root?: string, mode?: string): Promise<UserConfig>;
@@ -0,0 +1,177 @@
1
+ // CSS nesting for NativeScript apps.
2
+ //
3
+ // GTK 4 reads `#main-button { revealer { padding-bottom: 16px; } }` as `#main-button revealer
4
+ // { … }`, and a GTK app's stylesheet is written that way (Learn6502's `main-button.css`).
5
+ // NativeScript's own CSS parser does not: its `css-tree` transform files a nested rule under
6
+ // the parent's `declarations`, so the parent keeps no real declaration and the inner rule
7
+ // never applies. Measured on a Galaxy S9: the app's floating action button kept libadwaita's
8
+ // 34 dp default padding instead of the authored 12 px.
9
+ //
10
+ // `@nativescript/vite` feeds `app.css` through Vite's `preprocessCSS()` and parses the result,
11
+ // so the one seam that runs before NativeScript's parser is the PostCSS stage. This module
12
+ // appends `postcss-nesting` (the CSS Nesting spec's reference plugin, the same semantics GTK
13
+ // implements) to the PostCSS config the app already has. `css.postcss` given inline replaces
14
+ // Vite's config-file lookup, so the app's own `postcss.config.*` is loaded here and kept first.
15
+ import { existsSync, readFileSync } from 'node:fs';
16
+ import { createRequire } from 'node:module';
17
+ import { dirname, join, resolve } from 'node:path';
18
+ import { pathToFileURL } from 'node:url';
19
+ import { cssPxToDip } from '@gjsify/adwaita-core/css-length';
20
+ import postcssNesting from 'postcss-nesting';
21
+ import selectorParser from 'postcss-selector-parser';
22
+ const CONFIG_FILES = [
23
+ 'postcss.config.js',
24
+ 'postcss.config.cjs',
25
+ 'postcss.config.mjs',
26
+ '.postcssrc.json',
27
+ '.postcssrc.js',
28
+ '.postcssrc.cjs',
29
+ '.postcssrc.mjs',
30
+ '.postcssrc',
31
+ ];
32
+ /** The name `postcss-nesting` registers; an app that already runs it is not given a second pass. */
33
+ const NESTING_PLUGIN_NAME = 'postcss-nesting';
34
+ const PX_PLUGIN_NAME = 'gjsify-ns-px-to-dip';
35
+ /**
36
+ * `postcss-nesting` writes a nested rule under a complex parent as `:is(#main-button revealer)
37
+ * button` — the spec's specificity-preserving form, and the only one its current edition has
38
+ * (the `noIsPseudoSelector` option belongs to the 2021 edition, which does not accept GTK's
39
+ * bare `button { … }` nested rule). NativeScript's selector engine has no `:is()`, so the rule
40
+ * would be dropped as silently as the nesting was. A LEADING `:is()` followed by a combinator
41
+ * (or alone) is the parent itself, so it is unwrapped: `:is(A, B) c` becomes `A c, B c`, which
42
+ * is what GTK reads. An `:is()` anywhere else is left as authored.
43
+ */
44
+ function unwrapIs(selector) {
45
+ return selectorParser((selectors) => {
46
+ let changed = true;
47
+ while (changed) {
48
+ changed = false;
49
+ selectors.each((sel) => {
50
+ const head = sel.first;
51
+ if (head?.type !== 'pseudo' || head.value.toLowerCase() !== ':is')
52
+ return;
53
+ const next = sel.at(1);
54
+ if (next !== undefined && next.type !== 'combinator')
55
+ return;
56
+ for (const arg of head.nodes) {
57
+ const copy = sel.clone();
58
+ copy.first.replaceWith(...arg.clone().nodes);
59
+ sel.parent?.insertBefore(sel, copy);
60
+ }
61
+ sel.remove();
62
+ changed = true;
63
+ });
64
+ }
65
+ }).processSync(selector);
66
+ }
67
+ const unwrapLeadingIs = {
68
+ postcssPlugin: 'gjsify-ns-unwrap-is',
69
+ OnceExit(root) {
70
+ root.walkRules((rule) => {
71
+ if (rule.selector.includes(':is('))
72
+ rule.selector = unwrapIs(rule.selector);
73
+ });
74
+ },
75
+ };
76
+ /**
77
+ * GTK's `px` is a logical pixel, NativeScript's a physical one (see `cssPxToDip`): a declaration
78
+ * value is rewritten to the unitless device-independent length. Declarations only, so a media
79
+ * query's `px` and a selector are left as authored.
80
+ */
81
+ const pxToDip = {
82
+ postcssPlugin: PX_PLUGIN_NAME,
83
+ OnceExit(root) {
84
+ root.walkDecls((decl) => {
85
+ decl.value = cssPxToDip(decl.value);
86
+ });
87
+ },
88
+ };
89
+ function findConfigFile(from) {
90
+ let dir = resolve(from);
91
+ for (;;) {
92
+ for (const name of CONFIG_FILES) {
93
+ const candidate = join(dir, name);
94
+ if (existsSync(candidate))
95
+ return candidate;
96
+ }
97
+ const parent = dirname(dir);
98
+ if (parent === dir)
99
+ return undefined;
100
+ dir = parent;
101
+ }
102
+ }
103
+ /** `plugins: { name: options }` — the object form `postcss-load-config` accepts. */
104
+ function pluginsFromMap(map, file) {
105
+ const requireFrom = createRequire(file);
106
+ const plugins = [];
107
+ for (const [name, options] of Object.entries(map)) {
108
+ if (options === false)
109
+ continue;
110
+ const loaded = requireFrom(name);
111
+ const factory = (loaded?.default ?? loaded);
112
+ plugins.push(typeof factory === 'function' && options !== undefined
113
+ ? factory(options)
114
+ : factory);
115
+ }
116
+ return plugins;
117
+ }
118
+ async function loadConfigFile(file, mode) {
119
+ if (file.endsWith('.json') || file.endsWith('.postcssrc')) {
120
+ const parsed = JSON.parse(readFileSync(file, 'utf8'));
121
+ return normalise(parsed, file);
122
+ }
123
+ const imported = (await import(pathToFileURL(file).href));
124
+ let exported = imported.default ?? imported;
125
+ if (typeof exported === 'function')
126
+ exported = await exported({ env: mode, file, options: {} });
127
+ return normalise((exported ?? {}), file);
128
+ }
129
+ function normalise(config, file) {
130
+ const { plugins, ...options } = config;
131
+ const list = Array.isArray(plugins)
132
+ ? plugins
133
+ : plugins && typeof plugins === 'object'
134
+ ? pluginsFromMap(plugins, file)
135
+ : [];
136
+ return { ...options, plugins: list };
137
+ }
138
+ function hasPlugin(plugins, name) {
139
+ return plugins.some((plugin) => plugin?.postcssPlugin === name);
140
+ }
141
+ /**
142
+ * Add `postcss-nesting` and the `px` rewrite after the app's own PostCSS plugins.
143
+ *
144
+ * - `css.postcss` unset: the app's `postcss.config.*` (searched upward from `root`, like Vite)
145
+ * is loaded and kept, ours follow it.
146
+ * - `css.postcss` a path: that directory is searched the same way.
147
+ * - `css.postcss` inline: its plugins stay, ours are appended.
148
+ *
149
+ * Idempotent: a plugin that is already there (an app that runs `postcss-nesting` itself) is not
150
+ * added a second time, and the config is returned as it came when nothing is missing.
151
+ *
152
+ * @param config the composed config, user layer included.
153
+ * @param root the app root; `config.root` wins, then this, then the working directory.
154
+ * @param mode the Vite mode, handed to a config FUNCTION the way `postcss-load-config` does.
155
+ */
156
+ export async function nativescriptCssNesting(config, root, mode = 'production') {
157
+ const appRoot = resolve(config.root ?? root ?? process.cwd());
158
+ const declared = config.css?.postcss;
159
+ let base;
160
+ if (declared !== undefined && typeof declared !== 'string') {
161
+ base = declared;
162
+ }
163
+ else {
164
+ const searchFrom = typeof declared === 'string' ? resolve(appRoot, declared) : appRoot;
165
+ const file = findConfigFile(searchFrom);
166
+ base = file === undefined ? { plugins: [] } : await loadConfigFile(file, mode);
167
+ }
168
+ const plugins = base.plugins ?? [];
169
+ const added = [];
170
+ if (!hasPlugin(plugins, NESTING_PLUGIN_NAME))
171
+ added.push(postcssNesting(), unwrapLeadingIs);
172
+ if (!hasPlugin(plugins, PX_PLUGIN_NAME))
173
+ added.push(pxToDip);
174
+ if (added.length === 0)
175
+ return config;
176
+ return { ...config, css: { ...config.css, postcss: { ...base, plugins: [...plugins, ...added] } } };
177
+ }
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ConfigEnv, UserConfig, Alias } from 'vite';
2
2
  import type { GjsifyNativescriptOptions } from '@gjsify/vite-plugin-gjsify';
3
+ export { nativescriptCssNesting } from './css-nesting.js';
3
4
  export type GjsifyNativescriptViteOptions = GjsifyNativescriptOptions;
4
5
  /** A user-supplied Vite config (or config factory) merged in as the final layer. */
5
6
  export type UserConfigInput = UserConfig | ((env: ConfigEnv) => UserConfig | Promise<UserConfig>);
package/lib/index.js CHANGED
@@ -51,6 +51,8 @@ import { dirname, join } from 'node:path';
51
51
  import { fileURLToPath, pathToFileURL } from 'node:url';
52
52
  import { mergeConfig } from 'vite';
53
53
  import { gjsifyNativescript as gjsifyNativescriptTransforms } from '@gjsify/vite-plugin-gjsify';
54
+ import { nativescriptCssNesting } from './css-nesting.js';
55
+ export { nativescriptCssNesting } from './css-nesting.js';
54
56
  /**
55
57
  * Build a NativeScript Vite config that works under Vite 8 / Rolldown with
56
58
  * gjsify's transforms. Use it as your app's `vite.config.ts`:
@@ -99,10 +101,15 @@ export function defineNativescriptConfig(options = {}, userConfig) {
99
101
  // Keep the strings and decorators NativeScript's static binding generator reads
100
102
  // (see {@link nativescriptReleaseMinify}); a consumer's own `build.minify` wins below.
101
103
  const withMinify = mergeConfig(withSbgFix, nativescriptReleaseMinify(withSbgFix));
102
- if (userConfig === undefined)
103
- return withMinify;
104
- const resolved = typeof userConfig === 'function' ? await userConfig(env) : userConfig;
105
- return mergeConfig(withMinify, resolved);
104
+ const resolved = userConfig === undefined
105
+ ? undefined
106
+ : typeof userConfig === 'function'
107
+ ? await userConfig(env)
108
+ : userConfig;
109
+ const merged = resolved === undefined ? withMinify : mergeConfig(withMinify, resolved);
110
+ // GTK-style nested stylesheets (`#id { child { … } }`) reach NativeScript's parser flat
111
+ // (see {@link nativescriptCssNesting}); last, so the user's own `css.postcss` is included.
112
+ return nativescriptCssNesting(merged, undefined, env.mode);
106
113
  };
107
114
  }
108
115
  export default defineNativescriptConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/nativescript-vite",
3
- "version": "0.64.15-next.287",
3
+ "version": "0.64.15",
4
4
  "description": "Vite 8 / Rolldown compatibility + gjsify transforms for building NativeScript apps — composes @nativescript/vite and fixes the pieces Rolldown rejects.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -39,7 +39,10 @@
39
39
  ],
40
40
  "license": "MIT",
41
41
  "dependencies": {
42
- "@gjsify/vite-plugin-gjsify": "0.64.15-next.287"
42
+ "@gjsify/adwaita-core": "^0.64.15",
43
+ "@gjsify/vite-plugin-gjsify": "^0.64.15",
44
+ "postcss-nesting": "^13.0.2",
45
+ "postcss-selector-parser": "^7.1.0"
43
46
  },
44
47
  "peerDependencies": {
45
48
  "@nativescript/canvas": "*",
@@ -75,11 +78,11 @@
75
78
  },
76
79
  "devDependencies": {
77
80
  "@types/node": "^25.9.2",
81
+ "postcss": "^8.5.0",
78
82
  "typescript": "^6.0.3",
79
83
  "vite": "^8.1.3"
80
84
  },
81
85
  "gjsify": {
82
86
  "tier": 2
83
- },
84
- "gitHead": "5742b282a8a2bc8633dd8384ef436768447f6a83"
87
+ }
85
88
  }