donnikitos / vite-plugin-php

Vite's speed and tooling to preprocess PHP-files!
https://www.npmjs.com/package/vite-plugin-php
MIT License
38 stars 0 forks source link

Does not work with namespaces #34

Open rottmann opened 1 month ago

rottmann commented 1 month ago

Cause the generated code looks like

<link rel="stylesheet" crossorigin href="../../style-D00TQ7QE.css">
<script type="module" crossorigin src="../../index.php-DlZYXcJR.js"></script>
<link rel="modulepreload" crossorigin href="../../modulepreload-polyfill-B5Qt9EMX.js">
<?php
namespace MyNamespace;

namespace must be first.

A workaround could be a placeholder, that we can set within our code and vite-plugin-php insert (if it exists) at the placeholder.

And it would be great, if it could work with vites root parameter.

donnikitos commented 1 month ago

Hi @rottmann,

thanks for the submission! The Vite root-parameter is already earmarked in issue #10 - so it's on it's way :)


Can you clarify your use case with the namespace? As far as I understand you have a namespaced module with imports in it and during trasnpilation the imports are being set to the top of the file?

rottmann commented 1 month ago

One case is a custom autoloader, that uses namespaces. Pages / Layouts are in namespaces and the autoloader include the e.g. the layouts within pages. Only direct includes work currently.

A download page could only be done, if it includes only the download functionality (header() ...) and is not allowed to respond a custom page, e.g. if the user is not allowed to download. For this, it must be used a redirect workaround, that redirects to a separate page (because of the js/css headers). So we need a download.php and a download-not-allowed.php with the HTML response.