splitbrain/php-jsstrip

基于PHP的JavaScript最小化工具

1.0.1 2023-03-12 10:23 UTC

This package is auto-updated.

Last update: 2024-09-12 13:19:12 UTC


README

这是Nick Galbreath的python工具jsstrip.py的PHP端口。

它最初于2006年被移植到PHP,作为DokuWiki维基引擎的一部分。多年来它得到了多次改进,现在作为一个独立的库可用。

引用原始描述

jsstrip是一个开源库,用于从JavaScript文件中移除空白和注释。你可能想这样做以优化大小和性能,或者使文件更难阅读。它通常可以节省文件大小的30-40%。

警告

jsstrip不是一个真正的JavaScript解析器。它假设你已经正确地使用分号 (;) 标识了'行结束'。

  • print 'foo'; print 'bar';
  • print 'foo' print 'bar'

你需要先转换你的代码以使用分号 (;)。

始终在生产部署前测试删除后的版本。

安装

通过composer安装

composer require splitbrain/php-jsstrip

使用

<?php

require_once 'vendor/autoload.php';

$js = file_get_contents('somefile.js'); // gather your JS here somehow

$minifiedJS = (new \splitbrain\JSStrip\JSStrip())->compress($js);

跳过最小化

你可以通过特殊注释包围代码部分来跳过部分代码的最小化。

/* BEGIN NOCOMPRESS */
const foo = 'No compression here'; // this comment will also stay
/* END NOCOMPRESS */