From 7762233836dddc19a01d572fe1abb3093021e11d Mon Sep 17 00:00:00 2001 From: "jianbing.chen" Date: Sat, 3 Aug 2024 21:34:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 ++-------------------------- dist/cjs/index.d.ts | 2 +- dist/cjs/index.js | 45 +++---------------------------------------- dist/cjs/index.js.map | 2 +- dist/esm/index.d.ts | 2 +- dist/esm/index.js | 23 +++------------------- dist/esm/index.js.map | 2 +- package.json | 5 +++-- src/index.ts | 22 +++------------------ 9 files changed, 18 insertions(+), 114 deletions(-) diff --git a/README.md b/README.md index 19b9bf7..00b4723 100644 --- a/README.md +++ b/README.md @@ -19,16 +19,7 @@ import { defineConfig } from 'vite'; import { VitePluginExternals } from 'vite-plugin-externals-new'; export default defineConfig({ ... - plugins: [VitePluginExternals()], -}); -``` - -三、创建配置 - -1. 方法一(创建 external.config.js) - -```js -export default { + plugins: [VitePluginExternals({ vue: { src: 'https://unpkg.com/vue@3/dist/vue.global.js', varName: 'Vue', @@ -41,23 +32,7 @@ export default { defer: true, async: false, }, -}; -``` - -> `inject`默认为`head`,`defer`默认为`true` - -2. 方法二(添加 options 配置) - -```js -VitePluginExternals({ - vue: { - src: 'https://unpkg.com/vue@3/dist/vue.global.js', - varName: 'Vue', - }, - 'vue-router': { - src: 'https://unpkg.com/vue-router@4', - varName: 'VueRouter', - }, +})], }); ``` diff --git a/dist/cjs/index.d.ts b/dist/cjs/index.d.ts index 7fef939..a7ceeab 100644 --- a/dist/cjs/index.d.ts +++ b/dist/cjs/index.d.ts @@ -1,3 +1,3 @@ import { Options } from './types'; import { UserOptions } from 'vite-plugin-externals/dist/src/types'; -export declare function VitePluginExternals(options?: Options, userOptions?: UserOptions): Promise<{}>; +export declare function VitePluginExternals(options: Options, userOptions?: UserOptions): {}; diff --git a/dist/cjs/index.js b/dist/cjs/index.js index 6e4c8fd..b5ba5b5 100644 --- a/dist/cjs/index.js +++ b/dist/cjs/index.js @@ -4,57 +4,20 @@ Object.defineProperty(exports, '__esModule', { value: true }); var cheerio = require('cheerio'); var vitePluginExternals = require('vite-plugin-externals'); -var fs = require('fs'); -var path = require('path'); - -function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } - -function _interopNamespace(e) { - if (e && e.__esModule) return e; - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n["default"] = e; - return Object.freeze(n); -} - -var path__default = /*#__PURE__*/_interopDefaultLegacy(path); /** * vite-plugin-external * 1. 处理JS模块中导入方式,修改为 window['vue']方式 * 2. 自动把外部链接插入到HTML中 */ -async function VitePluginExternals() { - let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; +function VitePluginExternals(options) { let userOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; const keys = Object.keys(options); - const configRoot = process.cwd(); - const resolvePath = path__default["default"].resolve(configRoot, 'external.config.js'); + process.cwd(); // 判断 options 是否有值 if (!keys.length) { - try { - // 配置文件检测 - const info = fs.statSync(resolvePath); - } catch (error) { - throw new Error('external.config.js file does not exist'); - } - // 动态获取配置文件 - const configFile = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(resolvePath); - if (!configFile.default || typeof configFile.default !== 'object') { - throw new Error('external.config.js file is error'); - } // 覆盖默认配置 - options = configFile.default || {}; + return {}; } // 生成 external 配置 const map = {}; @@ -63,8 +26,6 @@ async function VitePluginExternals() { if (!varName) throw new Error('Vite plugin external missing configuration parameter varName'); map[key] = varName; } - // 如果未检测到配置变量,则直接返回 - if (!Object.keys(map).length) return {}; return { ...vitePluginExternals.viteExternalsPlugin(map, userOptions), name: 'VitePluginExternalsNew', diff --git a/dist/cjs/index.js.map b/dist/cjs/index.js.map index 7edda5d..ee83771 100644 --- a/dist/cjs/index.js.map +++ b/dist/cjs/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} \ No newline at end of file diff --git a/dist/esm/index.d.ts b/dist/esm/index.d.ts index 7fef939..a7ceeab 100644 --- a/dist/esm/index.d.ts +++ b/dist/esm/index.d.ts @@ -1,3 +1,3 @@ import { Options } from './types'; import { UserOptions } from 'vite-plugin-externals/dist/src/types'; -export declare function VitePluginExternals(options?: Options, userOptions?: UserOptions): Promise<{}>; +export declare function VitePluginExternals(options: Options, userOptions?: UserOptions): {}; diff --git a/dist/esm/index.js b/dist/esm/index.js index f948a37..8a97a12 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -1,34 +1,19 @@ import { load } from 'cheerio'; import { viteExternalsPlugin } from 'vite-plugin-externals'; -import { statSync } from 'fs'; -import path from 'path'; /** * vite-plugin-external * 1. 处理JS模块中导入方式,修改为 window['vue']方式 * 2. 自动把外部链接插入到HTML中 */ -async function VitePluginExternals() { - let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; +function VitePluginExternals(options) { let userOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; const keys = Object.keys(options); - const configRoot = process.cwd(); - const resolvePath = path.resolve(configRoot, 'external.config.js'); + process.cwd(); // 判断 options 是否有值 if (!keys.length) { - try { - // 配置文件检测 - const info = statSync(resolvePath); - } catch (error) { - throw new Error('external.config.js file does not exist'); - } - // 动态获取配置文件 - const configFile = await import(resolvePath); - if (!configFile.default || typeof configFile.default !== 'object') { - throw new Error('external.config.js file is error'); - } // 覆盖默认配置 - options = configFile.default || {}; + return {}; } // 生成 external 配置 const map = {}; @@ -37,8 +22,6 @@ async function VitePluginExternals() { if (!varName) throw new Error('Vite plugin external missing configuration parameter varName'); map[key] = varName; } - // 如果未检测到配置变量,则直接返回 - if (!Object.keys(map).length) return {}; return { ...viteExternalsPlugin(map, userOptions), name: 'VitePluginExternalsNew', diff --git a/dist/esm/index.js.map b/dist/esm/index.js.map index 127dd6b..3c936fe 100644 --- a/dist/esm/index.js.map +++ b/dist/esm/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} \ No newline at end of file diff --git a/package.json b/package.json index 915ac0f..c4c8a54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-externals-new", - "version": "1.5.2", + "version": "1.5.3", "description": "", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", @@ -12,7 +12,8 @@ }, "unpkg": "dist/cjs/index.js", "files": [ - "dist" + "dist", + "package.json" ], "license": "ISC", "engines": { diff --git a/src/index.ts b/src/index.ts index 42bf5e1..17aecb2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,32 +5,18 @@ */ import { BasicAcceptedElems, load } from 'cheerio'; import { viteExternalsPlugin } from 'vite-plugin-externals'; -import { statSync } from 'fs'; -import path from 'path'; import { Options } from './types'; import { UserOptions } from 'vite-plugin-externals/dist/src/types'; -export async function VitePluginExternals( - options: Options = {}, +export function VitePluginExternals( + options: Options, userOptions: UserOptions = {}, ) { const keys = Object.keys(options); const configRoot = process.cwd(); - const resolvePath = path.resolve(configRoot, 'external.config.js'); // 判断 options 是否有值 if (!keys.length) { - try { - // 配置文件检测 - const info = statSync(resolvePath); - } catch (error) { - throw new Error('external.config.js file does not exist'); - } - // 动态获取配置文件 - const configFile = await import(resolvePath); - if (!configFile.default || typeof configFile.default !== 'object') { - throw new Error('external.config.js file is error'); - } // 覆盖默认配置 - options = configFile.default || {}; + return {}; } // 生成 external 配置 const map: { [key: string]: string } = {}; @@ -42,8 +28,6 @@ export async function VitePluginExternals( ); map[key] = varName; } - // 如果未检测到配置变量,则直接返回 - if (!Object.keys(map).length) return {}; return { ...viteExternalsPlugin(map, userOptions), name: 'VitePluginExternalsNew',