If you want to create a standalone HTML file that includes JS, CSS, and images directly, you can utilize the HTML Bundler Plugin for Webpack. This plugin is capable of embedding assets within the HTML itself.
For instance, let's consider a basic Vue application with SCSS, TypeScript, and image files:
index.html
<!doctype html>
<html lang="en">
<head>
<link href="./favicon.ico" rel="icon" />
<link href="./main.scss" rel="stylesheet" />
<title>Vue</title>
</head>
<body>
<div id="app">
<h1>{{ title }}</h1>
<img src="./picture.png" />
<my-button></my-button>
</div>
<script src="./index.ts"></script>
</body>
</html>
index.ts
import { createApp, ref } from 'vue';
import MyButton from './MyButton.vue';
import './styles.scss';
createApp({
setup() {
return {
title: ref('Hello Vue!'),
};
},
})
.component('my-button', MyButton)
.mount('#app');
MyButton.vue
<template>
<button>{{ text }}</button>
</template>
<script setup>
import { ref } from 'vue';
const text = ref('Button');
</script>
<style src="./MyButton.scss" lang="scss"></style>
A simple configuration for Webpack could look like this:
const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
module.exports = {
mode: 'production',
output: {
path: path.join(__dirname, 'dist/'),
},
resolve: {
alias: {
vue: 'vue/dist/vue.esm-bundler',
},
extensions: ['.ts', '...'],
},
plugins: [
new VueLoaderPlugin(),
new HtmlBundlerPlugin({
entry: {
index: './src/views/index.html',
},
js: {
filename: '[name].[contenthash:8].js',
inline: true,
},
css: {
filename: '[name].[contenthash:8].css',
inline: true,
},
minify: 'auto',
}),
],
module: {
rules: [
{
test: /\.vue$/i,
use: ['vue-loader'],
},
{
test: /\.(css|scss)$/,
use: ['css-loader', 'sass-loader'],
},
{
test: /\.(ico|png|jp?
The resulting generated HTML file located at `dist/index.html` will contain inlined CSS, JS, and images.
Explore a live demonstration on StackBlitz by clicking here.
Access the full source code on GitHub here.