定制 bugo/sass-embedded-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

bugo/sass-embedded-php

最新稳定版本:0.3

Composer 安装命令:

composer require bugo/sass-embedded-php

包简介

A lightweight PHP bridge for the official Node.js Sass embedded package

README 文档

README

PHP Coverage Status

По-русски

Created for research purposes, not for use on production sites!

PHP wrapper for sass-embedded (Dart Sass via Node.js).

Allows compiling SCSS/SASS to CSS through PHP using Node.js and npm.

Requirements

  • PHP >= 8.2
  • Node.js >= 18

Installation via Composer

composer require bugo/sass-embedded-php

Usage examples

Compiling SCSS file

<?php
require __DIR__ . '/vendor/autoload.php';

use Bugo\Sass\Compiler;

$compiler = new Compiler();

$css = $compiler->compileFile(__DIR__ . '/assets/app.scss');

file_put_contents(__DIR__ . '/assets/app.css', $css);

echo "CSS compiled!\n";

Compiling SCSS from string

<?php
require __DIR__ . '/vendor/autoload.php';

use Bugo\Sass\Compiler;

$compiler = new Compiler();

$scss = '$color: red; body { color: $color; }';
$css = $compiler->compileString($scss);

echo $css;

Compiling string with options

<?php
require __DIR__ . '/vendor/autoload.php';

use Bugo\Sass\Compiler;

$compiler = new Compiler();

$scss = <<<'SCSS'
$color: #3498db;
$font-size: 14px;

body {
  font-size: $font-size;
  color: $color;
}
SCSS;

$css = $compiler->compileString($scss, [
    'compressed' => true,
    'sourceMap' => true,
]);

echo $css;

Catching compilation errors

require __DIR__ . '/vendor/autoload.php';

use Bugo\Sass\Exception;
use Bugo\Sass\Compiler;

$compiler = new Compiler();

$scss = <<<'SCSS'
$color: #e74c3c;
.foo { color: $color; }
SCSS;

try {
    echo $compiler->compileString($scss);
} catch (Exception $e) {
    echo "Compilation error: " . $e->getMessage();
}

Compiling SCSS file with change checking

require __DIR__ . '/vendor/autoload.php';

use Bugo\Sass\Exception;
use Bugo\Sass\Compiler;

$compiler = new Compiler();

try {
    $done = $compiler->compileFileAndSave(
        __DIR__ . '/assets/style.scss',
        __DIR__ . '/assets/style.css',
    );

    if ($done) {
        echo "CSS recompiled and saved.\n";
    } else {
        echo "No changes detected, skipped compilation.\n";
    }
} catch (Exception $e) {
    echo "Compilation error: " . $e->getMessage();
}

This method automatically checks if the source file has been modified since the last compilation and only compiles and saves if changes are detected.

Compiling file with source maps and compressed output

<?php
require __DIR__ . '/vendor/autoload.php';

use Bugo\Sass\Compiler;

$compiler = new Compiler();

$css = $compiler->compileFile(__DIR__ . '/assets/app.scss', [
    'sourceMap' => true,
    'includeSources' => true,
    'sourceMapPath' => __DIR__ . '/assets/',
    'style' => 'compressed',
]);

file_put_contents(__DIR__ . '/assets/app.css', $css);

echo "CSS compiled with source map!\n";

Options

Paths to bridge.js and Node are specified only through the constructor:

$compiler = new Compiler('/path/to/bridge.js', '/path/to/node');
Option Type Description Possible values
syntax string Input syntax 'scss' for SCSS, 'indented' or 'sass' for SASS
style string Output style 'compressed' or 'expanded'
sourceMap bool Generate source map true or false
includeSources bool Include source code in map true or false
sourceMapPath string URL to existing map or path for saving new

Options can be set either for the entire compiler at once or for a specific method separately:

$compiler->setOptions([
    'syntax' => 'indented',
    'minimize' => true,
    'sourceMap' => true,
]);

Advanced options

These options allow controlling additional aspects of Sass compilation.

Option Type Description Usage example
loadPaths array Array of paths for searching Sass imports ['./libs', './node_modules']
quietDeps bool Suppresses warnings from dependencies true
silenceDeprecations bool Suppresses warnings about deprecated functions true
verbose bool Enables verbose output of messages true
$compiler->setOptions([
    'loadPaths' => ['/path/to/custom/libs'],
    'quietDeps' => true,
    'silenceDeprecations' => true,
    'verbose' => true,
]);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-06