Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

htmlWebPackPlugin EJS to vite-plugin-html #156

Open
DanielRuf opened this issue Jul 24, 2024 · 0 comments
Open

htmlWebPackPlugin EJS to vite-plugin-html #156

DanielRuf opened this issue Jul 24, 2024 · 0 comments

Comments

@DanielRuf
Copy link

DanielRuf commented Jul 24, 2024

Coming from a setup with Vue CLI and webpack to Vite, the following template is not transformed:

<%
for (let i = 0; i < htmlWebpackPlugin.files.js.length; i++) {
	const file = htmlWebpackPlugin.files.js[i];
	if (!file.match(/mainCss.*\.js/)) {
%>
		<script src="/<%= file %>"></script>
<%
	}
}
%>

The old vue.config.js file:

/* jshint node: true, esversion: 6 */

const path = require('path');
const LicenseWebpackPlugin = require('license-webpack-plugin').LicenseWebpackPlugin;

module.exports = () => {
	const watch = process.env.npm_lifecycle_event === 'watch';
	const production = process.env.NODE_ENV === 'production';
	const config = {
		runtimeCompiler: true,
		outputDir: '../public/dist',
		publicPath: production || watch ? 'dist/' : '',
		configureWebpack: config => {
			config.entry.main =  './src/main.js';
			if (production) {
				config.plugins.push(new LicenseWebpackPlugin({
					perChunkOutput: false,
					skipChildCompilers: true,
				}));
			}
			config.performance = {
				hints: production ? false : 'warning'
			};
			config.devtool = production ? 'source-map' : 'eval-source-map';
		},
	};
	if (production || watch) {
		config.pages = {
			main: {
				filename: path.resolve(__dirname, '../public/dist/js.html'),
				template: 'public/js.html',
				entry: 'src/main.js',
				inject: false,
			},
			mainCss: {
				filename: path.resolve(__dirname, '../public/dist/css.html'),
				template: 'public/css.html',
				entry: 'src/main.js',
				inject: false,
			},
		};
	}
	return config;
};

The new vite.config.js file:

/* jshint node: true, esversion: 6 */

import path from 'path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { createHtmlPlugin } from 'vite-plugin-html'


// https://vitejs.dev/config/
export default defineConfig({
	plugins: [
		vue(),
		createHtmlPlugin({pages: [
				{
					filename: path.resolve(__dirname, '../public/dist/js.html'),
					template: 'public/js.html',
					entry: 'src/main.js',
					inject: false,
				},
				{
					filename: path.resolve(__dirname, '../public/dist/css.html'),
					template: 'public/css.html',
					entry: 'src/main.js',
					inject: false,
				}
			]})
	],
	resolve: {
		alias: {
			"@": path.resolve(__dirname, "./src"),
		},
	},
});

What steps are needed to get the asset injection working with this new plugin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant