-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.ts
More file actions
274 lines (238 loc) · 9.56 KB
/
Copy pathmod.ts
File metadata and controls
274 lines (238 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @license LGPL-2.1-or-later
*
* vite-plugin-deno
*
* Copyright (C) 2024 - 2025 Hans Schallmoser
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA or see <https://www.gnu.org/licenses/>.
*/
import type { HmrContext, PluginOption } from "vite";
import type { Opt } from "./src/options.ts";
import { ModuleGraph } from "./src/graph.ts";
import { decodeSpec, encodeSpec, resolve } from "./src/resolve.ts";
import { resolve_undeclared_npm } from "./src/undeclared_npm.ts";
import { is_excluded } from "./src/exclude.ts";
import { getLogger } from "@logtape/logtape";
import { Ray } from "./src/ray.ts";
/**
* Plugin configuration
*/
export interface PluginDenoOptions {
/**
* override path to deno.json
*/
deno_json?: string;
/**
* override path to deno.lock
*/
deno_lock?: string;
/**
* bundling context
* 1. Controls the Node module resolution conditions (when set to `"browser"` the package.json
* `browser` entry is preferred over `main`)
* 2. `node:` imports are only allowed when set to `"deno"`
*/
env?: "deno" | "browser";
/**
* Those NPM packages will always be allowed even if they are not included in Deno's module graph.
*
* This can be used to resolve packages that are imported by injected code like HMR.
*/
undeclared_npm_imports?: string[];
/**
* Those npm dependencies are left over to vite for resolution. This requires a `node_nodules` folder (like the symlinked one deno generates)
*/
legacy_npm?: string[];
/**
* import map that is only applied to build module resolution.
*
* Useful for polyfilling `node:` and handling injected imports (JSX runtime, HMR, ...)
*
* Sometimes it might be required to add `#standalone` to the replaced import, otherwise
* you will get errors because the replaced import is (of course) not reported by deno info.
* The `#standalone` instructs the plugin to treat the import like an independent entrypoint.
*/
extra_import_map?: [string, string][] | Map<string, string | Promise<string>>;
/**
* All specifiers that are equal to this or match the RegExp are ignored, deferring
* resolution to other plugins and Vite.
*
* Using strings is deprecated (strings are converted to RegExps anyway)
*
* Note that the specifiers passed to this might be in different formats (even for the
* same one) depending on the circumstances where it is checked: It might be absolute
* or relative, as path or `file:` URL and with or without `npm:` prefix, semver version
* or version range.
*
* The RegExps should not be too expensive to check
*/
exclude?: (string | RegExp)[];
/**
* @logtape/logtape logger id
* @default "vite-plugin-deno"
*/
log_id?: string;
/**
* Minimum time in milliseconds between hot updates, see Issue #5
* @default 0
*/
hot_update_min_time?: number;
}
/**
* see {@link PluginDenoOptions}
*/
export function pluginDeno(options: PluginDenoOptions): PluginOption {
const extra_import_map = new Map(options.extra_import_map);
const o: Opt = {
logger: getLogger(options.log_id ?? "vite-plugin-deno"),
deno_json: options.deno_json,
deno_lock: options.deno_lock,
extra_import_map,
environment: options.env ?? "browser",
exclude: [
...options.exclude?.map((excl) => {
if (excl instanceof RegExp) {
return excl;
} else {
// transform to regexp
return new RegExp(`^${excl.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`);
}
}) ?? [],
],
legacy_npm: options.legacy_npm ?? [],
};
const legacy_npm_regex = new RegExp(`"npm:(${o.legacy_npm.join("|")})(@.+)"`, "g");
if (o.environment === "deno") {
o.exclude.push(/^node:/);
}
o.exclude.push(/\/@(fs|id)/);
o.exclude.push(/\/?(@|\.)vite\//);
o.exclude.push(/\/node_modules\//);
o.exclude.push(/^<stdin>$/);
const graph = new ModuleGraph(o);
resolve_undeclared_npm(options.undeclared_npm_imports ?? [], graph, extra_import_map);
return {
// @ts-ignore some of the Plugin type definitions do not include name
name: "vite-plugin-deno",
enforce: "pre",
config() {
if (o.environment === "deno") {
return {
build: {
rollupOptions: {
external: [/^node:/],
},
},
};
}
},
resolveId: {
order: "pre",
async handler(raw_id: string, raw_referrer: string | undefined) {
const start = performance.now();
const id = decodeSpec(raw_id);
const referrer = raw_referrer && decodeSpec(raw_referrer);
o.logger.debug(
`Resolve module {id}${id !== raw_id ? `({raw_id})` : ""} from {referrer}${
referrer !== raw_referrer ? `({raw_referrer})` : ""
}`,
{
id,
raw_id,
referrer,
raw_referrer,
},
);
const ray = new Ray(id, referrer ?? "");
if (id.endsWith(".html")) {
return null;
}
if (is_excluded(id, o)) {
o.logger.debug(`skipping (excluded specifier) {id}`, { ...ray });
return null;
}
if (referrer && is_excluded(referrer, o)) {
o.logger.debug(`skipping (excluded referrer) {id} from {referrer}`, { ...ray });
return null;
}
if (o.legacy_npm.includes(id)) {
o.logger.info(`skipping (legacy npm) {id}`, { ...ray });
return null;
}
const resolved = await resolve(o, ray, graph, id, referrer);
if (resolved) {
o.logger.debug(`resolved {id} from {referrer} to {resolved} ({duration}ms)`, {
...ray,
resolved: resolved.href,
duration: performance.now() - start,
});
const encoded = encodeSpec(resolved);
/**
* Does not work with workspaces, because workspace imports would be skipped
*/
// if (resolved.protocol === "file:" && referrer?.startsWith("file://")) {
// o.logger.debug(`resolved file specifier {id} from {referrer} to {resolved} ({duration}ms)`, {
// ...ray,
// resolved: resolved.href,
// duration: performance.now() - start,
// });
// return null;
// }
return encoded;
} else {
return null;
}
},
},
async load(raw_id: string) {
const id = decodeSpec(raw_id);
// const id = decodeURIComponent(raw_id);
o.logger.debug(`Loading module {id}({raw_id})`, { id, raw_id });
const mod = graph.get_resolved(id);
if (!mod) {
o.logger.warn(`[loader] failed to resolve {id}`, { id });
return;
}
let result = await mod.load();
result = result.replaceAll(legacy_npm_regex, (_, package_name) => `"${package_name}"`);
if (id.startsWith("http")) {
result = result.replace(/\/\/# sourceMappingURL.+/, "");
}
if (id.endsWith(".tsx")) {
result = result.replaceAll(/\/\*\* \@jsx.+\*\//g, "");
}
o.logger.debug(`loaded {id}`, { id });
return result;
},
handleHotUpdate(ctx: HmrContext) {
// console.log({ ...ctx, server: undefined, read: undefined, modules: undefined });
const last_file_update = last_update.get(ctx.file);
last_update.set(ctx.file, ctx.timestamp);
if (last_file_update && (ctx.timestamp - last_file_update) < (options.hot_update_min_time ?? 0)) {
o.logger.error(`skipping HMR update for {file}, too early (d={diff})`, {
file: ctx.file,
last_update: last_file_update,
timestamp: ctx.timestamp,
hot_update_min_time: options.hot_update_min_time,
diff: ctx.timestamp - last_file_update,
});
return [];
}
},
};
}
const last_update = new Map<string, number>();