vite-plugin-resolve
Advanced tools
Comparing version 1.4.2 to 1.4.3
@@ -8,3 +8,12 @@ import { Plugin, UserConfig } from 'vite'; | ||
export interface VitePluginResolve { | ||
(resolves: Resolves): Plugin; | ||
( | ||
resolves: Resolves, | ||
options?: { | ||
/** | ||
* @default true | ||
* Whether to insert the external module into "optimizeDeps.exclude" | ||
*/ | ||
optimize: boolean; | ||
}, | ||
): Plugin; | ||
} | ||
@@ -20,3 +29,3 @@ | ||
* viteResolve({ | ||
* // resolve external module | ||
* // resolve external module, this like Vite external plugin | ||
* vue: `const vue = window.Vue; export default vue;`, | ||
@@ -49,1 +58,5 @@ * | ||
} | ||
export interface ModifyOptimizeDepsExclude { | ||
(config: UserConfig, exclude: string[]): void; | ||
} |
22
index.js
@@ -7,3 +7,4 @@ const fs = require('fs'); | ||
*/ | ||
module.exports = function resolve(resolves = {}) { | ||
module.exports = function resolve(resolves = {}, options = {}) { | ||
const { optimize = true } = options; | ||
let root = process.cwd(); | ||
@@ -24,2 +25,4 @@ let cacheDir = '.vite-plugin-resolve'; | ||
if (optimize) modifyOptimizeDepsExclude(config, Object.keys(resolves)); | ||
modifyAlias( | ||
@@ -42,3 +45,3 @@ config, | ||
const moduleId = path.join(cacheDir, module + '.js'); | ||
const moduleContent = typeof strOrFn === 'function' ? strOrFn() : strOrFn; | ||
const moduleContent = await (typeof strOrFn === 'function' ? strOrFn() : strOrFn); | ||
@@ -48,6 +51,3 @@ // supported nest moduleId ('@scope/name') | ||
// write custom-resolve | ||
fs.writeFileSync( | ||
moduleId, | ||
moduleContent instanceof Promise ? await moduleContent : moduleContent, | ||
); | ||
fs.writeFileSync(moduleId, moduleContent); | ||
} | ||
@@ -76,2 +76,12 @@ } | ||
/** | ||
* @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 --------- | ||
@@ -78,0 +88,0 @@ |
{ | ||
"name": "vite-plugin-resolve", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"description": "Custom resolve module content.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,2 +10,8 @@ # vite-plugin-resolve | ||
**English | [įŽäŊä¸æ](https://github.com/caoxiemeihao/vite-plugins/blob/main/packages/resolve/README.zh-CN.md)** | ||
- It can be compatible with Browser, Node.js and Electron, without environment | ||
- 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) | ||
## Install | ||
@@ -26,4 +32,4 @@ | ||
viteResolve({ | ||
// resolve external module | ||
vue: `const vue = window.Vue; export default vue;`, | ||
// resolve external module, this like Vite external plugin | ||
vue: `const vue = window.Vue; export { vue as default }`, | ||
@@ -40,18 +46,56 @@ // nested moduleId and return Promis<string> | ||
## Type define | ||
```ts | ||
export type viteResolve = ( | ||
resolves: [moduleId: string]: string | (() => string | Promise<string>), | ||
options?: { | ||
/** | ||
* @default true | ||
* Whether to insert the resolve module into "optimizeDeps.exclude" | ||
*/ | ||
optimize: boolean | ||
} | ||
) => import('vite').VitePlugin | ||
``` | ||
## How to work | ||
1. Resolve-module will be generated code into `node_modules/.vite-plugin-resolve/vue.js` | ||
2. Append an resolve-module into alias | ||
**Let's use Vue as an example** | ||
```js | ||
{ | ||
resolve: { | ||
alias: [ | ||
{ | ||
find: 'vue', | ||
replacement: 'User/work-directory/node_modules/.vite-plugin-resolve/vue.js', | ||
}, | ||
], | ||
}, | ||
} | ||
``` | ||
```js | ||
viteResolve({ | ||
vue: `const vue = window.Vue; export { vue as default }`, | ||
}) | ||
``` | ||
1. Create `node_modules/.vite-plugin-resolve/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-resolve/vue.js', | ||
}, | ||
], | ||
}, | ||
} | ||
``` | ||
3. Add `vue` to the `optimizeDeps.exclude` **by default**. You can disable it through `options.optimize` | ||
```js | ||
export default { | ||
optimizeDeps: { | ||
exclude: ['vue'], | ||
}, | ||
} | ||
``` |
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
9571
5
139
98