Skip to content

Commit 56fc810

Browse files
0xTanejabmeurer
authored andcommitted
renamed,nit fixed,added tests
1 parent 3e3eff2 commit 56fc810

3 files changed

Lines changed: 54 additions & 17 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ specify it in the options like in the following:
4545
| Name | Type | Default | Description |
4646
|------|------|---------|-------------|
4747
| `projectRoot` | `string` | `config.root` | Absolute path that will be reported to DevTools. Useful for monorepos or when the Vite root is not the desired folder. |
48-
| `normalizeForChrome` | `boolean` | `true` | Convert Linux paths to UNC form so Chrome on Windows (WSL / Docker Desktop) can mount them. Pass `false` to disable. |
48+
| `normalizeForWindowsContainer` | `boolean` | `true` | Convert Linux paths to UNC form so Chrome on Windows (WSL / Docker Desktop) can mount them (e.g. via WSL or Docker Desktop). Pass `false` to disable. <br/>_Alias:_ `normalizeForChrome` (deprecated)_ |
4949
| `uuid` | `string` | auto-generated | Fixed UUID if you prefer to control it yourself. |
5050

5151
Example with all options:
@@ -58,7 +58,7 @@ export default defineConfig({
5858
plugins: [
5959
devtoolsJson({
6060
projectRoot: '/absolute/path/to/project',
61-
normalizeForChrome: true,
61+
normalizeForWindowsContainer: true,
6262
uuid: '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'
6363
})
6464
]

src/index.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ interface DevToolsJsonOptions {
1313

1414
/**
1515
* Absolute (or relative) path that should be reported as the project root
16-
* in DevTools. When omitted we fall back to Vite's `config.root` logic
17-
* – identical to the original implementation.
16+
* in DevTools. When omitted, we fall back to Vite’s `config.root` logic.
1817
*/
1918
projectRoot?: string;
2019

2120
/**
22-
* Whether to rewrite Linux paths to UNC form so Chrome running on Windows
23-
* (WSL or Docker Desktop) can mount them as a workspace. Enabled by
24-
* default to preserve the original behaviour; pass `false` to opt-out.
21+
* @deprecated Use `normalizeForWindowsContainer` instead. Will be removed in a future major version.
2522
*/
2623
normalizeForChrome?: boolean;
24+
/**
25+
* Whether to rewrite Linux paths to UNC form so Chrome running on Windows
26+
* (WSL or Docker Desktop) can mount them as a workspace. Enabled by default.
27+
*/
28+
normalizeForWindowsContainer?: boolean;
2729
}
2830

2931
interface DevToolsJSON {
@@ -80,22 +82,37 @@ const plugin = (options: DevToolsJsonOptions = {}): Plugin => ({
8082
return uuid;
8183
};
8284

85+
// Determine effective normalisation flag once so we can reuse it.
86+
const normalizePaths =
87+
options.normalizeForWindowsContainer ??
88+
(options.normalizeForChrome ?? true);
89+
90+
// Emit deprecation warning if old option is used in isolation.
91+
if (
92+
Object.prototype.hasOwnProperty.call(options, 'normalizeForChrome') &&
93+
options.normalizeForWindowsContainer === undefined
94+
) {
95+
logger.warn(
96+
'[vite-plugin-devtools-json] "normalizeForChrome" is deprecated – please rename to "normalizeForWindowsContainer".'
97+
);
98+
}
99+
83100
server.middlewares.use(ENDPOINT, async (_req, res) => {
84101
// Determine the project root that will be reported to DevTools.
85102
const resolveProjectRoot = (): string => {
86103
if (options.projectRoot) {
87104
return path.resolve(options.projectRoot);
88105
}
89106
// Fall back to Vite's root handling (original behaviour)
90-
let {root} = config;
91-
if (!path.isAbsolute(root)) {
92-
root = path.resolve(process.cwd(), root);
93-
}
107+
let {root} = config;
108+
if (!path.isAbsolute(root)) {
109+
root = path.resolve(process.cwd(), root);
110+
}
94111
return root;
95112
};
96113

97-
const maybeNormalizeForChrome = (absRoot: string): string => {
98-
if (options.normalizeForChrome === false) return absRoot;
114+
const maybeNormalizePath = (absRoot: string): string => {
115+
if (!normalizePaths) return absRoot;
99116

100117
// WSL path rewrite
101118
if (process.env.WSL_DISTRO_NAME) {
@@ -112,12 +129,12 @@ const plugin = (options: DevToolsJsonOptions = {}): Plugin => ({
112129
return path
113130
.join('\\\\wsl.localhost', 'docker-desktop-data', withoutLeadingSlash)
114131
.replace(/\//g, '\\');
115-
}
132+
}
116133

117134
return absRoot;
118135
};
119136

120-
let root = maybeNormalizeForChrome(resolveProjectRoot());
137+
let root = maybeNormalizePath(resolveProjectRoot());
121138
const uuid = getOrCreateUUID();
122139
const devtoolsJson: DevToolsJSON = {
123140
workspace: {

test/index.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ describe('#VitePluginDevToolsJson', () => {
102102
await server.close();
103103
});
104104

105-
it('should skip path normalization when `normalizeForChrome` is false', async () => {
105+
it('should skip path normalization when `normalizeForWindowsContainer` is false', async () => {
106106
process.env.WSL_DISTRO_NAME = 'fake-distro';
107107
const server = await createServer({
108-
plugins: [VitePluginDevToolsJson({ normalizeForChrome: false })],
108+
plugins: [VitePluginDevToolsJson({ normalizeForWindowsContainer: false })],
109109
server: { port, host: true },
110110
});
111111
await server.listen();
@@ -118,5 +118,25 @@ describe('#VitePluginDevToolsJson', () => {
118118
await server.close();
119119
delete process.env.WSL_DISTRO_NAME;
120120
});
121+
122+
it('should convert path when running inside Docker Desktop on Windows', async () => {
123+
delete process.env.WSL_DISTRO_NAME;
124+
process.env.DOCKER_DESKTOP = 'true';
125+
126+
const server = await createServer({
127+
plugins: [VitePluginDevToolsJson()],
128+
server: { port, host: true },
129+
});
130+
131+
await server.listen();
132+
133+
const response = await request(server.httpServer!)
134+
.get('/.well-known/appspecific/com.chrome.devtools.json');
135+
const json = JSON.parse(response.text);
136+
expect(json.workspace.root).to.include('docker-desktop-data');
137+
138+
await server.close();
139+
delete process.env.DOCKER_DESKTOP;
140+
});
121141
});
122142
});

0 commit comments

Comments
 (0)