vite-plugin-resolve
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -13,1 +13,3 @@ import { Plugin } from 'vite'; | ||
} | ||
export const lib2esm: import('./presets').Lib2esm; |
@@ -45,1 +45,3 @@ /** | ||
}; | ||
module.exports.lib2esm = require('./presets').lib2esm; |
{ | ||
"name": "vite-plugin-resolve", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Custom resolve module content.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'antd', | ||
@@ -6,0 +6,0 @@ members: [ |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'antd', | ||
@@ -6,0 +6,0 @@ members: [ |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'antd', | ||
@@ -6,0 +6,0 @@ members: [ |
@@ -1,2 +0,1 @@ | ||
export interface LibMeta { | ||
@@ -7,21 +6,25 @@ name: string | ||
export declare function lib2esm( | ||
name: string, | ||
members: string[], | ||
options?: { | ||
/** | ||
* Generate code snippet format | ||
* | ||
* e.g. | ||
* ```js | ||
* const _M_ = require('lib'); // cjs | ||
* const _M_ = window['lib']; // iife | ||
* ``` | ||
* | ||
* @default "iife" | ||
*/ | ||
format?: 'cjs' | 'iife', | ||
}, | ||
): string | ||
export interface Lib2esmOptions { | ||
/** | ||
* Generate code snippet format | ||
* | ||
* e.g. | ||
* ```js | ||
* const _M_ = require('lib') // cjs | ||
* const _M_ = window['lib'] // iife | ||
* ``` | ||
* | ||
* @default "iife" | ||
*/ | ||
format?: 'cjs' | 'iife', | ||
} | ||
export interface Lib2esm { | ||
(name: string): string | ||
(name: string, options: Lib2esmOptions): string | ||
(name: string, members: string[]): string | ||
(name: string, members: string[], options: Lib2esmOptions): string | ||
} | ||
export declare const lib2esm: Lib2esm | ||
export declare const antd_vue: { | ||
@@ -31,14 +34,19 @@ v1: string | ||
} | ||
export declare const antd: { | ||
v4: string | ||
} | ||
export declare const element_plus: { | ||
v2: string | ||
} | ||
export declare const element_ui: { | ||
v2: string | ||
} | ||
export declare const pinia: { | ||
v2: string | ||
} | ||
export declare const react_dom: { | ||
@@ -48,2 +56,3 @@ v17: string | ||
} | ||
export declare const react_router_dom: { | ||
@@ -53,2 +62,3 @@ v5: string | ||
} | ||
export declare const react_router: { | ||
@@ -58,2 +68,3 @@ v5: string | ||
} | ||
export declare const react: { | ||
@@ -63,11 +74,15 @@ v17: string | ||
} | ||
export declare const redux: { | ||
v5: string | ||
} | ||
export declare const vue_composition_api: { | ||
v1: string | ||
} | ||
export declare const vue_router: { | ||
v4: string | ||
} | ||
export declare const vue: { | ||
@@ -77,5 +92,6 @@ v2: string | ||
} | ||
export declare const vuex: { | ||
v3: string | ||
v4: string | ||
} | ||
} |
const antd_vue_v1 = require('./ant-design-vue-v1'); | ||
const antd_vue_v3 = require('./ant-design-vue-v3'); | ||
const antd_v4 = require('./antd-v4'); | ||
const element_plus = require('./element-plus'); | ||
const element_ui = require('./element-ui'); | ||
const element_plus_v2 = require('./element-plus-v2'); | ||
const element_ui_v2 = require('./element-ui-v2'); | ||
const pinia_v2 = require('./pinia-v2'); | ||
@@ -16,3 +16,3 @@ const react_dom_v17 = require('./react-dom-v17'); | ||
const redux_v5 = require('./redux-v5'); | ||
const vue_composition_api = require('./vue-composition-api'); | ||
const vue_composition_api_v1 = require('./vue-composition-api-v1'); | ||
const vue_router_v4 = require('./vue-router-v4'); | ||
@@ -25,6 +25,18 @@ const vue_v2 = require('./vue-v2'); | ||
/** | ||
* @type {typeof import('.')['lib2esm']} | ||
* @type {import('.').Lib2esm} | ||
*/ | ||
exports.lib2esm = function (name, members, options = {}) { | ||
exports.lib2esm = function (name, ...args) { | ||
if (!args.length) { | ||
return exports.lib2esm(name, [], {}); | ||
} | ||
if (args.length === 1) { | ||
return Object.prototype.toString.call(args[0]) === '[object Object]' | ||
// lib2esm(name, options) | ||
? exports.lib2esm(name, [], args[0]) | ||
// lib2esm(name, members) | ||
: exports.lib2esm(name, args[0], {}) | ||
} | ||
const [members, options] = args; | ||
const { format = 'iife' } = options; | ||
const keywords = [ | ||
@@ -78,2 +90,3 @@ 'await', | ||
]; | ||
const exportMembers = members | ||
@@ -83,12 +96,13 @@ .filter(e => !keywords.includes(e)) | ||
.join('\n'); | ||
const importTpl = format === 'cjs' | ||
? `const _M_ = require('${name}');` | ||
: `const _M_ = window['${name}'];` | ||
const externalTpl = ` | ||
${importTpl} | ||
const _D_ = _M_.default || _M_; | ||
export { _D_ as default } | ||
${exportMembers} | ||
`; | ||
`.trim(); | ||
@@ -98,85 +112,63 @@ return () => externalTpl; | ||
exports.antd_vue_v1 = this.lib2esm( | ||
antd_vue_v1.name, | ||
antd_vue_v1.members, | ||
); | ||
exports.antd_vue_v3 = this.lib2esm( | ||
antd_vue_v3.name, | ||
antd_vue_v3.members, | ||
); | ||
exports.antd_v4 = this.lib2esm( | ||
antd_v4.name, | ||
antd_v4.members, | ||
); | ||
exports.element_plus = this.lib2esm( | ||
element_plus.name, | ||
element_plus.members, | ||
); | ||
exports.element_ui = this.lib2esm( | ||
element_ui.name, | ||
element_ui.members, | ||
); | ||
exports.pinia_v2 = this.lib2esm( | ||
pinia_v2.name, | ||
pinia_v2.members, | ||
); | ||
exports.react_dom_v17 = this.lib2esm( | ||
react_dom_v17.name, | ||
react_dom_v17.members, | ||
); | ||
exports.react_dom_v18 = this.lib2esm( | ||
react_dom_v18.name, | ||
react_dom_v18.members, | ||
); | ||
exports.react_router_dom_v5 = this.lib2esm( | ||
react_router_dom_v5.name, | ||
react_router_dom_v5.members, | ||
); | ||
exports.react_router_dom_v6 = this.lib2esm( | ||
react_router_dom_v6.name, | ||
react_router_dom_v6.members, | ||
); | ||
exports.react_router_v5 = this.lib2esm( | ||
react_router_v5.name, | ||
react_router_v5.members, | ||
); | ||
exports.react_router_v6 = this.lib2esm( | ||
react_router_v6.name, | ||
react_router_v6.members, | ||
); | ||
exports.react_v17 = this.lib2esm( | ||
react_v17.name, | ||
react_v17.members, | ||
); | ||
exports.react_v18 = this.lib2esm( | ||
react_v18.name, | ||
react_v18.members, | ||
); | ||
exports.redux_v5 = this.lib2esm( | ||
redux_v5.name, | ||
redux_v5.members, | ||
); | ||
exports.vue_composition_api = this.lib2esm( | ||
vue_composition_api.name, | ||
vue_composition_api.members, | ||
); | ||
exports.vue_router_v4 = this.lib2esm( | ||
vue_router_v4.name, | ||
vue_router_v4.members, | ||
); | ||
exports.vue_v2 = this.lib2esm( | ||
vue_v2.name, | ||
vue_v2.members, | ||
); | ||
exports.vue_v3 = this.lib2esm( | ||
vue_v3.name, | ||
vue_v3.members, | ||
); | ||
exports.vuex_v3 = this.lib2esm( | ||
vuex_v3.name, | ||
vuex_v3.members, | ||
) | ||
exports.vuex_v4 = this.lib2esm( | ||
vuex_v4.name, | ||
vuex_v4.members, | ||
) | ||
exports.antd_vue = { | ||
v1: this.lib2esm(antd_vue_v1.name, antd_vue_v1.members), | ||
v3: this.lib2esm(antd_vue_v3.name, antd_vue_v3.members), | ||
}; | ||
exports.antd = { | ||
v4: this.lib2esm(antd_v4.name, antd_v4.members), | ||
}; | ||
exports.element_plus = { | ||
v2: this.lib2esm(element_plus_v2.name, element_plus_v2.members), | ||
}; | ||
exports.element_ui = { | ||
v2: this.lib2esm(element_ui_v2.name, element_ui_v2.members), | ||
}; | ||
exports.pinia = { | ||
v2: this.lib2esm(pinia_v2.name, pinia_v2.members), | ||
}; | ||
exports.react_dom = { | ||
v17: this.lib2esm(react_dom_v17.name, react_dom_v17.members), | ||
v18: this.lib2esm(react_dom_v18.name, react_dom_v18.members), | ||
}; | ||
exports.react_router_dom = { | ||
v5: this.lib2esm(react_router_dom_v5.name, react_router_dom_v5.members), | ||
v6: this.lib2esm(react_router_dom_v6.name, react_router_dom_v6.members), | ||
}; | ||
exports.react_router = { | ||
v5: this.lib2esm(react_router_v5.name, react_router_v5.members), | ||
v6: this.lib2esm(react_router_v6.name, react_router_v6.members), | ||
}; | ||
exports.react = { | ||
v17: this.lib2esm(react_v17.name, react_v17.members), | ||
v18: this.lib2esm(react_v18.name, react_v18.members), | ||
}; | ||
exports.redux = { | ||
v5: this.lib2esm(redux_v5.name, redux_v5.members), | ||
}; | ||
exports.vue_composition_api = { | ||
v1: this.lib2esm(vue_composition_api_v1.name, vue_composition_api_v1.members), | ||
}; | ||
exports.vue_router = { | ||
v4: this.lib2esm(vue_router_v4.name, vue_router_v4.members), | ||
}; | ||
exports.vue = { | ||
v2: this.lib2esm(vue_v2.name, vue_v2.members), | ||
v3: this.lib2esm(vue_v3.name, vue_v3.members), | ||
}; | ||
exports.vuex = { | ||
v3: this.lib2esm(vuex_v3.name, vuex_v3.members), | ||
v4: this.lib2esm(vuex_v4.name, vuex_v4.members), | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'Pinia', | ||
@@ -6,0 +6,0 @@ members: [ |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'ReactDOM', | ||
@@ -19,2 +19,2 @@ members: [ | ||
], | ||
}; | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'ReactDOM', | ||
@@ -20,2 +20,2 @@ members: [ | ||
], | ||
}; | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'ReactRouterDOM', | ||
@@ -26,2 +26,2 @@ members: [ | ||
], | ||
}; | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'ReactRouterDOM', | ||
@@ -45,2 +45,2 @@ members: [ | ||
], | ||
}; | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'ReactRouter', | ||
@@ -6,0 +6,0 @@ members: [ |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'ReactRouter', | ||
@@ -37,2 +37,2 @@ members: [ | ||
], | ||
}; | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'React', | ||
@@ -36,2 +36,2 @@ members: [ | ||
], | ||
}; | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'React', | ||
@@ -43,2 +43,2 @@ members: [ | ||
], | ||
}; | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'Redux', | ||
@@ -14,2 +14,2 @@ members: [ | ||
], | ||
}; | ||
}; |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'VueRouter', | ||
@@ -6,0 +6,0 @@ members: [ |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'Vue', | ||
@@ -6,0 +6,0 @@ members: [ |
/** | ||
* @type {import('.').LibMeta} | ||
*/ | ||
module.exports = { | ||
module.exports = { | ||
name: 'Vue', | ||
@@ -6,0 +6,0 @@ members: [ |
@@ -19,2 +19,2 @@ /** | ||
], | ||
}; | ||
}; |
129
README.md
@@ -24,3 +24,3 @@ # vite-plugin-resolve | ||
You can load any code snippet you want | ||
You can load any code snippets you want **(ESM format)** | ||
@@ -36,4 +36,7 @@ ```ts | ||
const vue = window.Vue; | ||
export { vue as default } | ||
export const version = vue.version; | ||
const version = vue.version; | ||
export { | ||
vue as default, | ||
version, | ||
} | ||
`, | ||
@@ -47,3 +50,2 @@ // Node.js, Electron | ||
} | ||
// ...others | ||
`, | ||
@@ -53,41 +55,39 @@ }), | ||
} | ||
``` | ||
In you App | ||
```ts | ||
import Vue from 'vue' | ||
// Use in your app | ||
import Vue, { version } from 'vue' | ||
import { ipcRenderer, shell } from 'electron' | ||
``` | ||
**Load a file** | ||
You can easy to use `lib2esm()` to customize some things | ||
Support nested module id, support return Promise | ||
```js | ||
import resolve, { lib2esm } from 'vite-plugin-resolve' | ||
export default { | ||
plugins: [ | ||
resolve({ | ||
// Let's use lodash as an example | ||
lodash: lib2esm( | ||
// lodash iife name | ||
'_', | ||
// export memebers | ||
[ | ||
'chunk', | ||
'curry', | ||
'debounce', | ||
'throttle', | ||
], | ||
), | ||
}), | ||
] | ||
} | ||
```ts | ||
import fs from 'fs' | ||
resolve({ | ||
'path/filename': () => fs.promise.readFile('path', 'utf-8'), | ||
}) | ||
// Use in your app | ||
import { chunk, curry, debounce, throttle } from 'lodash' | ||
``` | ||
## API | ||
**Use in Electron** 👉 [electron-vite-vue](https://github.com/electron-vite/electron-vite-vue/blob/main/packages/renderer/vite.config.ts) | ||
`resolve(entries)` | ||
## Builtin modules | ||
**entries** | ||
```ts | ||
{ | ||
[moduleId: string]: | ||
| ReturnType<Plugin['load']> | ||
| ((...args: Parameters<Plugin['load']>) => ReturnType<Plugin['load']>) | ||
} | ||
``` | ||
You can see the return value type definition here [rollup/types.d.ts#L272](https://github.com/rollup/rollup/blob/b8315e03f9790d610a413316fbf6d565f9340cab/src/rollup/types.d.ts#L272) | ||
## Use builtin modules | ||
This like Vite external plugin | ||
@@ -117,3 +117,6 @@ | ||
resolve({ | ||
// e.g. | ||
// external-lib: lib-name.version | ||
vue: vue.v3, | ||
react: react.v18, | ||
}), | ||
@@ -125,35 +128,45 @@ ] | ||
import { h, ref, reactive, watch } from 'vue' | ||
import { useState, useEffect } from 'react' | ||
``` | ||
**Advance**, you can use `lib2esm()` to customize some things | ||
## API | ||
```js | ||
import resolve from 'vite-plugin-resolve' | ||
import { lib2esm } from 'vite-plugin-resolve/presets' | ||
`resolve(entries)` | ||
export default { | ||
plugins: [ | ||
resolve({ | ||
// Let's use lodash as an example | ||
lodash: lib2esm( | ||
// lodash iife name | ||
'_', | ||
// export memebers | ||
[ | ||
'chunk', | ||
'curry', | ||
'debounce', | ||
'throttle', | ||
], | ||
), | ||
}), | ||
] | ||
```ts | ||
type entries = { | ||
[moduleId: string]: | ||
| ReturnType<Plugin['load']> | ||
| ((...args: Parameters<Plugin['load']>) => ReturnType<Plugin['load']>) | ||
} | ||
``` | ||
// Use in your app | ||
import { chunk, curry, debounce, throttle } from 'lodash' | ||
*You can see the return value type definition here [rollup/types.d.ts#L272](https://github.com/rollup/rollup/blob/b8315e03f9790d610a413316fbf6d565f9340cab/src/rollup/types.d.ts#L272)* | ||
`lib2esm(name[,members[,options]])` | ||
```ts | ||
export interface Lib2esmOptions { | ||
/** | ||
* Generate code snippet format | ||
* | ||
* e.g. | ||
* ```js | ||
* const _M_ = require('lib') // cjs | ||
* const _M_ = window['lib'] // iife | ||
* ``` | ||
* | ||
* @default "iife" | ||
*/ | ||
format?: 'cjs' | 'iife', | ||
} | ||
export interface Lib2esm { | ||
(name: string): string | ||
(name: string, options: Lib2esmOptions): string | ||
(name: string, members: string[]): string | ||
(name: string, members: string[], options: Lib2esmOptions): string | ||
} | ||
export declare const lib2esm: Lib2esm | ||
``` | ||
**Use in Electron** 👉 [electron-vite-vue](https://github.com/electron-vite/electron-vite-vue/blob/main/packages/renderer/vite.config.ts) | ||
## What's different from the official Demo? | ||
@@ -160,0 +173,0 @@ |
@@ -12,3 +12,3 @@ # vite-plugin-resolve | ||
🤔 你可以认为它是官方教程的一个实现 👉 [Virtual Modules Convention](https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention) | ||
📦 **开箱即用**, 内置 Vue, React, Antd, Element 等等 | ||
📦 **开箱即用**, 内置 Vue, React, Antd, Element 等等 | ||
🌱 支持自定义 external 代码段 | ||
@@ -56,3 +56,3 @@ ✅ Browser, Node.js, Electron | ||
```ts | ||
import Vue from 'vue' | ||
import Vue, { version } from 'vue' | ||
import { ipcRenderer, shell } from 'electron' | ||
@@ -111,3 +111,2 @@ ``` | ||
} from 'vite-plugin-resolve/presets' | ||
export default { | ||
@@ -120,3 +119,2 @@ plugins: [ | ||
} | ||
// 使用 | ||
@@ -131,3 +129,2 @@ import { h, ref, reactive, watch } from 'vue' | ||
import { lib2esm } from 'vite-plugin-resolve/presets' | ||
export default { | ||
@@ -151,3 +148,2 @@ plugins: [ | ||
} | ||
// 使用 | ||
@@ -159,2 +155,3 @@ import { chunk, curry, debounce, throttle } from 'lodash' | ||
## 这与官方的 Demo 有何异同? | ||
@@ -161,0 +158,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
44483
172
0
1816