定制 craftsnippets/static-file-manager 二次开发

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

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

craftsnippets/static-file-manager

最新稳定版本:3.1.1

Composer 安装命令:

composer require craftsnippets/static-file-manager

包简介

Keep your static files list in PHP configuration file.

README 文档

README

Requirements

This plugin requires Craft CMS 3.0.0-beta.23 or later.

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
    
  2. Then tell Composer to load the plugin:

     composer require craftsnippets/static-file-manager
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for asset manifest.

Overview

  • This plugin allows you to keep the list of CSS, JS and Favicon files in PHP configuration file instead of Twig templates.
  • You can inject files from config file into frontend site and into control panel. CSS and JS files will be injected into proper places - CSS files to end of <head> and JS files at end of <body>.
  • You can also include links to google fonts CSS files, like https://fonts.googleapis.com/css2?family=Montserrat:wght@100;400&display=swap - it will be included like other CSS files.
  • Plugin also allows for cache busting of files by appending URL parameter to their paths which contains their modification date. This means that files will be cache busted only if they were modified. Files from external servers won't be cache busted.
  • Plugin exposes endpoint that lists all static files in JSON format, so the list can be consumed by frontend build tools. This does not include google fonts files.

Usage

In order to use plugin, place this code into base template of your project. Remember that static files won't be inserted is there is no <body> or <head> tags in your template.

{% do craft.staticFileManager.outputFiles() %}

You can pass variable set to false to this function to cancel injection of assets into template:

{% do craft.staticFileManager.outputFiles(false) %}

Files list to inject is set in config/static-file-manager.php setting file, under filesList setting. To inject files into control panel, place them into cpFileList setting. All file paths are relative to the web root. Here is example config file:

<?php
return [
   'filesList' => [
        'some-file.css',
        'other-file.js',
   ],
   
   'cpFileList' => [
        'some-control-panel-styles.css',
   ],
   'faviconPath' => 'some-favicon.png',
   'cpFaviconPath' => 'some-control-panel-favicon.png',
];

You can use inline php function to dynamically define injected file name:

<?php
return [
   'filesList' => [
        function(){
            $language = Craft::$app->getSites()->currentSite->language;
            if($language == 'en'){
                    return 'english-file.js';
            }else{
                    return 'non-english-file.js';
            }
        }
   ]
];

Twig filter

You can manually bust cache of files within Twig templates using version filter:

<script src="{{'some_script.js'|version}}"></script>

JSON endpoint

Plugin exposes endpoint that returns asset list in JSON format. The list consists of two arrays - array with key css contains CSS files and array with key js contains Javascript files. Only files present on the filesystem are listed, so these from external servers are omitted from this list. List is available under [website url]/actions/static-file-manager URL, but you first need to enable it using exposeJsonList config setting.

Here's an example gulp task that loads list of Javascript files from endpoint and minifies them into one file. This config assumes that you use xampp and your project lives in htdocs directory.

var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
const https = require('http');

gulp.task('production', ['sass:production', 'uglify:production']);

gulp.task('uglify:production', function() {

// set endpoint url
let file_path = __dirname;
let explode = file_path.split('htdocs');
let url = 'http://localhost'+explode[1]+'/web/actions/static-file-manager';

// load data

https.get(url,(res) => {
    let body = "";

    res.on("data", (chunk) => {
        body += chunk;
    });

    res.on("end", () => {

        let json = JSON.parse(body);
        let js = json['js'];
        
        // gulp task
        return gulp.src(js)
        .pipe(concat('main.min.js'))
        .pipe(uglify())
        .on('error', swallowError)
        .pipe(gulp.dest('web/static'));

    });

});

});

Settings

Place these settings in config/static-file-manager.php file.

  • filesList - array with list of static files paths within web root directory (usually web directory). These files will be injected into frontend site.
  • cpFileList - array with list of static files within web root directory that will be injected into control panel.
  • faviconPath - path to favicon file.
  • cpFaviconPath - path to control panel favicon file.
  • bustCache - if files should be cache busted. Default: true.
  • exposeJsonList - if plugin should expose list of files in JSON format. Default: false.

Brought to you by Piotr Pogorzelski

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固