wubinworks/module-require-css

An extremely simple module that enables Magento 2's ability to use Require Css.

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 1

Open Issues: 0

Language:JavaScript

Type:magento2-module

1.0.0 2023-10-08 15:35 UTC

This package is not auto-updated.

Last update: 2024-12-16 21:02:47 UTC


README

An extremely simple module that enables Magento 2's ability to use Require Css.
(This module is designed as a dependency for making other modules.)

  • Very useful when you need to load css in .phtml.
  • Dynamically load css.
  • Use inside <head>(eg: load different external css for different stores).

Usage

<script>
// Load a single css in
// [module root]/view/[area]/web/css/example-01.css
// or
// [theme root]/web/css/example-01.css
require(['require-css!css/example-01']);

// Load multiple css.
require([
    'require-css!css/example-02',
    'require-css!css/example-03'
], function () {
    // Code to run when all css loaded successfully.
});

// Load external css. You don't need the .css extension.
require(['require-css!https://some-cdn.tld/styles']);
</script>

How it works

The core part, which is a RequireJS Plugin, is taken from here. When the dependency require-css!css/styles is required, it will inject a link element that contains stylesheet into <head>.

Best used with

wubinworks/HeadContent

Requirements

Magento 2

Installation

composer require wubinworks/module-require-css
Don't forget to run php bin/magento setup:upgrade