Socket
Socket
Sign inDemoInstall

vite-plugin-fast-external

Package Overview
Dependencies
0
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.6.2 to 2.0.0

32

index.d.ts
import { Plugin } from 'vite';
export type Externals = Record<string, string | ((args: { dir: string; }) => string | Promise<string>)>;
export default external;
declare const external: VitePluginFastExternal;
export interface ExternalOptions {
/**
* Absolute path or relative path
* @default ".vite-plugin-fast-external"
*/
dir?: string;
}
export interface VitePluginFastExternal {
(externals: Externals, options?: ExternalOptions): Plugin;
(entries: Record<string, string | ((id: string) => string | Promise<string>)>): Plugin;
}
/**
* @example
* ```js
* fastExternal({
* // Simple example
* vue: 'Vue',
*
* // Custom external code by function
* '@scope/name': () => `const Lib = window.ScopeName.Member; export default Lib;`,
*
* // Read a template file and return Promise<string>
* externalId: async () => await require('fs').promises.readFile('path', 'utf-8'),
* })
* ```
*/
declare const fastExternal: VitePluginFastExternal;
export default fastExternal;

54

index.js

@@ -1,22 +0,44 @@

const resolve = require('vite-plugin-resolve');
/**
* @type {import('.').VitePluginFastExternal}
* @type {import(".").VitePluginFastExternal}
*/
module.exports = function external(externals, options = {}) {
if (!options.dir) {
options.dir = '.vite-plugin-fast-external';
}
module.exports = function external(entries) {
const externalId = '__fast-external:';
const keys = Object.keys(entries);
for (const [moduleId, strOrFn] of Object.entries(externals)) {
if (typeof strOrFn === 'string') {
const iifeName = strOrFn;
externals[moduleId] = `const ${iifeName} = window['${iifeName}']; export { ${iifeName} as default }`;
}
}
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 = [];
const plugin = resolve(externals, options);
plugin.name = 'vite-plugin-fast-external';
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;
}
}
return plugin;
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 }`;
}
},
};
};
{
"name": "vite-plugin-fast-external",
"version": "1.6.2",
"version": "2.0.0",
"description": "Without lexical transform, support custom external code.",

@@ -11,13 +11,10 @@ "main": "index.js",

"scripts": {},
"dependencies": {
"vite-plugin-resolve": "^1.6.0"
},
"devDependencies": {
"vite": "^2.6.13"
"vite": "^2.x"
},
"keywords": [
"vite",
"external",
"vite-plugin-external"
"plugin",
"external"
]
}

@@ -51,62 +51,8 @@ # vite-plugin-fast-external

### external(externals[, options])
### external(entries)
#### externals
**entries**
```ts
export type Externals = Record<string, string | ((args: { dir: string; }) => string | Promise<string>)>;
Record<string, string | ((id: string) => string | Promise<string>)>;
```
#### options
```ts
export interface ExternalOptions {
/**
* Absolute path or relative path
* @default ".vite-plugin-fast-external"
*/
dir?: string;
}
```
## How to work
**Let's use Vue as an example**
```js
external({
vue: 'Vue',
})
```
1. Create `node_modules/.vite-plugin-fast-external/vue.js` and contains the following code
```js
const vue = window['Vue']; export { vue as default }
```
2. Create a `vue` alias item and add it to `resolve.alias`
```js
{
resolve: {
alias: [
{
find: 'vue',
replacement: 'User/work-directory/node_modules/.vite-plugin-fast-external/vue.js',
},
],
},
}
```
3. Add `vue` to the `optimizeDeps.exclude` by default.
You can avoid it through `optimizeDeps.include`
```js
export default {
optimizeDeps: {
exclude: ['vue'],
},
}
```

@@ -51,62 +51,8 @@ # vite-plugin-fast-external

### external(externals[, options])
### external(entries)
#### externals
**entries**
```ts
export type Externals = Record<string, string | ((args: { dir: string; }) => string | Promise<string>)>;
Record<string, string | ((id: string) => string | Promise<string>)>;
```
#### options
```ts
export interface ExternalOptions {
/**
* 相对或绝对路径
* @default ".vite-plugin-fast-external"
*/
dir?: string;
}
```
## 工作原理
**用 Vue 来举个 🌰**
```js
external({
vue: 'Vue',
})
```
1. 创建 `node_modules/.vite-plugin-fast-external/vue.js` 文件并包含下面的代码
```js
const vue = window['Vue']; export { vue as default }
```
2. 创建一个 `vue` 的别名项,并且添加到 `resolve.alias`
```js
{
resolve: {
alias: [
{
find: 'vue',
replacement: 'User/work-directory/node_modules/.vite-plugin-fast-external/vue.js',
},
],
},
}
```
3. 默认会将 `vue` 添加到 `optimizeDeps.exclude` 中
你可以通过 `optimizeDeps.include` 绕开
```js
export default {
optimizeDeps: {
exclude: ['vue'],
},
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc