Skip to content

Commit

Permalink
终于写完了
Browse files Browse the repository at this point in the history
  • Loading branch information
censujiang committed May 25, 2023
1 parent f64b655 commit 3b24f9b
Show file tree
Hide file tree
Showing 11 changed files with 714 additions and 20 deletions.
5 changes: 3 additions & 2 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"dev": "vite --open",
"build": "vue-tsc && vite build",
"preview": "vite preview"
},
Expand All @@ -14,7 +14,8 @@
"devDependencies": {
"@vitejs/plugin-vue": "^4.1.0",
"typescript": "^5.0.2",
"vite": "^4.3.2",
"vite": "^4.3.8",
"vite-plugin-global-const": "file:../",
"vue-tsc": "^1.4.2"
}
}
5 changes: 5 additions & 0 deletions demo/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<script setup lang="ts">
import {ref} from 'vue'
import HelloWorld from './components/HelloWorld.vue'
console.log(import.meta.env)
const env = ref(import.meta.env)
</script>

<template>
Expand All @@ -11,6 +15,7 @@ import HelloWorld from './components/HelloWorld.vue'
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
</div>
{{ env }}
<HelloWorld msg="Vite + Vue" />
</template>

Expand Down
10 changes: 9 additions & 1 deletion demo/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { globalConst } from 'vite-plugin-global-const'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
plugins: [
globalConst({
Text: 'Hello World!',
MAIN: true
}),

vue(),
],
})
443 changes: 443 additions & 0 deletions demo/yarn.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function globalConst(config) {
return {
name: 'vite-plugin-global-const',
config() {
console.log(config);
const define = {};
for (const key in config) {
define[`import.meta.env.${key}`] = JSON.stringify(config[key]);
}
return {
define,
};
},
};
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Define constants for your project to facilitate reuse of your code across multiple products",
"main": "/esm/index.js",
"repository": "https://github.com/censujiang/vite-plugin-global-const.git",
"homepage":"https://github.com/censujiang/vite-plugin-global-const",
"homepage": "https://github.com/censujiang/vite-plugin-global-const",
"bugs": {
"url": "https://github.com/censujiang/vite-plugin-global-const/issues"
},
Expand All @@ -28,10 +28,13 @@
"types": "types",
"scripts": {
"build": "tsc",
"demo:reload": "node ./scripts/demo-reload.js",
"release": "node ./scripts/release.js"
},
"devDependencies": {
"@types/node": "^20.2.3",
"shelljs": "^0.8.5",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"vite": "^4.3.8"
}
}
16 changes: 16 additions & 0 deletions scripts/demo-reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import shell from 'shelljs';

//执行一遍npm run build
shell.exec('yarn run build');

//删除demo/node_modules的所有文件
shell.rm('-rf', 'demo/node_modules');

//进入demo目录
shell.cd('demo');

//安装demo/node_modules
shell.exec('yarn');

//启动demo
shell.exec('yarn run dev');
34 changes: 19 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
//使用这个函数挂载到vite上,可以在任何地方使用
export function globalConst(){
import { Plugin } from 'vite';

interface Config {
[key: string]: any;
}

export function globalConst(config: Config): Plugin {
return {
name:'vite-plugin-global-const',
//挂载后接收用户传入的参数
config(config){
return {
define:{
//这里的key就是用户传入的key,value就是用户传入的value
'process.env':{
...config
}
}
name: 'vite-plugin-global-const',
config() {
console.log(config);
const define = {};
for (const key in config) {
define[`import.meta.env.${key}`] = JSON.stringify(config[key]);
}
}
}
}
return {
define,
};
},
};
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"paths": {
"vite":["node_modules/vite/dist/index.esm.js"]
},
"baseUrl": "./",
"noImplicitAny": false,
"rootDir": "./src",
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Plugin } from 'vite';
interface Config {
[key: string]: any;
}
export declare function globalConst(config: Config): Plugin;
export {};
Loading

0 comments on commit 3b24f9b

Please sign in to comment.