This plugin adds several twig functions that help to include script and style html tags to twig templates from webpack-encore entries.
Before starting to work with the plugin, all you need is:
- Project based on Micro Framework;
- @symfony/webpack-encore installed by npm or yarn;
You can install the plugin via composer:
composer require oleksiibulba/webpack-encore-plugin
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;
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>
Please see CHANGELOG for more information on what has changed recently.
composer test
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.
If you discover any security related issues, please email oleksii_bulba@epam.com instead of using the issue tracker.
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.
The MIT License (MIT). Please see License File for more information.