Skip to content

Commit

Permalink
feat: 修改配置
Browse files Browse the repository at this point in the history
  • Loading branch information
jianbing.chen committed Aug 3, 2024
1 parent 78fb31f commit 7762233
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 114 deletions.
29 changes: 2 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
},
})],
});
```

Expand Down
2 changes: 1 addition & 1 deletion dist/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -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): {};
45 changes: 3 additions & 42 deletions dist/cjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cjs/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -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): {};
23 changes: 3 additions & 20 deletions dist/esm/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esm/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -12,7 +12,8 @@
},
"unpkg": "dist/cjs/index.js",
"files": [
"dist"
"dist",
"package.json"
],
"license": "ISC",
"engines": {
Expand Down
22 changes: 3 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {};
Expand All @@ -42,8 +28,6 @@ export async function VitePluginExternals(
);
map[key] = varName;
}
// 如果未检测到配置变量,则直接返回
if (!Object.keys(map).length) return {};
return {
...viteExternalsPlugin(map, userOptions),
name: 'VitePluginExternalsNew',
Expand Down

0 comments on commit 7762233

Please sign in to comment.