Socket
Socket
Sign inDemoInstall

vite-plugin-fast-external

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-fast-external - npm Package Compare versions

Comparing version 1.4.8 to 1.5.0

44

index.d.ts

@@ -1,16 +0,20 @@

import { Plugin, UserConfig } from 'vite';
import { Plugin } from 'vite';
export type External = Record<string, string | (() => string | Promise<string>)>;
export type Externals = Record<string, string | ((args: { dir: string; }) => string | Promise<string>)>;
export interface ExternalOptions {
/**
* Whether to insert the external module into "optimizeDeps.exclude"
* @default true
*/
optimizeDepsExclude?: boolean;
/**
* Absolute path or relative path
* @default ".vite-plugin-fast-external"
*/
dir?: string;
}
export interface VitePluginFastExternal {
(
external: External,
options?: {
/**
* @default true
* Whether to insert the external module into "optimizeDeps.exclude"
*/
optimizeDepsExclude: boolean;
},
): Plugin;
(externals: Externals, options?: ExternalOptions): Plugin;
}

@@ -36,17 +40,1 @@

export default fastExternal;
// --------- utils ---------
export interface GenerateExternalFile {
(externalDir: string, external: External): Promise<void>;
}
export interface ModifyAlias {
(
config: UserConfig,
aliaList: { [external: string]: string; }[],
): void;
}
export interface ModifyOptimizeDepsExclude {
(config: UserConfig, exclude: string[]): void;
}

@@ -1,3 +0,2 @@

const fs = require('fs');
const path = require('path');
const resolve = require('vite-plugin-resolve');

@@ -7,106 +6,17 @@ /**

*/
module.exports = function external(
external,
options = {},
) {
let root = process.cwd();
let externalDir = '.vite-plugin-fast-external';
const { optimizeDepsExclude = true } = options;
module.exports = function external(externals, options = {}) {
const { optimizeDepsExclude = true, dir = '.vite-plugin-fast-external' } = options;
return {
name: 'vite-plugin-fast-external',
async config(config) {
// init root path
if (config.root && path.isAbsolute(config.root)) {
// TODO: config.root is relative path
root = config.root;
}
// absolute path
externalDir = path.join(node_modules(root), externalDir);
if (optimizeDepsExclude) modifyOptimizeDepsExclude(config, Object.keys(external));
modifyAlias(
config,
Object.keys(external).map(moduleId => ({ [moduleId]: path.join(externalDir, moduleId) })),
);
await generateExternalFile(externalDir, external);
}
}
}
/**
* @type {import('.').GenerateExternalFile}
*/
async function generateExternalFile(externalDir, external) {
// generate external module file
for (const [module, strOrFn] of Object.entries(external)) {
const moduleId = path.join(externalDir, `${module}.js`);
let moduleContent;
Object.keys(externals).forEach(key => {
const strOrFn = Object.values(externals[key])[0];
if (typeof strOrFn === 'string') {
const libName = strOrFn;
moduleContent = `const ${libName} = window['${libName}']; export { ${libName} as default }`;
} else {
moduleContent = await strOrFn();
const iifeName = strOrFn;
externals[key] = `const ${iifeName} = window['${iifeName}']; export { ${iifeName} as default }`;
}
});
// supported nest moduleId ('@scope/name')
ensureDir(path.parse(moduleId).dir);
fs.writeFileSync(moduleId, moduleContent);
}
}
const plugin = resolve(externals, { optimizeDepsExclude, dir });
plugin.name = 'vite-plugin-fast-external';
/**
* @type {import('.').ModifyAlias}
*/
function modifyAlias(config, aliaList) {
if (!config.resolve) config.resolve = {};
let alias = config.resolve.alias || [];
if (!Array.isArray(alias)) {
// keep the the original alias
alias = Object.entries(alias).map(([k, v]) => ({ find: k, replacement: v }));
}
// redirect resolve-module to `node_modules/.vite-plugin-resolve`
alias.push(...aliaList.map(item => {
const [find, replacement] = Object.entries(item)[0];
return { find, replacement };
}));
config.resolve.alias = alias;
}
/**
* @type {import('.').ModifyOptimizeDepsExclude}
*/
function modifyOptimizeDepsExclude(config, exclude) {
if (!config.optimizeDeps) config.optimizeDeps = {};
if (!config.optimizeDeps.exclude) config.optimizeDeps.exclude = [];
config.optimizeDeps.exclude.push(...exclude);
}
// --------- utils ---------
function ensureDir(dir) {
if (!(fs.existsSync(dir) && fs.statSync(dir).isDirectory())) {
fs.mkdirSync(dir, { recursive: true });
}
return dir;
}
function node_modules(root, count = 0) {
if (node_modules.p) {
return node_modules.p;
}
const p = path.join(root, 'node_modules');
if (fs.existsSync(p)) {
return node_modules.p = p;
}
if (count >= 19) {
throw new Error('Can not found node_modules directory.');
}
return node_modules(path.join(root, '..'), count + 1);
}
return plugin;
};
{
"name": "vite-plugin-fast-external",
"version": "1.4.8",
"version": "1.5.0",
"description": "Without lexical transform, support custom external code.",

@@ -11,6 +11,6 @@ "main": "index.js",

"scripts": {},
"dependencies": {
"vite-plugin-resolve": "^1.5.2"
},
"devDependencies": {
"@types/node": "^16.11.6",
"tslib": "^2.3.1",
"typescript": "^4.4.4",
"vite": "^2.6.13"

@@ -17,0 +17,0 @@ },

@@ -12,3 +12,3 @@ # vite-plugin-fast-external

- Like Webpack externals, support browser, Node.js and Electron -- without environment
- Like Webpack externals, support browser, Node.js and Electron

@@ -19,18 +19,6 @@ - It's actually implemented by modify `resolve.alias`

**eg:**
```js
fastExternal({
// By default will generated code -> const Vue = window['Vue']; export { Vue as default }
vue: 'Vue',
// Custom external code snippets used in Node.js
nodeJsModule: () => `export default require('moduleId');`,
})
```
## Install
```bash
npm i -D vite-plugin-fast-external
npm i vite-plugin-fast-external -D
```

@@ -41,8 +29,9 @@

```js
import fastExternal from 'vite-plugin-fast-external';
import external from 'vite-plugin-fast-external';
export default defineConfig({
plugins: [
fastExternal({
external({
// Simple example
// By default will generated code -> const Vue = window['Vue']; export { Vue as default }
vue: 'Vue',

@@ -56,3 +45,3 @@

// Electron Renderer-process
// Use in Electron
electron: () => `const { ipcRenderer } = require('electron'); export { ipcRenderer }`,

@@ -64,17 +53,29 @@ })

## Type define
## API
### external(externals[, options])
#### externals
```ts
export type fastExternal = (
external: Record<string, string | (() => string | Promise<string>)>,
options?: {
/**
* @default true
* Whether to insert the external module into "optimizeDeps.exclude"
*/
optimizeDepsExclude: boolean
}
) => VitePlugin
export type Externals = Record<string, string | ((args: { dir: string; }) => string | Promise<string>)>;
```
#### options
```ts
export interface ExternalOptions {
/**
* Whether to insert the external module into "optimizeDeps.exclude"
* @default true
*/
optimizeDepsExclude?: boolean;
/**
* Absolute path or relative path
* @default ".vite-plugin-fast-external"
*/
dir?: string;
}
```
## How to work

@@ -85,3 +86,3 @@

```js
fastExternal({
external({
vue: 'Vue',

@@ -88,0 +89,0 @@ })

@@ -12,3 +12,3 @@ # vite-plugin-fast-external

- 类似 webpack 的 externals,支持浏览器、Node.js、Electron 等多环境 -- 环境无关
- 类似 webpack 的 externals,支持浏览器、Node.js、Electron 等多环境

@@ -19,18 +19,6 @@ - 本质上是通过 `resolve.alias` 实现的模块重定向加载

**比如:**
```js
fastExternal({
// 默认会生成 const Vue = window['Vue']; export { Vue as default }
vue: 'Vue',
// 自定义 external 代码段在 Node.js 中使用
nodeJsModule: () => `export default require('moduleId');`,
})
```
## 安装
```bash
npm i -D vite-plugin-fast-external
npm i vite-plugin-fast-external -D
```

@@ -41,8 +29,9 @@

```js
import fastExternal from 'vite-plugin-fast-external';
import external from 'vite-plugin-fast-external';
export default defineConfig({
plugins: [
fastExternal({
external({
// 基本使用
// 默认会生成 const Vue = window['Vue']; export { Vue as default }
vue: 'Vue',

@@ -63,17 +52,29 @@

## 类型定义
## API
### external(externals[, options])
#### externals
```ts
export type fastExternal = (
external: Record<string, string | (() => string | Promise<string>)>,
options?: {
/**
* @default true
* 是否要把 external 插入到 "optimizeDeps.exclude" 中,这样能避开 vite 的预构建
*/
optimizeDepsExclude: boolean
}
) => VitePlugin
export type Externals = Record<string, string | ((args: { dir: string; }) => string | Promise<string>)>;
```
#### options
```ts
export interface ExternalOptions {
/**
* 是否要把 external 插入到 "optimizeDeps.exclude" 中,这样能避开 vite 的预构建
* @default true
*/
optimizeDepsExclude?: boolean;
/**
* 相对或绝对路径
* @default ".vite-plugin-fast-external"
*/
dir?: string;
}
```
## 工作原理

@@ -84,3 +85,3 @@

```js
fastExternal({
external({
vue: 'Vue',

@@ -87,0 +88,0 @@ })

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc