UI developer tutorials and guides

Tuesday, June 14, 2022

How to create global variable in vitejs [Solidjs]

 Vitejs is build tool for modern nodejs applications. Global variable requires in any application where common information need to be accessed in different modules. 

It is common practice is that store values such as URL, KEYS in separate files, in such cases we can use either environment variable or project level symbols.

In our Solidjs project [Vite project] we can configure variables (vite.config.js). Following example using a variable to store the API which is requires in different components.

export default defineConfig({

define:{
api:JSON.stringify('https://jsonplaceholder.typicode.com/posts')
}
,
plugins: [solidPlugin()],
build: {
target: 'esnext',
polyfillDynamicImport: false,
},
});

Here the define section is used to create variables, also note that the value should be in proper JSON format, so we have to stringify the value first.

In any module the api variable can consumed as follows

console.log(api)

 

0 comments:

Post a Comment