vite-plugin-resolve
Advanced tools
Comparing version 1.5.2 to 1.6.0
@@ -23,7 +23,2 @@ import { Plugin, UserConfig } from 'vite'; | ||
/** | ||
* Whether to insert the external module into "optimizeDeps.exclude" | ||
* @default true | ||
*/ | ||
optimizeDepsExclude?: boolean; | ||
/** | ||
* Absolute path or relative path | ||
@@ -30,0 +25,0 @@ * @default ".vite-plugin-resolve" |
10
index.js
@@ -8,3 +8,3 @@ const fs = require('fs'); | ||
module.exports = function resolve(resolves = {}, options = {}) { | ||
let { optimizeDepsExclude = true, dir = '.vite-plugin-resolve' } = options; | ||
let { dir = '.vite-plugin-resolve' } = options; | ||
let root = process.cwd(); | ||
@@ -18,6 +18,3 @@ | ||
if (optimizeDepsExclude) { | ||
modifyOptimizeDepsExclude(config, Object.keys(resolves)); | ||
} | ||
modifyOptimizeDepsExclude(config, Object.keys(resolves)); | ||
modifyAlias( | ||
@@ -77,2 +74,5 @@ config, | ||
if (config.optimizeDeps.include) { | ||
exclude = exclude.filter(e => !config.optimizeDeps.include.includes(e)); | ||
} | ||
config.optimizeDeps.exclude.push(...exclude); | ||
@@ -79,0 +79,0 @@ } |
{ | ||
"name": "vite-plugin-resolve", | ||
"version": "1.5.2", | ||
"version": "1.6.0", | ||
"description": "Custom resolve module content.", | ||
@@ -11,5 +11,2 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@types/node": "^16.11.6", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.4.4", | ||
"vite": "^2.6.13" | ||
@@ -16,0 +13,0 @@ }, |
136
README.md
# vite-plugin-resolve | ||
[![npm package](https://nodei.co/npm/vite-plugin-resolve.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/vite-plugin-resolve) | ||
<br/> | ||
[![NPM version](https://img.shields.io/npm/v/vite-plugin-resolve.svg?style=flat)](https://npmjs.org/package/vite-plugin-resolve) | ||
[![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-resolve.svg?style=flat)](https://npmjs.org/package/vite-plugin-resolve) | ||
[![NPM version](https://img.shields.io/npm/v/vite-plugin-resolve.svg)](https://npmjs.org/package/vite-plugin-resolve) | ||
[![NPM Downloads](https://img.shields.io/npm/dw/vite-plugin-resolve.svg)](https://npmjs.org/package/vite-plugin-resolve) | ||
@@ -25,6 +23,3 @@ Custom resolve module content | ||
```ts | ||
// vite.config.ts | ||
import { builtinModules } from 'module' | ||
import { defineConfig, build } from 'vite' | ||
import { defineConfig } from 'vite' | ||
import resolve from 'vite-plugin-resolve' | ||
@@ -35,44 +30,75 @@ | ||
resolve({ | ||
// Resolve external module, this like Vite external plugin | ||
// Resolve custom module content | ||
// This like Vite external plugin | ||
vue: `const vue = window.Vue; export { vue as default }`, | ||
}), | ||
] | ||
}) | ||
``` | ||
// Supported nested moduleId and return an Promis<string> | ||
'@scope/name': async () => await require('fs').promises.readFile('path', 'utf-8'), | ||
#### Read a local file | ||
// Resolve Electron ipcRenderer | ||
electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`, | ||
```ts | ||
resolve({ | ||
// Supported nested moduleId | ||
// Supported return an Promis<string> | ||
'@scope/name': async () => await require('fs').promises.readFile('path', 'utf-8'), | ||
}) | ||
``` | ||
// 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`, | ||
#### Electron | ||
```ts | ||
resolve({ | ||
// Resolve Electron ipcRenderer in Renderer-process | ||
electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`, | ||
}) | ||
``` | ||
#### Resolve an ES module as an CommonJs module for Node.js | ||
**Such as [execa](https://www.npmjs.com/package/execa), [node-fetch](https://www.npmjs.com/package/node-fetch)** | ||
```ts | ||
import { builtinModules } from 'module' | ||
import { defineConfig, build } from 'vite' | ||
import resolve from 'vite-plugin-resolve' | ||
export default defineConfig({ | ||
plugins: [ | ||
resolve({ | ||
async execa(args) { | ||
// Build execa as an CommonJs module | ||
await build({ | ||
plugins: [ | ||
{ | ||
name: 'vite-plugin[node:mod-to-mod]', | ||
enforce: 'pre', | ||
// Replace `import fs from "node:fs"` with `import fs from "fs"` | ||
resolveId(source) { | ||
if (source.startsWith('node:')) { | ||
return source.replace('node:', '') | ||
} | ||
}, | ||
rollupOptions: { | ||
external: [ | ||
...builtinModules, | ||
], | ||
}, | ||
} | ||
], | ||
// Build execa.js into cache directory | ||
build: { | ||
outDir: args.dir, | ||
minify: false, | ||
emptyOutDir: false, | ||
lib: { | ||
entry: require.resolve('execa'), | ||
formats: ['cjs'], | ||
fileName: () => `execa.js`, | ||
}, | ||
}) | ||
}, | ||
} as Parameters<typeof resolve>[0]), {}), | ||
rollupOptions: { | ||
external: [ | ||
...builtinModules, | ||
], | ||
}, | ||
}, | ||
}) | ||
}, | ||
}) | ||
@@ -90,7 +116,2 @@ ] | ||
```ts | ||
export interface ResolveArgs { | ||
/** Generated file cache directory */ | ||
dir: string; | ||
} | ||
export interface Resolves { | ||
@@ -105,2 +126,7 @@ [moduleId: string]: | ||
} | ||
export interface ResolveArgs { | ||
/** Generated file cache directory */ | ||
dir: string; | ||
} | ||
``` | ||
@@ -113,7 +139,2 @@ | ||
/** | ||
* Whether to insert the external module into "optimizeDeps.exclude" | ||
* @default true | ||
*/ | ||
optimizeDepsExclude: boolean; | ||
/** | ||
* Absolute path or relative path | ||
@@ -128,3 +149,3 @@ * @default ".vite-plugin-resolve" | ||
**Let's use Vue as an example** | ||
#### Let's use Vue as an example | ||
@@ -158,3 +179,4 @@ ```js | ||
3. Add `vue` to the `optimizeDeps.exclude` **by default**. You can disable it through `options.optimizeDepsExclude` | ||
3. Add `vue` to the `optimizeDeps.exclude` by default. | ||
You can avoid it through `optimizeDeps.include` | ||
@@ -167,2 +189,2 @@ ```js | ||
} | ||
``` | ||
``` |
# vite-plugin-resolve | ||
[![npm package](https://nodei.co/npm/vite-plugin-resolve.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/vite-plugin-resolve) | ||
<br/> | ||
[![NPM version](https://img.shields.io/npm/v/vite-plugin-resolve.svg?style=flat)](https://npmjs.org/package/vite-plugin-resolve) | ||
[![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-resolve.svg?style=flat)](https://npmjs.org/package/vite-plugin-resolve) | ||
[![NPM version](https://img.shields.io/npm/v/vite-plugin-resolve.svg)](https://npmjs.org/package/vite-plugin-resolve) | ||
[![NPM Downloads](https://img.shields.io/npm/dw/vite-plugin-resolve.svg)](https://npmjs.org/package/vite-plugin-resolve) | ||
@@ -25,6 +23,3 @@ 自定义加载模块内容 | ||
```ts | ||
// vite.config.ts | ||
import { builtinModules } from 'module' | ||
import { defineConfig, build } from 'vite' | ||
import { defineConfig } from 'vite' | ||
import resolve from 'vite-plugin-resolve' | ||
@@ -35,44 +30,75 @@ | ||
resolve({ | ||
// 加载外部 vue 这个场景就是 external | ||
// 加载自定模块内容 | ||
// 这个场景就是 external | ||
vue: `const vue = window.Vue; export { vue as default }`, | ||
}), | ||
] | ||
}) | ||
``` | ||
// 支持嵌套模块命名,支持返回 Promis<string> | ||
'@scope/name': async () => await require('fs').promises.readFile('path', 'utf-8'), | ||
#### 读取本地文件 | ||
// 在 Electron 中使用 | ||
electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`, | ||
```ts | ||
resolve({ | ||
// 支持嵌套模块命名 | ||
// 支持返回 Promis<string> | ||
'@scope/name': async () => await require('fs').promises.readFile('path', 'utf-8'), | ||
}) | ||
``` | ||
// 将 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`, | ||
#### Electron | ||
```ts | ||
resolve({ | ||
// 在 Electron 渲染进程中加载 ipcRenderer | ||
electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`, | ||
}) | ||
``` | ||
#### 将 ES 模块转换成 CommonJs 模块供 Node.js 使用 | ||
**例如 [execa](https://www.npmjs.com/package/execa), [node-fetch](https://www.npmjs.com/package/node-fetch)** | ||
```ts | ||
import { builtinModules } from 'module' | ||
import { defineConfig, build } from 'vite' | ||
import resolve from 'vite-plugin-resolve' | ||
export default defineConfig({ | ||
plugins: [ | ||
resolve({ | ||
async execa(args) { | ||
// 将 execa 构建成 CommonJs 模块 | ||
await build({ | ||
plugins: [ | ||
{ | ||
name: 'vite-plugin[node:mod-to-mod]', | ||
enforce: 'pre', | ||
// 将 import fs from "node:fs" 替换为 import fs from "fs" | ||
resolveId(source) { | ||
if (source.startsWith('node:')) { | ||
return source.replace('node:', '') | ||
} | ||
}, | ||
rollupOptions: { | ||
external: [ | ||
...builtinModules, | ||
], | ||
}, | ||
} | ||
], | ||
// 将 execa.js 写入到缓存目录 | ||
build: { | ||
outDir: args.dir, | ||
minify: false, | ||
emptyOutDir: false, | ||
lib: { | ||
entry: require.resolve('execa'), | ||
formats: ['cjs'], | ||
fileName: () => `execa.js`, | ||
}, | ||
}) | ||
}, | ||
} as Parameters<typeof resolve>[0]), {}), | ||
rollupOptions: { | ||
external: [ | ||
...builtinModules, | ||
], | ||
}, | ||
}, | ||
}) | ||
}, | ||
}) | ||
@@ -90,7 +116,2 @@ ] | ||
```ts | ||
export interface ResolveArgs { | ||
/** 生成缓存文件夹 */ | ||
dir: string; | ||
} | ||
export interface Resolves { | ||
@@ -105,2 +126,7 @@ [moduleId: string]: | ||
} | ||
export interface ResolveArgs { | ||
/** 生成缓存文件夹 */ | ||
dir: string; | ||
} | ||
``` | ||
@@ -113,7 +139,2 @@ | ||
/** | ||
* 是否将模块插入到 "optimizeDeps.exclude" | ||
* @default true | ||
*/ | ||
optimizeDepsExclude: boolean; | ||
/** | ||
* 相对或绝对路径 | ||
@@ -128,3 +149,3 @@ * @default ".vite-plugin-resolve" | ||
**用 Vue 来举个 🌰** | ||
#### 用 Vue 来举个 🌰 | ||
@@ -158,3 +179,4 @@ ```js | ||
3. 默认会将 `vue` 添加到 `optimizeDeps.exclude` 中. 你可以通过 `options.optimizeDepsExclude` 禁用 | ||
3. 默认会将 `vue` 添加到 `optimizeDeps.exclude` 中 | ||
你可以通过 `optimizeDeps.include` 绕开 | ||
@@ -161,0 +183,0 @@ ```js |
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
1
183
11897
124