@nightshadeui/vite 0.0.0-stage → 3.12.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
@@ -1,3 +1,28 @@
1
- # Temporary Holding Version
1
+ # @nightshadeui/vite
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Vite plugins for Nightshade cascade layers. Vue components never write `@layer`; these put their styles in one, and state the order ahead of every stylesheet.
4
+
5
+ ```bash
6
+ npm i -D @nightshadeui/vite
7
+ ```
8
+
9
+ ```ts
10
+ import vue from '@vitejs/plugin-vue';
11
+ import { cssLayer, cssLayerOrder } from '@nightshadeui/vite';
12
+
13
+ export default defineConfig({
14
+ plugins: [
15
+ cssLayerOrder(['acme']), // Nightshade's three, then yours
16
+ cssLayer('acme'),
17
+ vue(),
18
+ ],
19
+ });
20
+ ```
21
+
22
+ `cssLayer()` takes every SFC outside `node_modules`; `cssLayer('acme', { dir })` only those under `dir`. A block that already opens with `@layer` is left alone, so list narrower plugins first. It wraps each style block's own module, so hot updates stay in the layer. Put it before `vue()`. A component library uses its own layer (`nightshade-components` for Nightshade's) and never lists the order.
23
+
24
+ `cssLayerOrder(['acme'])` states the order (Nightshade's three, then yours) ahead of every stylesheet: first in each CSS file the build emits, and in `index.html` for dev. Apps use it; libraries don't.
25
+
26
+ CSS that a library injects at runtime (CodeMirror's `<style>` tags) is unlayered, and no plugin can put it in a layer: it beats every layer. Style that library's elements through its own theme API (CodeMirror's `EditorView.theme()`), reading your variables, rather than with CSS in a component. That's by design: `cssLayer()` has no opt-out for unlayered blocks.
27
+
28
+ The `nightshade` umbrella re-exports the same plugins as `nightshade/vite`.
package/index.d.mts ADDED
@@ -0,0 +1,18 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ export interface CssLayerOptions {
4
+ /** Only the SFCs under this directory. By default, every SFC outside `node_modules`. */
5
+ dir?: string | URL;
6
+ }
7
+
8
+ /**
9
+ * A Vite plugin that puts the styles of Vue single-file components in a cascade layer, so a
10
+ * component's `<style>` never writes `@layer` itself. Put it before `vue()`.
11
+ */
12
+ export function cssLayer(layer: string, options?: CssLayerOptions): Plugin;
13
+
14
+ /**
15
+ * A Vite plugin that states the cascade layer order (Nightshade's, then `layers`) ahead of every
16
+ * stylesheet: first in each CSS file the build emits, and in `index.html` for dev.
17
+ */
18
+ export function cssLayerOrder(layers?: string[]): Plugin;
package/index.mjs ADDED
@@ -0,0 +1,90 @@
1
+ import { fileURLToPath } from 'node:url';
2
+
3
+ const NIGHTSHADE_LAYERS = ['nightshade', 'nightshade-docs', 'nightshade-components'];
4
+
5
+ /**
6
+ * A Vite plugin that states the cascade layer order ahead of every stylesheet: Nightshade's layers,
7
+ * weakest first, then the app's. Otherwise the first stylesheet to name a layer sets the order, and
8
+ * that is rarely the CSS entry: in dev a component's style loads first, and in a build a code-split
9
+ * chunk (or a VitePress bundle) can. It prepends the statement to every CSS file the build emits, and
10
+ * to `index.html` for dev. Apps use it; component libraries never list the order.
11
+ *
12
+ * @param {string[]} [layers] The app's own layers, after Nightshade's: `['primevue', 'acme']`.
13
+ * @returns {import('vite').Plugin}
14
+ */
15
+ export function cssLayerOrder(layers = []) {
16
+ const statement = `@layer ${[...NIGHTSHADE_LAYERS, ...layers].join(', ')};`;
17
+ return {
18
+ name: 'nightshade-css-layer-order',
19
+ transformIndexHtml: {
20
+ order: 'pre',
21
+ handler() {
22
+ return [
23
+ {
24
+ tag: 'style',
25
+ children: statement,
26
+ injectTo: 'head-prepend',
27
+ },
28
+ ];
29
+ },
30
+ },
31
+ // After Vite's own CSS plugin, which emits a single-file bundle in this hook.
32
+ generateBundle: {
33
+ order: 'post',
34
+ handler(_options, bundle) {
35
+ for (const file of Object.values(bundle)) {
36
+ if (file.type === 'asset' && file.fileName.endsWith('.css')) {
37
+ file.source = `${statement}\n${String(file.source)}`;
38
+ }
39
+ }
40
+ },
41
+ },
42
+ };
43
+ }
44
+
45
+ /**
46
+ * A Vite plugin that puts the styles of Vue single-file components in a cascade layer, so a
47
+ * component's `<style>` never writes `@layer` itself.
48
+ *
49
+ * It wraps each style block's own module (`Foo.vue?vue&type=style…`), not the SFC source, so blocks
50
+ * that a hot update reloads straight from disk land in the layer too. Any file compiled as an SFC
51
+ * counts, such as a VitePress page's `<style>`.
52
+ *
53
+ * Without `dir`, it takes every SFC outside `node_modules`. With `dir`, only the SFCs under it,
54
+ * wherever that is. A block that already opens with `@layer` is left alone, so the first plugin to
55
+ * claim a file wins: list narrower ones (`dir`) before a catch-all. Put it before `vue()`.
56
+ *
57
+ * @param {string} layer The cascade layer: `nightshade-components` for a component library, the
58
+ * app's or system's own layer (`acme`) otherwise.
59
+ * @param {{ dir?: string | URL }} [options]
60
+ * @returns {import('vite').Plugin}
61
+ */
62
+ export function cssLayer(layer, options = {}) {
63
+ const dir = options.dir ? toDirPrefix(options.dir) : null;
64
+ return {
65
+ name: `nightshade-css-layer:${layer}`,
66
+ enforce: 'pre',
67
+ transform(code, id) {
68
+ const [file, query = ''] = id.split('?', 2);
69
+ if (!/(?:^|&)vue(?:&|$)/.test(query) || !/(?:^|&)type=style(?:&|$)/.test(query)) {
70
+ return;
71
+ }
72
+ const included = dir ? file.startsWith(dir) : !file.includes('/node_modules/');
73
+ if (!included || /^\s*@layer\b/.test(code)) {
74
+ return;
75
+ }
76
+ return {
77
+ code: `@layer ${layer} {${code}}`,
78
+ map: null,
79
+ };
80
+ },
81
+ };
82
+ }
83
+
84
+ /**
85
+ * @param {string | URL} dir
86
+ */
87
+ function toDirPrefix(dir) {
88
+ const path = dir instanceof URL ? fileURLToPath(dir) : dir;
89
+ return path.replaceAll('\\', '/').replace(/\/?$/, '/');
90
+ }
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
- "name": "@nightshadeui/vite",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@nightshadeui/vite",
3
+ "version": "3.12.0",
4
+ "description": "Vite plugins for Nightshade cascade layers: cssLayer and cssLayerOrder",
5
+ "author": "Boris Okunskiy",
6
+ "license": "ISC",
7
+ "type": "module",
8
+ "sideEffects": false,
9
+ "exports": {
10
+ ".": {
11
+ "types": "./index.d.mts",
12
+ "import": "./index.mjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "index.mjs",
17
+ "index.d.mts"
18
+ ],
19
+ "peerDependencies": {
20
+ "vite": ">=6"
21
+ },
22
+ "peerDependenciesMeta": {
23
+ "vite": {
24
+ "optional": true
25
+ }
26
+ }
27
+ }