-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
180 lines (158 loc) · 5.59 KB
/
mod.ts
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
/**
* @license LGPL-2.1-or-later
*
* vite-plugin-deno
*
* Copyright (C) 2024 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 { Plugin } 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";
/**
* 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[];
/**
* 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)[];
}
/**
* see {@link PluginDenoOptions}
*/
export function pluginDeno(options: PluginDenoOptions): Plugin {
const extra_import_map = new Map(options.extra_import_map);
const o: Opt = {
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, "\\$&")}$`);
}
}) ?? [],
],
};
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 {
name: "vite-plugin-deno",
enforce: "pre",
config() {
if (o.environment === "deno") {
return {
build: {
rollupOptions: {
external: [/^node:/],
},
},
};
}
},
resolveId: {
order: "pre",
async handler(id, referrer) {
id = decodeSpec(id);
referrer = referrer && decodeSpec(referrer);
if (id.endsWith(".html")) {
return null;
}
if (is_excluded(id, o)) {
return null;
}
if (referrer && is_excluded(referrer, o)) {
return null;
}
const resolved = await resolve(o, graph, id, referrer);
if (resolved) {
return encodeSpec(resolved);
} else {
return null;
}
},
},
async load(id) {
id = decodeSpec(id);
const mod = graph.get_resolved(id);
if (!mod) {
return;
}
let result = await mod.load();
if (id.startsWith("http")) {
result = result.replace(/\/\/# sourceMappingURL.+/, "");
}
return result;
},
};
}