@navita/vite-plugin 2.0.5 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navita/vite-plugin",
3
- "version": "2.0.5",
3
+ "version": "2.1.0",
4
4
  "description": "Navita Vite Plugin",
5
5
  "keywords": [
6
6
  "vite",
@@ -19,6 +19,11 @@
19
19
  "import": "./remix.mjs",
20
20
  "require": "./remix.cjs",
21
21
  "types": "./remix.d.ts"
22
+ },
23
+ "./rwsdk": {
24
+ "import": "./rwsdk.mjs",
25
+ "require": "./rwsdk.cjs",
26
+ "types": "./rwsdk.d.ts"
22
27
  }
23
28
  },
24
29
  "dependencies": {
package/rwsdk.cjs ADDED
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ var fsp = require('node:fs/promises');
4
+ var path = require('node:path');
5
+ var index = require('./index.cjs');
6
+
7
+ function _interopNamespaceDefault(e) {
8
+ var n = Object.create(null);
9
+ if (e) {
10
+ Object.keys(e).forEach(function (k) {
11
+ if (k !== 'default') {
12
+ var d = Object.getOwnPropertyDescriptor(e, k);
13
+ Object.defineProperty(n, k, d.get ? d : {
14
+ enumerable: true,
15
+ get: function () { return e[k]; }
16
+ });
17
+ }
18
+ });
19
+ }
20
+ n.default = e;
21
+ return Object.freeze(n);
22
+ }
23
+
24
+ var fsp__namespace = /*#__PURE__*/_interopNamespaceDefault(fsp);
25
+ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
26
+
27
+ function navitaRwsdk(options) {
28
+ let projectRootDir;
29
+ let base;
30
+ const navitaPlugin = index.navita(options);
31
+ return [
32
+ navitaPlugin,
33
+ {
34
+ name: "navita-rwsdk",
35
+ enforce: "post",
36
+ configResolved (config) {
37
+ projectRootDir = config.root;
38
+ base = config.base;
39
+ },
40
+ async renderChunk (code) {
41
+ // Only run during the linker pass on the worker environment
42
+ // @ts-expect-error - environment exists at runtime in Vite 6+
43
+ const environmentName = this.environment?.name;
44
+ if (environmentName !== "worker" || process.env.RWSDK_BUILD_PASS !== "linker") {
45
+ return null;
46
+ }
47
+ // Read the client manifest to find the navita CSS path
48
+ const manifestPath = path__namespace.resolve(projectRootDir, "dist", "client", ".vite", "manifest.json");
49
+ let manifestContent;
50
+ try {
51
+ manifestContent = await fsp__namespace.readFile(manifestPath, "utf-8");
52
+ } catch {
53
+ console.warn("[navita-rwsdk] Could not read client manifest, skipping CSS replacement");
54
+ return null;
55
+ }
56
+ const manifest = JSON.parse(manifestContent);
57
+ // Find the navita CSS file in the manifest
58
+ let navitaCssPath = null;
59
+ for (const [key, value] of Object.entries(manifest)){
60
+ // Check if this is the navita CSS entry directly
61
+ if (key.includes("navita") && key.endsWith(".css")) {
62
+ navitaCssPath = (base || "/") + value.file;
63
+ break;
64
+ }
65
+ // Also check if it's referenced in the css array of any entry
66
+ if (value.css) {
67
+ for (const cssFile of value.css){
68
+ if (cssFile.includes("navita")) {
69
+ navitaCssPath = (base || "/") + cssFile;
70
+ break;
71
+ }
72
+ }
73
+ if (navitaCssPath) break;
74
+ }
75
+ }
76
+ if (!navitaCssPath) {
77
+ console.warn("[navita-rwsdk] Could not find navita CSS in manifest");
78
+ return null;
79
+ }
80
+ // Replace virtual:navita.css references with the actual hashed path
81
+ let newCode = code;
82
+ newCode = newCode.replaceAll(`/${index.VIRTUAL_MODULE_ID}`, navitaCssPath);
83
+ newCode = newCode.replaceAll(index.VIRTUAL_MODULE_ID, navitaCssPath);
84
+ if (newCode !== code) {
85
+ return {
86
+ code: newCode,
87
+ map: null
88
+ };
89
+ }
90
+ return null;
91
+ }
92
+ }
93
+ ];
94
+ }
95
+
96
+ exports.navitaRwsdk = navitaRwsdk;
package/rwsdk.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Plugin } from 'vite';
2
+ import { Options } from './index.js';
3
+ import '@navita/core/createRenderer';
4
+
5
+ declare function navitaRwsdk(options?: Options): Plugin[];
6
+
7
+ export { navitaRwsdk };
package/rwsdk.mjs ADDED
@@ -0,0 +1,74 @@
1
+ import * as fsp from 'node:fs/promises';
2
+ import * as path from 'node:path';
3
+ import { navita, VIRTUAL_MODULE_ID } from './index.mjs';
4
+
5
+ function navitaRwsdk(options) {
6
+ let projectRootDir;
7
+ let base;
8
+ const navitaPlugin = navita(options);
9
+ return [
10
+ navitaPlugin,
11
+ {
12
+ name: "navita-rwsdk",
13
+ enforce: "post",
14
+ configResolved (config) {
15
+ projectRootDir = config.root;
16
+ base = config.base;
17
+ },
18
+ async renderChunk (code) {
19
+ // Only run during the linker pass on the worker environment
20
+ // @ts-expect-error - environment exists at runtime in Vite 6+
21
+ const environmentName = this.environment?.name;
22
+ if (environmentName !== "worker" || process.env.RWSDK_BUILD_PASS !== "linker") {
23
+ return null;
24
+ }
25
+ // Read the client manifest to find the navita CSS path
26
+ const manifestPath = path.resolve(projectRootDir, "dist", "client", ".vite", "manifest.json");
27
+ let manifestContent;
28
+ try {
29
+ manifestContent = await fsp.readFile(manifestPath, "utf-8");
30
+ } catch {
31
+ console.warn("[navita-rwsdk] Could not read client manifest, skipping CSS replacement");
32
+ return null;
33
+ }
34
+ const manifest = JSON.parse(manifestContent);
35
+ // Find the navita CSS file in the manifest
36
+ let navitaCssPath = null;
37
+ for (const [key, value] of Object.entries(manifest)){
38
+ // Check if this is the navita CSS entry directly
39
+ if (key.includes("navita") && key.endsWith(".css")) {
40
+ navitaCssPath = (base || "/") + value.file;
41
+ break;
42
+ }
43
+ // Also check if it's referenced in the css array of any entry
44
+ if (value.css) {
45
+ for (const cssFile of value.css){
46
+ if (cssFile.includes("navita")) {
47
+ navitaCssPath = (base || "/") + cssFile;
48
+ break;
49
+ }
50
+ }
51
+ if (navitaCssPath) break;
52
+ }
53
+ }
54
+ if (!navitaCssPath) {
55
+ console.warn("[navita-rwsdk] Could not find navita CSS in manifest");
56
+ return null;
57
+ }
58
+ // Replace virtual:navita.css references with the actual hashed path
59
+ let newCode = code;
60
+ newCode = newCode.replaceAll(`/${VIRTUAL_MODULE_ID}`, navitaCssPath);
61
+ newCode = newCode.replaceAll(VIRTUAL_MODULE_ID, navitaCssPath);
62
+ if (newCode !== code) {
63
+ return {
64
+ code: newCode,
65
+ map: null
66
+ };
67
+ }
68
+ return null;
69
+ }
70
+ }
71
+ ];
72
+ }
73
+
74
+ export { navitaRwsdk };