forked from caoxiemeihao/vite-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (41 loc) · 1.19 KB
/
index.js
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
/**
* @type {import(".").VitePluginFastExternal}
*/
module.exports = function external(entries) {
const name = 'vite-plugin-fast-external';
const externalId = '__fast-external:';
const keys = Object.keys(entries);
return [
{
name: `${name}:resolveId`,
enforce: 'pre',
resolveId(source) {
if (keys.includes(source)) {
// avoid vite builtin `vite:resolve` plugin
return externalId + source;
}
},
},
{
name,
config(config) {
if (!config.optimizeDeps) config.optimizeDeps = {};
if (!config.optimizeDeps.exclude) config.optimizeDeps.exclude = [];
let exclude = keys;
if (config.optimizeDeps.include) {
exclude = keys.filter(key => !config.optimizeDeps.include.includes(key));
}
config.optimizeDeps.exclude.push(...exclude);
},
load(id) {
if (id.startsWith(externalId)) {
const module = id.split(externalId)[1];
const fnOrIife = entries[module];
return typeof fnOrIife === 'function'
? fnOrIife(id)
: `const M = window['${fnOrIife}']; export { M as default }`;
}
},
},
];
};