carbon/includeassets 问题修复 & 功能扩展

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

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

carbon/includeassets

Composer 安装命令:

composer require carbon/includeassets

包简介

Include your assets (css, js) in an easy way into Neos

README 文档

README

Latest stable version Total downloads License GitHub forks GitHub stars GitHub watchers

Carbon.IncludeAssets Package for Neos CMS

With this package, you get able to import all your CSS and Javascript assets with few lines of code in Settings.Carbon.yaml. The best practice is to include carbon/includeassets into your composer.json from your site package. After that, you can just add your settings. Besides the filenames, you are also able to pass all your necessary attributes to the tags. If you are not able to provide a file extension, you can force the type via (css), (js) or (mjs) at the end.

  • You can define multiple files from multiple packages.
  • You can pass the filenames as a string (comma separated) or as an array (recommended)
  • If you want to add attributes, add it with square brackets e.g. Filename.js[async data-prop data-attr="true" class="-js-loader"]
  • If you want to get a file included inline, add the attribute inline: e.g. Filename.css[inline]
  • You can add multiple resources per line. E. g. Slider.js,Main.css,Footer.css[async class="footer-styles"],Header[inline class="header-styles"],//ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js[async]
  • CSS can also be loaded asynchronously: Add async as an attribute, e.g. Filename.css[async]
  • If you want to add Google Fonts, you can write them down the fonts, e.g. Lato|Open+Sans:400,700 or Lato|Open+Sans:400,700[async]
  • On internal files, a hash from the content of the file gets appended. Please be aware that you have to clear the cache from Neos to update the hash value. It is meant to have a cache buster on production projects.
  • You can also give the browser some resource hints: Globally via the settings Carbon.IncludeAssets.ResourceHints or via adding a special type ((preloadasset), (preloadcss), (preloadscript) or (modulepreload)) at the end of a file entry.
  • You can also include the content of HTML files (e.g. Favicon.html). Usefull for copy and paste tracking codes, favicons, etc. HTML files are always read from the inline path and ignore all attributes. If they put in Head (with automatic sorting), the entries have to end with .html, htm or (html).

Structure of the Settings

In Carbon.IncludeAssets following settings are available:

Key  Description
ResourceHints (array) The setting, which global resource hints should be added.
Default (array) The default setting for a Packages entry. If a key is not set within a Packages entry, this value will be used

Structure of Packages entry

In Carbon.IncludeAssets.Packages you can define your packages, which should output assets. The keys get sorted first by numbers, then by characters. Like that, it is possible to define a load order for you packages. A single entry can have following entries (The Defaults are stored in Carbon.IncludeAssets.Default):

Key  Description
position (string or integer) By using position, it is possible to define the order. The position setting follows the same sorting logic used in Fusion (see the Fusion Reference).
Package (string) The package key. If it set to SitePackage, it will be replaced automatically with the package key from the site package. Defaults to SitePackage
CacheBuster (boolean) Append a hash value from the content of the file. Defaults to true
ConditionPrototype (string) If set, the files get only included if the fusion prototype returns a truthy value. Defaults to null
Wrapper (string) If set, the generated tags will be wrapped. {content} will be replaced with the tags. Example: '<!--[if lt IE 9]>{content}<![endif]-->'
Path (array) Define the files get loaded from. There are different paths for inline and linked files. Every type (css,js, mjs, html, preloadasset, preloadcss, preloadscript or modulepreload) can have a different path inside the Resources folder
General (array) Asset files who get loaded in live and backend view. Contains five entries: Head, HeadStart, HeadBotton, BodyStart and BodyBottom
Backend (array) Asset files that get loaded only in the backend view. Contains five entries: Head, HeadStart, HeadBotton, BodyStart and BodyBottom
Live (array) Asset files that get loaded only in the live view. Contains five entries: Head, HeadStart, HeadBotton, BodyStart and BodyBottom

Example

Here is a small example:

Carbon:
  IncludeAssets:
    ResourceHints:
      # Preconnect to these domains
      Preconnect:
        - "https://use.typekit.net"
        - "https://www.google-analytics.com"
        - "https://www.googletagmanager.com"

    Packages:
      # The keys get sorted first by numbers, then by characters. After that, by position
      Theme:
        position: start
        # Because no Package is defined, SitePackage from the default
        # will be used and will set to the site package.

        # Asset files which get loaded in live and backend view
        General:
          # These assets get loaded in the <head> (at the start)
          HeadStart:
            - Favicons.html

            # If a string starts with < and ends with an >, it get's inlucded as plain HTML
            - <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />

          # These assets get loaded in the <head> (at the end)
          HeadBottom: Logo.png[as="image"](preloadasset)

          # These asset gets sorted based on a talk by Harry Roberts Get Your Head Straight
          # https://speakerdeck.com/csswizardry/get-your-head-straight?slide=39
          Head:
            # Read the content of the html files and put it in correct place
            - File.html
            - AnotherFile.htm

            # Preload this Javascript
            - JsForLaterUse.js(preloadscript)

            # Preload this asset
            - Logo.png[as="image"](preloadasset)

            # Load this CSS asynchronous
            - GeneralHead.css[async]

            # If a file has the attribute `inline`, the file gets
            # inlined. Also, a different path is used. This path is
            # set in under `Carbon.IncludeAssets.Default.Path.Inline.CSS`
            # and `Carbon.IncludeAssets.Default.Path.Inline.JS`
            - AboveTheFold.css[inline]

            # Run this javascript after to document is ready
            - GeneralHead.js[defer]

            # Add this javascript as a module
            - JavascriptModule.mjs

            # External files can also be defined.
            # It has to start with //, https:// or http://
            - //foo.bar/externalfile.js

            # If you can't provide a proper file extension you can force the type:
            - FileWithCustomFileExtension.ext(css)

            # This works also with external files
            - //foo.bar/externalfile.php[async](js)

          # This assets get loaded at the start of the <body>
          BodyStart:
            - NoscriptWarning.html

          # This assets get loaded at the end of the <body>
          BodyBottom:
            # You can also pass all attributes you want
            - GeneralBody.js[async class='-js-loader']

            # You can also pass further attributes if you use the inline attribute
            - AdditionSpecialFancyTracking.js[inline class="-js-tracking"]

        # Asset files which get loaded only in the backend
        Backend:
          # Arrays can also be defined like this
          Head: ["BackendHead.css", "BackendHead.js[inline]"]

        # Asset files which get loaded only in the live view
        Live:
          # You can set the value as a string, it will be automatically converted to an array
          Head: SingleLive.css
          BodyBottom: LiveBody.css,LiveBody.js[inline]

      # If you want to add a cached package (for dynamically updates)
      "Foo.Bar.Cached":
        Cached: true
        # In cached mode you have to set always the package name
        Package: "Foo.Bar"
        General:
          Head: Main.css

      # Example taken from Jonnitto.Plyr
      "zz_Jonnitto.Plyr":
        Package: "Jonnitto.Plyr"

        # The files get only included if the fusion prototype
        # Jonnitto.Plyr:IncludeCase` return a truthy value
        ConditionPrototype: "Jonnitto.Plyr:IncludeCase"

        # Ajust the paths for external files
        Path:
          File:
            CSS: "Public"
            JS: "Public"

        # Set the files
        General:
          Head:
            - plyr.min.js[defer]
            - plyr.css

Take a look at the Settings.Carbon.yaml. There you see all the available options.

Fusion Prototypes

Basically, you have to folders with Fusion Prototypes: Internal and External. In the External folder you will find some prototypes which you can help you in your development:

Carbon.IncludeAssets:Case

This prototype is a small helper to write prototypes for the ConditionPrototype setting. Return true or false.

Property Description
mixin (string) The node type name of an mixin. Defaults to null
document (string) The node type name the document type. Defaults to this.mixin
content (string) The node type name the content type. Defaults to this.mixin
contentCollection (string) The filter for the content collection. Defaults to [instanceof Neos.Neos:ContentCollection]
documentNode (node) The node from the document. Defaults to documentNode
alwaysInclude (boolean) If true, the prototype return true. Defaults to Carbon.IncludeAssets:InBackend

Carbon.IncludeAssets:Collection

This prototype generates your script, style and link tags from the files which are defined in the property collection.

Property Description
collection (array with strings) The collection with the filenames. (Example: ['Main.css','Main.js[defer]']) Defaults to []
assetPackage (string) The name of the package. (Example: Jonnitto.Plyr) Defaults to null
cacheBuster (boolean) Append a hash value from the content of the file. Defaults to the value set in the Settings.Carbon.yaml
paths (array) The paths to the internal and external files inside the Resources folder. Defaults to the value set in the Settings.Carbon.yaml
wrapper (string) If set, the generated tags will be wrapped. {content} will be replaced with the tags. Example: '<!--[if lt IE 9]>{content}<![endif]-->'
cached (boolean) If true, every asset from this collection will get a own cache entry. Defaults to false. Can be used if you want to update an asset dynamically.

Carbon.IncludeAssets:File

The heart of this package. This prototype generates a script, style, and link tag. You can pass a file (with or without the path). Be aware that you can also pass the attributes like described on top. To force a type you can write (js), (css), (preloadasset), (preloadcss), (preloadscript) or (modulepreload) at the end of file.

Property Description
file (string) The filename. You have to write it in the same way as it would be defined in the Settings.yaml file. Defaults to null
assetPackage (string) The name of the package. (Example: Jonnitto.Plyr) Defaults to Carbon.IncludeAssets:SiteResourcesPackageKey
cacheBuster (boolean) Append a hash value from the content of the file. Defaults to the value set in the Settings.Carbon.yaml
assetPath (string) The path to the file inside the Resources folder. Per default, it is set dynamically
wrapper (string) If set, the tag will be wrapped. {content} will be replaced with the tag. Example: '<!--[if lt IE 9]>{content}<![endif]-->'
cached (boolean) If true, the asset will get a own cache entry. Defaults to false. Can be used if you want to update an asset dynamically.

Carbon.IncludeAssets:ResourceHints

This prototype renders the resource hints for the browser. Per default, this prototype read the Settings.Carbon.yaml. But you can also pass preloadNodes or prerenderNodes (Array, FlowQuery, or a single node) for further optimization.

Usage with Sandstorm.CookiePunch

If you have Sandstorm.CookiePunch installed, please add the following attribute to your script entry:

- YourJSFile.js[data-never-block]

Installation

Most of the time, you have to make small adjustments to a package (e.g., the configuration in Settings.Carbon.yaml). Because of that, it is important to add the corresponding package to the composer from your theme package. Mostly this is the site package located under Packages/Sites/. To install it correctly go to your theme package (e.g.Packages/Sites/Foo.Bar) and run following command:

composer require carbon/includeassets --no-update

The --no-update command prevent the automatic update of the dependencies. After the package was added to your theme composer.json, go back to the root of the Neos installation and run composer update. Et voilà! Your desired package is now installed correctly.

carbon/includeassets 适用场景与选型建议

carbon/includeassets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 234.9k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2017 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「flow」 「helper」 「Neos」 「carbon」 「FUSION」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 carbon/includeassets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 carbon/includeassets 我们能提供哪些服务?
定制开发 / 二次开发

基于 carbon/includeassets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 14
  • Watchers: 5
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2017-04-01