creative-web-solution/front-polyfill-bundle
composer require creative-web-solution/front-polyfill-bundle
Resources/sample/config.yaml to your project (e.g., /frontend/polyfill/config.yaml).parameters.yaml to point to your config file:
parameters:
cws.polyfill.config_path: /frontend/polyfill/config.yaml
config/packages/_your_bundle_name_/services.yaml:
imports:
- { resource: '@CwsFrontPolyfillBundle/Resources/config/services.xml' }
active: true for desired polyfills in your config.yaml.Generate a polyfill script URL dynamically in Twig:
{% set polyfillUrl = get_front_polyfill_list('js') %}
<script>
let neededPolyfills = [];
{{ polyfillUrl }}
.filter(p => p.test())
.forEach(p => neededPolyfills.push(p.name));
if (neededPolyfills.length) {
document.write('<script src="js/' + neededPolyfills.join('-') + '.js"><\/script>');
}
</script>
config.yaml:
polyfills:
domch:
active: true
test: "!('querySelector' in document)"
file: "path/to/domch-polyfill.js"
{% set polyfills = get_front_polyfill_list('js') %}
<script>
{{ polyfills|raw }}
.filter(p => p.test())
.forEach(p => {
const script = document.createElement('script');
script.src = `js/${p.name}.js`;
document.head.appendChild(script);
});
</script>
# config/routes.yaml
cws_front_polyfill:
path: /js/polyfill-{polyfill_list}.js
controller: Cws\FrontPolyfillBundle\Controller\PolyfillController::content
{{ get_front_polyfill_content()|raw }}
{{ get_front_polyfill_list('js', 'customTest', 'customName') }}
Outputs:
[{ "customTest": test1, "customName": "test1" }, ...]
<script src="js/polyfill-domch-eachnl.js"></script>
get_front_polyfill_content() in a Twig template for /js/polyfill-{polyfill_list}.js.php bin/console cache:clear --env=prod --no-debug
rm -f var/cache/prod/js/polyfill-*.js
Route Placeholder Mismatch:
polyfill_list) matches the config:
parameters:
cws.polyfill.route_placeholder: polyfill_list # Default; omit if using default
/js/polyfill-{polyfill_list}.js.Twig Auto-escaping:
|raw to prevent escaping in get_front_polyfill_list() output:
{{ get_front_polyfill_list('js')|raw }}
Query String vs. Filename:
?polyfill1&polyfill2) is less cache-friendly.polyfill-polyfill1-polyfill2.js) is preferred for static files.Config Path Overrides:
config_path is misconfigured, polyfills won’t load. Verify:
parameters:
cws.polyfill.config_path: /correct/path/to/config.yaml
$polyfills = $this->get('cws_front_polyfill.config')->getActivePolyfills();
dump($polyfills);
true/false (or truthy/falsy values) in the browser console:
console.log(polyfill.test()); // Should log `true` or `false`
Custom Polyfill Tests: Extend the config schema to support custom test logic:
polyfills:
custom:
test: "@service_id.method" # Call a service method for dynamic tests
Cws\FrontPolyfillBundle\Polyfill\TestInterface.Dynamic Polyfill Loading: Use JavaScript to load polyfills asynchronously:
<script>
(function() {
const polyfills = {{ get_front_polyfill_list('js')|raw }};
polyfills.forEach(p => {
if (p.test) {
const script = document.createElement('script');
script.src = `js/${p.name}.js`;
script.onload = () => console.log(`${p.name} loaded`);
document.head.appendChild(script);
}
});
})();
</script>
Polyfill Bundling: Combine polyfills into a single file for production:
js/polyfill-*.js files.module.exports = {
entry: './var/cache/prod/js/polyfill-*.js',
output: {
filename: 'polyfills.bundle.js',
path: './public/js'
}
};
php bin/console cws:polyfill:generate --polyfills="domch,eachnl"
{% if app.request.get('feature') == 'picture' %}
<script src="js/polyfill-picture.js"></script>
{% endif %}
polyfills:
simple:
test: true # Static true/false
dynamic:
test: "!('fetch' in window)" # Dynamic JS expression
public/ or a configured web directory. Use absolute paths in config.yaml:
polyfills:
domch:
file: "%kernel.project_dir%/public/js/polyfills/domch.js"
How can I help you explore Laravel packages today?