How import own js file into vite?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Effortlessly Incorporate Custom JavaScript Files into Vite's Environment
Body:
Introduction
Vite is an efficient frontend development tool for building modern web applications, offering significant improvements over older solutions like Webpack and Gulp. With its modular architecture and support for popular frameworks like Laravel, Vite has gained popularity among developers looking to streamline their workflows. However, as a newcomer seeking to integrate your personal JavaScript file with vanilla code into the Vite ecosystem, you may find it challenging to set up correctly. In this blog post, we will guide you through the process step-by-step.Adding JavaScript Files
Firstly, you need to create a new JavaScript file where your custom code resides, for example: 'resources/js/test.js'. It's essential to follow proper naming conventions and ensure that your scripts are organized in folders corresponding to their functionality or purpose.Setup the vite.config.js File
Secondly, you need to modify your 'vite.config.js' file to include your newly created JavaScript files. Here is a working example:laravel({
input: [
'resources/sass/app.scss',
'resources/js/app.js',
'resources/js/test.js', //this is my code
],
refresh: true,
}),
In this example, you can see that your custom file ('resources/js/test.js') is included along with the default app.scss and app.js files. The 'refresh' option ensures that any changes made to these files will be detected during development.
Importing External Libraries
To efficiently use external libraries in your custom JavaScript file, you can import them using ES modules by following a standard naming convention:import { libraryName } from 'libraryPath';
// Use the imported library
libraryName.method();
In this code snippet, you are importing a specific function or method from your chosen JavaScript library using ES6 syntax, which Vite supports by default. The 'libraryPath' should be updated to reflect the correct path to your library file.
Using Vue Component Libraries
For Vue-based libraries, you can use the 'defineComponent' method in your custom JavaScript file to register a new Vue component. Here is an example using the 'Vuetify' library:import { defineComponent } from '@vue/composition-api';
import Vuetify from 'vuetify';
export default defineComponent({
name: 'CustomComponent',
setup() {
return {
vuetifyInstance: Vuetify(),
};
},
});
In this example, you create a new custom component using the 'defineComponent' function and register it with your existing project. You can then access this component in your other files by importing and using it as necessary.