femundfilou/kirby-vite 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

femundfilou/kirby-vite

最新稳定版本:0.1.1

Composer 安装命令:

composer require femundfilou/kirby-vite

包简介

Use Vite Frontend Tooling in Kirby.

关键字:

README 文档

README

A Kirby CMS Plugin to use scripts and styles generated by Vite.

Why

This plugin is built through a personal need to use vite as frontend tooling. While there are some good plugins for the same usecase out there already, I ran in some issues when using svelte components. My build output consisted of multiple javascript and stylesheet files deriving from differnent svelte components. While the javascript import is handled internally, the css entries would not be rendered. That's why this plugin has a built in method to generate link tags for all non-entry styleheets coming from vite.

Popular other plugins

There are some other plugins for the same use case, have a look at them.

Installation

composer require femundfilou/kirby-vite

Setup

This plugin comes with two helper functions to render your assets.

<!DOCTYPE html>
<html>
  <head>
    <?php
    /**
     * In production, this will generate a link tag with the main options entry. In development, it will return nothing.
     * */
    echo vite()->css();

    /**
     * Optionally: You can use different templates per page to split up css. This will generate the link tag for the given template.
     * */
    $template = $page->template();
    $entry = "frontend/templates/$template/index.ts";
    echo vite()->css($entry);
    /**
     * Optionally: Adding null as argument will render all css files from manifest.json that are not marked as [ 'isEntry' => true ]
     * */
    echo vite()->css(null);
    ?>
  </head>
  <body>
  <main>...</main>
  <?php
    /**
     * In production, this will generate a script tag with the main options entry. In development, it will create two script tags, one for vite client server and one for the main entry.
     * */
    echo vite()->js();

    /**
     * Optionally: You can use different templates per page to split up js.
     * This will generate the script tag for the given template.
     * */
    $template = $page->template();
    $entry = "frontend/templates/$template/index.ts";
    echo vite()->js($entry);
    ?>
  </body>
</html>

Options + Defaults

This plugin is meant to be used with laravel valet, a really nice local development tool, hence the default server adress. You can of course change it to your liking, but is must be the same setting as in your vite.config.js.

If you use valet, you can add a proxy to your typical localhost:3000 to get a universal vite domain with ssl like so valet proxy vite http://localhost:3000 --secure

Default configuration

You can override this in your site/config.php.

return [
  'femundfilou.vite' => [
    'main' => "frontend/index.ts", // Main vite entry
    'manifest' => 'manifest.json', // Path to manifest, relative from root
    'server' => 'https://vite.test:3000', // Server used in development
    'dev' => false // Toggle development on / off
  ]
];

Automatically switch development & production

My recommended way to switch from development to production automatically is by a using .env variable.

  1. Install vlucas/phpdotenv
composer require vlucas/phpdotenv
  1. Create .env file in root

  2. Load .env in index.php

use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
  1. Use it in site/config.php
return [
  'femundfilou.vite' => [
    'dev' => $_ENV["MODE"] === "dev"
  ]
];
  1. Now you need a way to update the .env file, e.g. through pipeline or while running npm, depending on your system for example like this:
{
  "scripts": {
    "dev": "sed -i '' -e 's/production/dev/g' path/to/.env && vite",
    "build": "sed -i '' -e 's/dev/production/g' path/to/.env && vite build"
  }
}

Legacy Mode

Currently this plugin only generates type="module" scripts, so there is no support for vite legacy mode.

统计信息

  • 总下载量: 2.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固