Skip to content

Commit

Permalink
新增defineConstCore导出函数
Browse files Browse the repository at this point in the history
  • Loading branch information
censujiang committed Jul 23, 2023
1 parent 1b951e8 commit d705999
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ and you will get the following results in the browser console or terminal:
true
```

## For Development Other Plugins

If you are developing a plugin, you can use the global constants in your plugin code, they are injected into the `import.meta.env` object.For example:

```js
import { defineConstCore } from 'vite-plugin-global-const'

export function customPlugin() {
return {
name: 'vite-plugin-custom',
config: () => {
const defineConst = defineConstCore({
YourConst: true
})
return {
define: {
...defineConst
}
}
}
}
}
```

## LAST

If you have any questions, please submit an issue, thank you for your support, and if you like this plugin, please give me a star, thank you!
12 changes: 8 additions & 4 deletions esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ export function globalConst(config) {
return {
name: 'vite-plugin-global-const',
config() {
const define = {};
for (const key in config) {
define[`import.meta.env.${key}`] = JSON.stringify(config[key]);
}
const define = defineConst(config);
return {
define,
};
},
};
}
function defineConst(config) {
const define = {};
for (const key in config) {
define[`import.meta.env.${key}`] = JSON.stringify(config[key]);
}
return define;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-global-const",
"version": "0.0.2",
"version": "0.0.3",
"type": "module",
"description": "Define constants for your project to facilitate reuse of your code across multiple products",
"main": "/esm/index.js",
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ export function globalConst(config: Config): Plugin {
return {
name: 'vite-plugin-global-const',
config() {
const define = {};
for (const key in config) {
define[`import.meta.env.${key}`] = JSON.stringify(config[key]);
}
const define = defineConstCore(config);
return {
define,
};
},
};
}


export function defineConstCore(config: Config) {
const define = {};
for (const key in config) {
define[`import.meta.env.${key}`] = JSON.stringify(config[key]);
}
return define;
}

0 comments on commit d705999

Please sign in to comment.