Socket
Socket
Sign inDemoInstall

vite-plugin-resolve

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-resolve - npm Package Compare versions

Comparing version 1.5.0 to 1.5.1

59

index.d.ts
import { Plugin, UserConfig } from 'vite';
export interface ResolveArgs {
/** Generated file cache directory */
dir: string;
}
export interface Resolves {
[moduleId: string]: string | ((args: { dir: string }) => string | Promise<string | void> | void) | void;
[moduleId: string]:
| string
| ((args: ResolveArgs) =>
| string
| Promise<string | void>
| void)
| void;
}
export interface ResolveOptions {
/**
* Whether to insert the external module into "optimizeDeps.exclude"
* @default true
*/
optimizeDepsExclude?: boolean;
/**
* Absolute path or relative path
* @default ".vite-plugin-resolve"
*/
dir?: string;
}
export interface VitePluginResolve {
(
resolves: Resolves,
options?: {
/**
* @default true
* Whether to insert the external module into "optimizeDeps.exclude"
*/
optimizeDepsExclude: boolean;
},
): Plugin;
(resolves: Resolves, options?: ResolveOptions)
}
/**
* Custom resolve code for vite
*
* @example
* ```js
* export default defineConfig({
* plugins: [
* viteResolve({
* // resolve external module, this like Vite external plugin
* vue: `const vue = window.Vue; export default vue;`,
*
* // nested moduleId and return Promis<string>
* '@scope/name': async () => await require('fs').promises.readFile('path', 'utf-8'),
*
* // electron
* electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`,
* })
* ]
* })
* ```
*/
declare const resolve: VitePluginResolve;
export default resolve;

@@ -44,0 +37,0 @@

@@ -8,5 +8,4 @@ const fs = require('fs');

module.exports = function resolve(resolves = {}, options = {}) {
const { optimizeDepsExclude = true } = options;
let { optimizeDepsExclude = true, dir = '.vite-plugin-resolve' } = options;
let root = process.cwd();
let dir = '.vite-plugin-resolve';

@@ -16,13 +15,9 @@ return {

async config(config) {
// init root path
if (config.root && path.isAbsolute(config.root)) {
// TODO: config.root is relative path
root = config.root;
if (!path.isAbsolute(dir)) dir = path.join(node_modules(root), dir);
if (config.root) root = path.resolve(config.root);
if (optimizeDepsExclude) {
modifyOptimizeDepsExclude(config, Object.keys(resolves));
}
// absolute path
dir = path.join(node_modules(root), dir);
if (optimizeDepsExclude) modifyOptimizeDepsExclude(config, Object.keys(resolves));
modifyAlias(

@@ -78,3 +73,3 @@ config,

*/
function modifyOptimizeDepsExclude(config, exclude) {
function modifyOptimizeDepsExclude(config, exclude) {
if (!config.optimizeDeps) config.optimizeDeps = {};

@@ -81,0 +76,0 @@ if (!config.optimizeDeps.exclude) config.optimizeDeps.exclude = [];

{
"name": "vite-plugin-resolve",
"version": "1.5.0",
"version": "1.5.1",
"description": "Custom resolve module content.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -12,5 +12,5 @@ # vite-plugin-resolve

- It can be compatible with Browser, Node.js and Electron, without environment
- It can be compatible with Browser, Node.js and Electron
- You can think of it as an enhanced Vite external plugin
- You can think of it as manual version [Pre-Bundling](https://vitejs.dev/guide/dep-pre-bundling.html)
- You can think of it as manually [Pre-Bundling](https://vitejs.dev/guide/dep-pre-bundling.html)

@@ -20,3 +20,3 @@ ## Install

```bash
npm i -D vite-plugin-resolve
npm i vite-plugin-resolve -D
```

@@ -26,17 +26,54 @@

```js
import { defineConfig } from 'vite'
import viteResolve from 'vite-plugin-resolve'
```ts
// vite.config.ts
import { builtinModules } from 'module'
import { defineConfig, build } from 'vite'
import resolve from 'vite-plugin-resolve'
export default defineConfig({
plugins: [
viteResolve({
// resolve external module, this like Vite external plugin
resolve({
// Resolve external module, this like Vite external plugin
vue: `const vue = window.Vue; export { vue as default }`,
// nested moduleId and return Promis<string>
// Supported nested moduleId and return an Promis<string>
'@scope/name': async () => await require('fs').promises.readFile('path', 'utf-8'),
// electron
// Resolve Electron ipcRenderer
electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`,
// Resolve Node.js ES Module as CommonJs Module. Such as execa, node-fetch
...['execa', 'node-fetch'].reduce((memo, moduleId) => Object.assign(memo, {
async [moduleId](args) {
await build({
plugins: [
{
name: 'vite-plugin[node:mod-to-mod]',
enforce: 'pre',
resolveId(source) {
if (source.startsWith('node:')) {
return source.replace('node:', '')
}
},
}
],
build: {
outDir: args.dir,
minify: false,
emptyOutDir: false,
lib: {
entry: require.resolve(moduleId),
formats: ['cjs'],
fileName: () => `${moduleId}.js`,
},
rollupOptions: {
external: [
...builtinModules,
],
},
},
})
},
} as Parameters<typeof resolve>[0]), {}),
})

@@ -47,17 +84,42 @@ ]

## Type define
## API
### resolve(resolves[, options])
##### resolves
```ts
export type viteResolve = (
resolves: [moduleId: string]: string | ((args: { dir: string }) => string | Promise<string | void> | void) | void,
options?: {
/**
* @default true
* Whether to insert the resolve module into "optimizeDeps.exclude"
*/
optimizeDepsExclude: boolean
}
) => import('vite').VitePlugin
export interface ResolveArgs {
/** Generated file cache directory */
dir: string;
}
export interface Resolves {
[moduleId: string]:
| string
| ((args: ResolveArgs) =>
| string
| Promise<string | void>
| void)
| void;
}
```
##### options
```ts
export interface ResolveOptions {
/**
* Whether to insert the external module into "optimizeDeps.exclude"
* @default true
*/
optimizeDepsExclude: boolean;
/**
* Absolute path or relative path
* @default ".vite-plugin-resolve"
*/
dir: string;
}
```
## How to work

@@ -68,3 +130,3 @@

```js
viteResolve({
resolve({
vue: `const vue = window.Vue; export { vue as default }`,

@@ -71,0 +133,0 @@ })

@@ -12,3 +12,3 @@ # vite-plugin-resolve

- 兼容 Browser, Node.js and Electron, 无关环境
- 兼容 Browser, Node.js and Electron
- 你可以认为它是一个加强版的 Vite external 插件

@@ -20,3 +20,3 @@ - 你可以认为它是手动版的 Vite 预构建 [Pre-Bundling](https://vitejs.dev/guide/dep-pre-bundling.html)

```bash
npm i -D vite-plugin-resolve
npm i vite-plugin-resolve -D
```

@@ -26,9 +26,12 @@

```js
import { defineConfig } from 'vite'
import viteResolve from 'vite-plugin-resolve'
```ts
// vite.config.ts
import { builtinModules } from 'module'
import { defineConfig, build } from 'vite'
import resolve from 'vite-plugin-resolve'
export default defineConfig({
plugins: [
viteResolve({
resolve({
// 加载外部 vue 这个场景就是 external

@@ -42,2 +45,36 @@ vue: `const vue = window.Vue; export { vue as default }`,

electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`,
// 将 Node.js ES Module 模块转换成 CommonJs 模块. 比如 execa, node-fetch
...['execa', 'node-fetch'].reduce((memo, moduleId) => Object.assign(memo, {
async [moduleId](args) {
await build({
plugins: [
{
name: 'vite-plugin[node:mod-to-mod]',
enforce: 'pre',
resolveId(source) {
if (source.startsWith('node:')) {
return source.replace('node:', '')
}
},
}
],
build: {
outDir: args.dir,
minify: false,
emptyOutDir: false,
lib: {
entry: require.resolve(moduleId),
formats: ['cjs'],
fileName: () => `${moduleId}.js`,
},
rollupOptions: {
external: [
...builtinModules,
],
},
},
})
},
} as Parameters<typeof resolve>[0]), {}),
})

@@ -48,17 +85,42 @@ ]

## 类型定义
## API
### resolve(resolves[, options])
##### resolves
```ts
export type viteResolve = (
resolves: [moduleId: string]: string | (() => string | Promise<string>),
options?: {
/**
* @default true
* 是否将模块插入到 "optimizeDeps.exclude"
*/
optimizeDepsExclude: boolean
}
) => import('vite').VitePlugin
export interface ResolveArgs {
/** 生成缓存文件夹 */
dir: string;
}
export interface Resolves {
[moduleId: string]:
| string
| ((args: ResolveArgs) =>
| string
| Promise<string | void>
| void)
| void;
}
```
##### options
```ts
export interface ResolveOptions {
/**
* 是否将模块插入到 "optimizeDeps.exclude"
* @default true
*/
optimizeDepsExclude: boolean;
/**
* 相对或绝对路径
* @default ".vite-plugin-resolve"
*/
dir: string;
}
```
## 工作原理

@@ -65,0 +127,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