sammyjo20 / lasso
Lasso - 简化 Laravel 资产管理的工具。
Requires
- php: ^8.1
- ext-json: *
- ext-zip: *
- illuminate/console: ^10.0 || ^11.0
- illuminate/filesystem: ^10.0 || ^11.0
- illuminate/support: ^10.0 || ^11.0
- league/flysystem: ^3.0
- symfony/finder: ^6.0 || ^7.0
- symfony/process: ^6.0 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.1.0
- orchestra/testbench: ^8.0 || ^9.0
- pestphp/pest: ^2.25
- phpstan/phpstan: ^1.10
- spatie/ray: ^1.33
- v3.3.0
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.0
- v3.0.x-dev
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.x-dev
- v2.0.3
- v2.0.2
- v2.0.1
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.0
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.3
- v1.0
- v0.1.99
- v0.1.98
- v0.1.97
- v0.1.96
- v0.1.95
- v0.1.94
- v0.1.93
- v0.1.92
- v0.1.91
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-upgrade/laravel-11
- dev-master
This package is auto-updated.
Last update: 2024-09-06 12:29:50 UTC
README
Lasso
简化 Laravel 资产管理的工具。
简介
在 Laravel 中部署资产可能是一个噩梦。开发者面临的一个问题是处理他们的构建资产(由 Webpack/Laravel Mix 或 Vite 创建)。你是否将它们存储在版本控制中?你是否在服务器上部署它们?如果我是在一个团队中工作呢?这些资产解决方案中的每一个都可能给开发者带来头痛,包括合并冲突和减缓服务器速度。
Lasso 是一个为简化将资产部署到您的服务器而设计的 Laravel 包。它在负载均衡环境中也运行良好。
Lasso 做了什么?
Lasso 在您的本地机器或持续集成(例如 GitHub Actions)中编译您的资产,然后将资产上传到您的一个 Laravel 文件系统中。这利用了您本地机器的强大功能,这比服务器可能要强大得多。
在部署期间,Lasso 将从文件系统下载您的资产。它使用 Git 来跟踪最后一个创建的资产包,并自动清理旧包。
安装
Lasso 支持 Laravel 9+ 和 PHP 8.1 或更高版本。要安装 Lasso,只需运行以下 composer require 命令
composer require sammyjo20/lasso "^3.0"
之后,运行以下命令以创建 lasso.php 配置文件
php artisan vendor:publish --tag=lasso-config
配置
现在请确保在您的 app/config 目录中配置 lasso.php 配置文件。请确保指定 Lasso 要使用的文件系统磁盘。
如果您有多个项目,请确保更改 "upload_to" 路径,否则您可能在应用程序中遇到资产冲突。
设置
请确保将所有由 Webpack/Laravel Mix 创建的公共资产添加到您的 .gitignore 文件中。请确保还将 ".lasso" 目录添加到您的 .gitignore 文件中。
示例
mix-manifest.json
public/css/*
public/js/*
public/build/**/*
public/
.lasso
.lasso 目录是由 Lasso 创建的临时目录,用于在资产被压缩时保持资产。此目录将自动创建和删除,但出于谨慎,最好忽略此目录,以防 Lasso 在清理阶段之前崩溃。
Vite 支持
Lasso 支持通过一些配置更改 Vite。在 config/lasso.php
中,将 compiler.script
更改为 yarn run build
,将 compiler.excluded_files
更改为 ['hot']
以排除 Vite 的 "hot" 文件,并将 compiler.output
更改为 disable
(进度条输出尚未支持 Vite)。
'script' => 'yarn run build', 'output' => 'disable', 'excluded_files' => ['hot'],
推荐用法
Lasso 随附两个命令,应在您的项目/部署过程中使用。推荐用法是在本地机器上运行 "publish" 命令,这比持续集成或服务器编译更强大。
发布
当您想要上传/发布新资产到应用程序时,应执行 publish 命令。Lasso 将运行提供的脚本(例如 npm run production),然后压缩编译器创建的文件(例如 Webpack)。
php artisan lasso:publish
运行此命令后,Lasso 将在您的应用程序根目录中创建一个 "lasso-bundle.json" 文件。这是推荐的方法,因为当您提交文件时,Lasso 将使用它来下载与您的提交相关的最新捆绑包。如果您不使用 Git,例如,如果您在持续集成内编译资产,可以在命令中添加 --no-git
标志。
警告:使用 --no-git
标志时,版本控制将受到限制,因为 lasso-bundle.json 存储在您的文件系统中,而不是您的存储库中。使用 Git 是推荐的方法。
如果您使用 git,并且想要轻松跟踪哪个捆绑文件对应于哪个提交,请使用 --use-commit
标志。这将确保捆绑包的 zip 文件名是提交哈希的前 12 个字符。这还增加了在 CI 管道中发布捆绑包的优势,无需创建新的提交,同时提供了使用 Git 的版本控制优势。
拉取
然后应在您的部署脚本或需要资产的服务器上执行拉取命令。只需运行以下命令。如果您使用 Laravel Forge,请将此命令添加到您的部署脚本中。如果您使用 Laravel Envoyer,请将其添加到部署期间的钩子列表中。它应该在 每台服务器 上运行。
在拉取时使用 --use-commit
标志,如果您使用 --use-commit
标志发布捆绑包。
php artisan lasso:pull
在持续集成(CI)中使用,例如 Github Actions
要使用 Lasso 进行持续集成,建议运行 php artisan lasso:publish
命令,然后提交创建的 "lasso-bundle.json" 文件。如果您在 CI 过程中无法提交文件,请在命令中使用 --no-git
标志,例如:php artisan lasso:publish --no-git
阅读 Alex Justesen 撰写的关于如何将 Lasso 集成到 CI/CD 管道中的优秀博客文章:https://dev.to/alexjustesen/laravel-cicd-pipeline-w-lasso-and-github-actions-53gm
多个环境
默认情况下,Lasso 支持多环境应用程序。例如,如果您的应用程序有一个测试环境,您可以使用 LASSO_ENV
环境变量来设置当前环境。在您的 Web 服务器上
LASSO_ENV=staging
网络钩子
当命令执行成功时,Lasso 还可以触发网络钩子。只需在 lasso.php 配置文件中的 "webhooks" 数组中列出 URL。
/* * Lasso will can also trigger Webhooks after its commands have been * successfully executed. You may specify URLs that Lasso will POST * to, for each of the commands. */ 'webhooks' => [ /* * Specify which webhooks should be triggered after a successful * "php artisan lasso:publish" command execution. */ 'publish' => [ // ], /* * Specify which webhooks should be triggered after a successful * "php artisan lasso:pull" command execution. */ 'pull' => [ // ] ],
清理
Lasso 将自动尝试保持您的文件系统清洁,并将自动删除旧的捆绑包。您可以通过在 config/lasso.php 文件中设置 max_bundles
配置变量来增加/减少 Lasso 每个环境保留的捆绑包数量。
/* * Lasso will automatically version the assets. This is useful if you * suddenly need to roll-back a deployment and use an older version * of built files. You can set the maximum amount of files stored here. */ 'max_bundles' => 5,
排除的文件/目录
在发布过程中,Lasso 将复制 public 目录。如果您有任何希望在过程中忽略的文件或目录,请在 config/lasso.php 文件中的 excluded_files
和 excluded_directories
配置变量中指定它们。
/* * If there any directories/files you would like to Lasso to * exclude when uploading to the Filesystem, specify them below. */ 'excluded_files' => [], 'excluded_directories' => [],
感谢
特别感谢 @codepotato 为我们的标志!❤️
安全性
如果您发现任何安全相关的问题,请发送电子邮件至 29132017+Sammyjo20@users.noreply.github.com
就这样!✨
这是我第一个 Laravel 包,我真的希望它对您有用,如果您喜欢我的工作并且想表达一些喜爱,请考虑购买一些代码燃料(咖啡)❤