vite-for-wp icon indicating copy to clipboard operation
vite-for-wp copied to clipboard

`prepare_asset_url` can be fooled if the CWD is symlinked to `wp-content/plugins`

Open lolcabanon opened this issue 5 months ago • 0 comments

Hey! Awesome work here.

I just wanted to raise an issue in case we can improve the code or if someone else faces a similar problem.

I had the issue while using the https://github.com/kucrut/vite-for-wp-example-svelte example, but the issue is related to this repo's code.

So here's the issue I've got :

I have the habit to work in my ~/DEV directory and to symlink relevant folders to the WordPress plugin directory. With npm run dev everything is fine, but with a prod build the URL of the main script is wrongly created.

On this line, since I am working from another directory (ex: /home/USER/DEV/wp-plugin-dev), the $manifest_dir has no overlap with $content_dir so nothing get replaced.

  // vite-for-wp.php
  $url = content_url(str_replace($content_dir, '', $manifest_dir));

The computed URL looks like this : https://wp.dev.local/wp-content/home/USER/DEV/wp-plugin-dev/app/dist but should instead look like this : https://wp.dev.local/wp-content/wp-plugin-dev/app/dist.

Just to be clear, putting the actual root of the project in the WordPress plugins directory, so this issue should not really happen in real productions environments.

How to reproduce

  1. Clone and link the repo
cd ~/DEV # or any folder you want

git clone https://github.com/kucrut/vite-for-wp-example-svelte

ln -s . ~/DEV/dev_wordpress 
  1. composer install && npm install && npm run build
  2. Activate the plugin in WordPress
  3. Look into the Network tab into the inspector => the request for main-XXXXX.js is 404

Hope it's clear enough!

lolcabanon avatar Sep 06 '24 23:09 lolcabanon