Skip to content

Commit b184026

Browse files
committed
Release v0.1.4
1 parent 1852dd4 commit b184026

File tree

16 files changed

+677
-1
lines changed

16 files changed

+677
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-plugin-node-polyfills",
33
"description": "A Vite plugin to polyfill native Node modules for the browser",
4-
"version": "0.1.3",
4+
"version": "0.1.4",
55
"author": "David Myers <[email protected]>",
66
"funding": "https://github.com/sponsors/voracious",
77
"license": "MIT",

package/.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"arcanis.vscode-zipfs"
4+
]
5+
}

package/.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"search.exclude": {
3+
"**/.yarn": true,
4+
"**/.pnp.*": true
5+
},
6+
"typescript.tsdk": ".yarn/sdks/typescript/lib",
7+
"typescript.enablePromptUseWorkspaceTsdk": true
8+
}

package/.yarn/sdks/integrations.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file is automatically generated by @yarnpkg/sdks.
2+
# Manual changes might be lost!
3+
4+
integrations:
5+
- vscode

package/.yarn/sdks/typescript/bin/tsc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire, createRequireFromPath} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
11+
12+
if (existsSync(absPnpApiPath)) {
13+
if (!process.versions.pnp) {
14+
// Setup the environment to be able to require typescript/bin/tsc
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real typescript/bin/tsc your application uses
20+
module.exports = absRequire(`typescript/bin/tsc`);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire, createRequireFromPath} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
11+
12+
if (existsSync(absPnpApiPath)) {
13+
if (!process.versions.pnp) {
14+
// Setup the environment to be able to require typescript/bin/tsserver
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real typescript/bin/tsserver your application uses
20+
module.exports = absRequire(`typescript/bin/tsserver`);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire, createRequireFromPath} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
11+
12+
if (existsSync(absPnpApiPath)) {
13+
if (!process.versions.pnp) {
14+
// Setup the environment to be able to require typescript/lib/tsc.js
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real typescript/lib/tsc.js your application uses
20+
module.exports = absRequire(`typescript/lib/tsc.js`);
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire, createRequireFromPath} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
11+
12+
const moduleWrapper = tsserver => {
13+
if (!process.versions.pnp) {
14+
return tsserver;
15+
}
16+
17+
const {isAbsolute} = require(`path`);
18+
const pnpApi = require(`pnpapi`);
19+
20+
const isVirtual = str => str.match(/\/(\$\$virtual|__virtual__)\//);
21+
const isPortal = str => str.startsWith("portal:/");
22+
const normalize = str => str.replace(/\\/g, `/`).replace(/^\/?/, `/`);
23+
24+
const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => {
25+
return `${locator.name}@${locator.reference}`;
26+
}));
27+
28+
// VSCode sends the zip paths to TS using the "zip://" prefix, that TS
29+
// doesn't understand. This layer makes sure to remove the protocol
30+
// before forwarding it to TS, and to add it back on all returned paths.
31+
32+
function toEditorPath(str) {
33+
// We add the `zip:` prefix to both `.zip/` paths and virtual paths
34+
if (isAbsolute(str) && !str.match(/^\^?(zip:|\/zip\/)/) && (str.match(/\.zip\//) || isVirtual(str))) {
35+
// We also take the opportunity to turn virtual paths into physical ones;
36+
// this makes it much easier to work with workspaces that list peer
37+
// dependencies, since otherwise Ctrl+Click would bring us to the virtual
38+
// file instances instead of the real ones.
39+
//
40+
// We only do this to modules owned by the the dependency tree roots.
41+
// This avoids breaking the resolution when jumping inside a vendor
42+
// with peer dep (otherwise jumping into react-dom would show resolution
43+
// errors on react).
44+
//
45+
const resolved = isVirtual(str) ? pnpApi.resolveVirtual(str) : str;
46+
if (resolved) {
47+
const locator = pnpApi.findPackageLocator(resolved);
48+
if (locator && (dependencyTreeRoots.has(`${locator.name}@${locator.reference}`) || isPortal(locator.reference))) {
49+
str = resolved;
50+
}
51+
}
52+
53+
str = normalize(str);
54+
55+
if (str.match(/\.zip\//)) {
56+
switch (hostInfo) {
57+
// Absolute VSCode `Uri.fsPath`s need to start with a slash.
58+
// VSCode only adds it automatically for supported schemes,
59+
// so we have to do it manually for the `zip` scheme.
60+
// The path needs to start with a caret otherwise VSCode doesn't handle the protocol
61+
//
62+
// Ref: https://github.com/microsoft/vscode/issues/105014#issuecomment-686760910
63+
//
64+
// Update Oct 8 2021: VSCode changed their format in 1.61.
65+
// Before | ^zip:/c:/foo/bar.zip/package.json
66+
// After | ^/zip//c:/foo/bar.zip/package.json
67+
//
68+
case `vscode <1.61`: {
69+
str = `^zip:${str}`;
70+
} break;
71+
72+
case `vscode`: {
73+
str = `^/zip/${str}`;
74+
} break;
75+
76+
// To make "go to definition" work,
77+
// We have to resolve the actual file system path from virtual path
78+
// and convert scheme to supported by [vim-rzip](https://github.com/lbrayner/vim-rzip)
79+
case `coc-nvim`: {
80+
str = normalize(resolved).replace(/\.zip\//, `.zip::`);
81+
str = resolve(`zipfile:${str}`);
82+
} break;
83+
84+
// Support neovim native LSP and [typescript-language-server](https://github.com/theia-ide/typescript-language-server)
85+
// We have to resolve the actual file system path from virtual path,
86+
// everything else is up to neovim
87+
case `neovim`: {
88+
str = normalize(resolved).replace(/\.zip\//, `.zip::`);
89+
str = `zipfile://${str}`;
90+
} break;
91+
92+
default: {
93+
str = `zip:${str}`;
94+
} break;
95+
}
96+
}
97+
}
98+
99+
return str;
100+
}
101+
102+
function fromEditorPath(str) {
103+
switch (hostInfo) {
104+
case `coc-nvim`: {
105+
str = str.replace(/\.zip::/, `.zip/`);
106+
// The path for coc-nvim is in format of /<pwd>/zipfile:/<pwd>/.yarn/...
107+
// So in order to convert it back, we use .* to match all the thing
108+
// before `zipfile:`
109+
return process.platform === `win32`
110+
? str.replace(/^.*zipfile:\//, ``)
111+
: str.replace(/^.*zipfile:/, ``);
112+
} break;
113+
114+
case `neovim`: {
115+
str = str.replace(/\.zip::/, `.zip/`);
116+
// The path for neovim is in format of zipfile:///<pwd>/.yarn/...
117+
return str.replace(/^zipfile:\/\//, ``);
118+
} break;
119+
120+
case `vscode`:
121+
default: {
122+
return process.platform === `win32`
123+
? str.replace(/^\^?(zip:|\/zip)\/+/, ``)
124+
: str.replace(/^\^?(zip:|\/zip)\/+/, `/`);
125+
} break;
126+
}
127+
}
128+
129+
// Force enable 'allowLocalPluginLoads'
130+
// TypeScript tries to resolve plugins using a path relative to itself
131+
// which doesn't work when using the global cache
132+
// https://github.com/microsoft/TypeScript/blob/1b57a0395e0bff191581c9606aab92832001de62/src/server/project.ts#L2238
133+
// VSCode doesn't want to enable 'allowLocalPluginLoads' due to security concerns but
134+
// TypeScript already does local loads and if this code is running the user trusts the workspace
135+
// https://github.com/microsoft/vscode/issues/45856
136+
const ConfiguredProject = tsserver.server.ConfiguredProject;
137+
const {enablePluginsWithOptions: originalEnablePluginsWithOptions} = ConfiguredProject.prototype;
138+
ConfiguredProject.prototype.enablePluginsWithOptions = function() {
139+
this.projectService.allowLocalPluginLoads = true;
140+
return originalEnablePluginsWithOptions.apply(this, arguments);
141+
};
142+
143+
// And here is the point where we hijack the VSCode <-> TS communications
144+
// by adding ourselves in the middle. We locate everything that looks
145+
// like an absolute path of ours and normalize it.
146+
147+
const Session = tsserver.server.Session;
148+
const {onMessage: originalOnMessage, send: originalSend} = Session.prototype;
149+
let hostInfo = `unknown`;
150+
151+
Object.assign(Session.prototype, {
152+
onMessage(/** @type {string | object} */ message) {
153+
const isStringMessage = typeof message === 'string';
154+
const parsedMessage = isStringMessage ? JSON.parse(message) : message;
155+
156+
if (
157+
parsedMessage != null &&
158+
typeof parsedMessage === `object` &&
159+
parsedMessage.arguments &&
160+
typeof parsedMessage.arguments.hostInfo === `string`
161+
) {
162+
hostInfo = parsedMessage.arguments.hostInfo;
163+
if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK && process.env.VSCODE_IPC_HOOK.match(/Code\/1\.([1-5][0-9]|60)\./)) {
164+
hostInfo += ` <1.61`;
165+
}
166+
}
167+
168+
const processedMessageJSON = JSON.stringify(parsedMessage, (key, value) => {
169+
return typeof value === 'string' ? fromEditorPath(value) : value;
170+
});
171+
172+
return originalOnMessage.call(
173+
this,
174+
isStringMessage ? processedMessageJSON : JSON.parse(processedMessageJSON)
175+
);
176+
},
177+
178+
send(/** @type {any} */ msg) {
179+
return originalSend.call(this, JSON.parse(JSON.stringify(msg, (key, value) => {
180+
return typeof value === `string` ? toEditorPath(value) : value;
181+
})));
182+
}
183+
});
184+
185+
return tsserver;
186+
};
187+
188+
if (existsSync(absPnpApiPath)) {
189+
if (!process.versions.pnp) {
190+
// Setup the environment to be able to require typescript/lib/tsserver.js
191+
require(absPnpApiPath).setup();
192+
}
193+
}
194+
195+
// Defer to the real typescript/lib/tsserver.js your application uses
196+
module.exports = moduleWrapper(absRequire(`typescript/lib/tsserver.js`));

0 commit comments

Comments
 (0)