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

Add Vite support #303

Merged
merged 3 commits into from
Jan 5, 2024
Merged

Add Vite support #303

merged 3 commits into from
Jan 5, 2024

Conversation

czernika
Copy link
Contributor

@czernika czernika commented Aug 4, 2023

This PR adds Vite support if user wants to use it since Vite is already bundled with Acorn package.

I found that it almost working but this line causing the problem as it expects to return a string but instead uses \Roots\asset() function which returns Asset object therefore fails. I believe I cannot change Illuminate content so I just extend a new Vite class with Laravel's one and change one method

Usage example with laravel-vite-plugin with basic setup

// vite.config

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel('resources/js/app.js'),
    ],
});

Needs to define vite as default manifest

// config/assets.php

return [
    'default' => 'vite',

    'manifests' => [
        'theme' => [
           // ...
        ],
        'vite' => [
            'path' => get_theme_file_path('public'),
            'url' => get_theme_file_uri('public'),
            'bundles' => get_theme_file_path('public/build/manifest.json'),
        ],
    ]
];

and inject assets in the head (it is not using wp_enqueue_ functions) something like

// app/setup.php

add_action('wp_head', function () {
  echo app('assets.vite')('resources/js/app.js');
});

For a custom vite configuration (let's say I need to use another manifest filename) we still can use Laravel API for Vite

// vite.config.js

export default defineConfig({
    build: {
      manifest: 'custom-manifest.json'
    },
    plugins: [
        laravel('resources/js/app.js'),
    ],
});
// setup.php
add_action('wp_head', function () {
  echo app('assets.vite')->useManifestFilename('custom-manifest.json')('resources/js/app.js');
});

// config/assets.php
// Change manifest name
'bundles' => get_theme_file_path('public/build/custom-manifest.json'),

@Log1x Log1x self-assigned this Jan 5, 2024
@Log1x Log1x merged commit 2f1599b into roots:main Jan 5, 2024
2 checks passed
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

Successfully merging this pull request may close these issues.

2 participants