GithubHelp home page GithubHelp logo

jannescb / blade-script Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cbl/blade-script

0.0 0.0 0.0 24 KB

A package to easily add transpiled & minified scripts to your blade components.

License: MIT License

PHP 100.00%

blade-script's Introduction

Blade Script

A package to easily add transpiled & minified scripts to your blade components.

<button class="btn" onlick="myFunction()">{{ $slot }}</button>

<x-script>
function myFunction() {
    // Do something.
}
</x-script>

Introduction

In case you want to add javascript functionality to a blade component, you can write it directly in the script tag. However, the script will then not be minified and if a component is used multiple times, the script tag will also be included multiple times.

Blade Script solves these problems. The javascript code can be minified in production and each script tag is only included once all without running a compiler separately. Also only required scripts are included.

Additionally there is the possibility to add transpilers like babel.

Installation

The package can be easily installed via composer.

composer requrie cbl/blade-script

Now the necessary assets must be published. This includes the script.php config and the storage folder where the compiled scripts are stored.

php artisan vendor:publish --provider="BladeScript\ServiceProvider"

Include Scripts

The blade component x-scripts includes all required scripts, so it may be placed at the very bottom of your body.

<body>
    ...

    <x-scripts />
</body>

Usage

Each blade component can contain exactly one x-script component. Your scripts can then be written inside the wrapper like so.

<button class="btn" onlick="myFunction()">{{ $slot }}</button>

<x-script>
myFunction() {
    // Do something.
}
</x-script>

Optimizing Scripts

Blade scripts share the same behavior as Views. As suggested in the View documentation, the script:cache command can be added to your deployment workflow to ensure that all scripts are compiled and thus improve performance.

php artisan script:cache

You may use the script:clear command to clear the script cache:

php artisan script:clear

Write Transpiler

You can easily add transpilers to the compiler, the following example explains how to create and include a transpiler.

First you have to create a transpiler class that implements the BladeScript\Contracts\Transpiler contract.

namespace BladeBabel;

use Babel\Transpiler as Babel;
use BladeScript\Contracts\Transpiler;

class BabelTranspiler implements Transpiler
{
    public function transpile($script)
    {
        return Babel::transform($script);
    }
}

The transpiler can now be added to the compiler in your service provider like so:

namespace BladeBabel;

use Illuminate\Support\ServiceProvider;

class BabelServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->afterResolving('script.compiler', function ($compiler) {
            $compiler->addTranspiler(new BabelTranspiler);
        });
    }
}

blade-script's People

Contributors

cbl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.