Source Code: https://github.com/mochamadboval/multipage-vite-vanilla-js
Main Configuration
Create a Vite Vanilla JavaScript project.
npm create vite@latest multipage-vite-vanilla-js -- --template vanilla cd multipage-vite-vanilla-js npm i
Customize the project folder structure as follows.
|- node_modules/ |- src/ |- assets/ |- img/ |- javascript.svg |- js/ |- counter.js |- public/ |- vite.svg |- index.html |- main.js |- style.css |- .gitignore |- package-lock.json |- package.json
Since the index.html is no longer in the main project folder, the npm run dev cannot be executed. Therefore, create a vite.config.js file and set the new root path to the src folder. This only applies when the npm run dev and npm run build commands are executed.
// vite.config.js export default { root: "./src", };
Make some adjustments as follows and move the CSS import method to HTML.
// ./src/main.js import { setupCounter } from "./assets/js/counter"; import viteLogo from "/vite.svg"; import javascriptLogo from "./assets/img/javascript.svg"; document.querySelector("#app").innerHTML = ` <div> ... ... <a href="/about">About</a> | <a href="/blog/article">Article</a> </div> `; setupCounter(document.querySelector("#counter"));
<!-- ./src/index.html --> <title>Vite App</title> <link rel="stylesheet" href="/style.css">
Now we can execute npm run dev command. However, when running npm run build the dist folder will be created in the src folder. To move it out of that folder add the following configuration.
// vite.config.js export default { root: "./src", build: { outDir: "../dist", emptyOutDir: true, }, };
We can still use the npm run preview command to run the dist folder because root: "./src" doesn't affect it (it still points to the main project folder).
Next, let's create about.html in the src folder and article.html in the blog folder.
<!-- ./src/about.html --> <meta charset="UTF-8"> <link rel="icon" type="image/svg+xml" href="/vite.svg"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>About | Multipage Vite Vanilla JavaScript</title> <link rel="stylesheet" href="/style.css"> <div> <h1 id="About-page">About page.</h1> </div>
<!-- ./src/blog/article.html --> <meta charset="UTF-8"> <link rel="icon" type="image/svg+xml" href="/vite.svg"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Article | Multipage Vite Vanilla JavaScript</title> <link rel="stylesheet" href="/style.css"> <div> <h1 id="Article-page">Article page.</h1> </div>
Then, add the following configuration so that both files are also created in the dist folder.
// vite.config.js export default { root: "./src", build: { outDir: "../dist", emptyOutDir: true, rollupOptions: { input: { index: "./src/index.html", about: "./src/about.html", article: "./src/blog/article.html", }, }, }, };
Now the project is successfully multipage.
However, you might have noticed that we need to add a new path in input every time we create a new HTML file. If you want this to work dynamically, let's install the glob package and add the following configuration.
npm install -D glob
// vite.config.js import { sync } from "glob"; export default { ... build: { ... rollupOptions: { input: sync("./src/**/*.html".replace(/\\/g, "/")), }, }, };
Things that can be improved and added:
'404 Not Found' page
When we write the wrong url, the page displayed is the main page. We can make it show the default 'Not Found' page.
// vite.config.js export default { appType: "mpa", root: "./src", ...
If we deploy the project to Netlify, we can easily redirect the default 'Not Found' page to our own 404 page.
Create 404.html in the src folder and _redirects file (without extension) in the public folder.
<!-- ./src/404.html --> <meta charset="UTF-8"> <link rel="icon" type="image/svg+xml" href="/vite.svg"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Page not found | Multipage Vite Vanilla JavaScript</title> <link rel="stylesheet" href="/style.css"> <div> <h1 id="Page-not-found">Page not found.</h1> </div>
_redirects /* /404.html 200
Minify HTML
We can minify all HTML files during the build process using this plugin (let me know if I can do it without the plugin).
npm install -D vite-plugin-minify
// vite.config.js ... import { ViteMinifyPlugin } from "vite-plugin-minify"; export default { plugins: [ViteMinifyPlugin()], appType: "mpa", ...
Create Path Aliases
// vite.config.js ... import { resolve } from "path"; export default { resolve: { alias: { "@js": resolve(__dirname, "src/assets/js"), }, }, plugins: [ViteMinifyPlugin()], ...
// ./src/main.js import { setupCounter } from "@js/counter"; import viteLogo from "/vite.svg"; ...
To enable auto-suggestion in the text editor (VS Code), create a jsconfig.json file and add this configuration.
// jsconfig.json { "compilerOptions": { "paths": { "@js/*": ["./src/assets/js/*"] } } }
Install TailwindCSS
You can follow the official documentation.
npm install -D tailwindcss postcss autoprefixer npx tailwindcss init
// tailwind.config.js /** @type {import('tailwindcss').Config} */ export default { content: ["./src/**/*.{html,js}"], theme: { extend: {}, }, plugins: [], };
// poscss.config.js export default { plugins: { tailwindcss: {}, autoprefixer: {}, }, };
/* ./src/style.css */ @tailwind base; @tailwind components; @tailwind utilities; root: { ...
To automatically sort TailwindCSS classes when using Prettier, you can follow this documentation.
npm install -D prettier prettier-plugin-tailwindcss
// .prettierrc { "plugins": ["prettier-plugin-tailwindcss"] }
Hope it helps you :)
Source:
- https://vitejs.dev/guide/#scaffolding-your-first-vite-project
- https://vitejs.dev/guide/build.html#multi-page-app
- https://stackoverflow.com/a/66877705
- https://github.com/isaacs/node-glob
- https://www.npmjs.com/package/vite-plugin-minify
The above is the detailed content of Multipage Vite Vanilla JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools
