Skip to content

Commit c282cf7

Browse files
committed
Transform theme import paths for window support
Refs: #7995
1 parent 05ea79d commit c282cf7

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

packages/samples/react/src/react.main.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { defineCustomElements } from '@public-ui/components/dist/loader';
88
import { BWSt, DEFAULT, ECL_EC, ECL_EU, ITZBund } from '@public-ui/themes';
99

1010
import { App } from './App';
11+
import { transformUncPathIfNecessary } from './shares/transformUncPathIfNecessary';
1112

1213
import type { Generic } from 'adopted-style-sheets';
1314

@@ -30,6 +31,7 @@ if (ENABLE_TAG_NAME_TRANSFORMER) {
3031
const getThemes = async () => {
3132
if (process.env.THEME_MODULE) {
3233
/* Visual regression testing mode: Themes are overridden with a certain theme module, that should be used instead. */
34+
process.env.THEME_MODULE = transformUncPathIfNecessary(process.env.THEME_MODULE); // process.env.THEME_MODULE must be used literally in the import(). Moving it to a constant breaks the import.
3335
const { [(process.env.THEME_EXPORT as string) || 'default']: theme } = (await import(/* @vite-ignore */ process.env.THEME_MODULE)) as Record<string, Theme>;
3436
return [theme];
3537
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Transforms a UNC-style path to a proper file URL format for browser usage on Windows systems.
3+
*
4+
* This function handles the conversion of UNC paths (Universal Naming Convention) that are commonly
5+
* used in Windows environments. UNC paths typically start with double backslashes and include a
6+
* server or drive specification.
7+
*
8+
* @param {string} uncPath - The UNC-style path to transform (e.g., "//c/path/to/file")
9+
* @returns {string} The transformed path suitable for browser usage:
10+
* - On Windows: Returns path in format "/C:/path/to/file/" with uppercase drive letter
11+
* - On non-Windows platforms: Returns the original path unchanged
12+
*
13+
* @example
14+
* // Windows platform
15+
* transformUncPathIfNecessary("//c/users/documents/file.txt")
16+
* // Returns: "/C:/users/documents/file.txt"
17+
*
18+
* // Non-Windows platform
19+
* transformUncPathIfNecessary("//c/users/documents/file.txt")
20+
* // Returns: "//c/users/documents/file.txt" (unchanged)
21+
*/
22+
export function transformUncPathIfNecessary(uncPath: string): string {
23+
if (process.env.PLATFORM !== 'win32') {
24+
return uncPath;
25+
}
26+
27+
// Remove leading // and split by /
28+
const parts = uncPath.replace(/^\/\//, '').split('/');
29+
30+
// Extract drive letter and convert to uppercase
31+
const driveLetter = parts[0].toUpperCase();
32+
33+
// Reconstruct the path with proper format
34+
const pathParts = parts.slice(1);
35+
const path = pathParts.join('/');
36+
37+
return `/${driveLetter}:/${path}`;
38+
}

packages/samples/react/vite.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineConfig } from 'vite';
22
import { execSync } from 'node:child_process';
33
import react from '@vitejs/plugin-react-swc';
44
import UnoCSS from '@unocss/vite';
5+
import process from 'process';
56

67
function getGitCommitHash(): string | null {
78
try {
@@ -21,6 +22,7 @@ export default defineConfig({
2122
'process.env.ENABLE_THEME_PATCHING': JSON.stringify(process.env.ENABLE_THEME_PATCHING || ''),
2223
'process.env.BUILD_DATE': JSON.stringify(new Date().toISOString()),
2324
'process.env.COMMIT_HASH': JSON.stringify(getGitCommitHash()),
25+
'process.env.PLATFORM': JSON.stringify(process.platform),
2426
},
2527
build: {
2628
sourcemap: true,

0 commit comments

Comments
 (0)