oleksiibulba/webpack-encore-plugin

This is a Micro plugin for webpack-encore support

v1.2 2023-01-09 09:14 UTC

This package is auto-updated.

Last update: 2024-11-04 12:18:27 UTC


README

Latest Version on Packagist Software License Total Downloads Scrutinizer Code Quality Code Coverage Build Status

This plugin adds several twig functions that help to include script and style html tags to twig templates from webpack-encore entries.

Get started

Before starting to work with the plugin, all you need is:

Install

You can install the plugin via composer:

composer require oleksiibulba/webpack-encore-plugin

Usage

Add OleksiiBulba\WebpackEncorePlugin\WebpackEncorePlugin to your plugins list:

<?php /* ./etc/plugins.php */

return [
    // List of plugins:
    // ...
    OleksiiBulba\WebpackEncorePlugin\WebpackEncorePlugin::class,
    // ...
];

To use the plugin you need to create an entrypoint in your webpack.config.js:

/* ./webpack.config.js */
const Encore = require('@symfony/webpack-encore');
Encore
    /* ... */
    .addEntry('your_entry_name', './path/to/your_entry_file.jsx')
    /* ... */

run build:

yarn dev
// or
npm dev

and add one of the twig functions to a template:

{# ./templates/base.html.twig #}
{# ... #}
<head>
    {{ encore_entry_script_tags('your_entry_name') }}
    {{ encore_entry_link_tags('your_entry_name') }}
</head>
{# ... #}

Here is the signature of the twig functions:

  • encore_entry_script_tags | encore_entry_link_tags:
    • entryName, type: string, required;
    • extraAttributes, type: array, optional, default value: empty array;
  • getJavaScriptFiles | getCssFiles | entryExists:
    • entryName, type: string, required;

If two or more entries contain common files, then they will be printed only once;

Extra attributes

To add extra attribute to the tags, you can pass them in the array as a second argument, like this:

<head>
    {{ encore_entry_script_tags('your_entry_name', {'defer':true}) }}
</head>

and as a result, it will print next html (assuming your entrypoint 'app' contains only one file './js/app.js'):

<head>
    <script href="/js/app.js" type="application/javascript" defer></script>
</head>

Change Log

Please see CHANGELOG for more information on what has changed recently.

Testing

composer test

Contributing

Please feel free to open pull request or create an issue, they are more than welcome! Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email oleksii_bulba@epam.com instead of using the issue tracker.

Credits

The code was taken and adapted from symfony/webpack-encore-bundle that was created by Symfony Community and Fabien Potencier in particular. Adapted for Micro framework plugin by Oleksii Bulba.

For the full copyright and license information, please see the License File that was distributed with this source code.

License

The MIT License (MIT). Please see License File for more information.