vite-plugin-fast-external
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -7,3 +7,3 @@ import { Plugin } from 'vite'; | ||
export interface VitePluginFastExternal { | ||
(entries: Record<string, string | ((id: string) => string | Promise<string>)>): Plugin; | ||
(entries: Record<string, string | ((id: string) => string | Promise<string>)>): Plugin[]; | ||
} |
64
index.js
@@ -5,41 +5,41 @@ /** | ||
module.exports = function external(entries) { | ||
const name = 'vite-plugin-fast-external'; | ||
const externalId = '__fast-external:'; | ||
const keys = Object.keys(entries); | ||
return { | ||
name: 'vite-plugin-fast-external', | ||
config(config) { | ||
if (!config.resolve) config.resolve = {}; | ||
if (!config.resolve.alias) config.resolve.alias = []; | ||
if (!config.optimizeDeps) config.optimizeDeps = {}; | ||
if (!config.optimizeDeps.exclude) config.optimizeDeps.exclude = []; | ||
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 = []; | ||
for (const key of keys) { | ||
if (Array.isArray(config.resolve.alias)) { | ||
config.resolve.alias.push({ | ||
find: key, | ||
replacement: externalId + key, | ||
}); | ||
} else { | ||
config.resolve.alias[key] = externalId + key; | ||
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]; | ||
let exclude = keys; | ||
if (config.optimizeDeps.include) { | ||
exclude = keys.filter(key => !config.optimizeDeps.include.includes(key)); | ||
} | ||
config.optimizeDeps.exclude.push(...exclude); | ||
return typeof fnOrIife === 'function' | ||
? fnOrIife(id) | ||
: `const M = window['${fnOrIife}']; export { M as default }`; | ||
} | ||
}, | ||
}, | ||
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 }`; | ||
} | ||
}, | ||
}; | ||
]; | ||
}; |
{ | ||
"name": "vite-plugin-fast-external", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Without lexical transform, support custom external code.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,5 +14,5 @@ # vite-plugin-fast-external | ||
- It's actually implemented by modify `resolve.alias` | ||
- With out ast analyze, load virtual files by resolveId-hooks -- Real efficient | ||
- By default `window` is used as the environment object, you can also customize the code snippets by return string from function -- Real flexible 🎉 | ||
- Support customize the code snippets by return string from function -- Real flexible 🎉 | ||
@@ -41,3 +41,3 @@ ## Install | ||
// Read a template file and return Promise<string> | ||
externalId: async () => await require('fs').promises.readFile('path', 'utf-8'), | ||
externalId: () => require('fs/promises').readFile('path', 'utf-8'), | ||
@@ -44,0 +44,0 @@ // Use in Electron |
@@ -12,7 +12,7 @@ # vite-plugin-fast-external | ||
- 类似 webpack 的 externals,支持浏览器、Node.js、Electron 等多环境 | ||
- 类似 Webpack 的 externals,支持浏览器、Node.js、Electron 等多环境 | ||
- 本质上是通过 `resolve.alias` 实现的模块重定向加载 | ||
- 没有使用语法分析,只是通过 resolveId 钩子拦截实现的模块重定向加载,十分高效 | ||
- 默认使用的 window 作为宿主对象,你也可以通过函数返回字符串的形式任意定制代码段 -- 十分灵活!🎉 | ||
- 支持通过函数返回字符串的形式任意定制代码段 -- 十分灵活!🎉 | ||
@@ -19,0 +19,0 @@ ## 安装 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47
5687