Skip to content

Commit aa1c55b

Browse files
added asset PackageJsonVersionStrategy
1 parent d2907db commit aa1c55b

File tree

3 files changed

+105
-6
lines changed

3 files changed

+105
-6
lines changed

frontend/custom_version_strategy.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ own version strategy.
1818

1919
Symfony provides various cache busting implementations via the
2020
:ref:`version <reference-framework-assets-version>`,
21-
:ref:`version_format <reference-assets-version-format>`, and
22-
:ref:`json_manifest_path <reference-assets-json-manifest-path>`
21+
:ref:`version_format <reference-assets-version-format>`,
22+
:ref:`json_manifest_path <reference-assets-json-manifest-path>` and
23+
:ref:`package_json_path <reference-assets-package-json-path>`
2324
configuration options.
2425

2526
Creating your Own Asset Version Strategy

reference/configuration/framework.rst

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Configuration
4040
* `base_path`_
4141
* `base_urls`_
4242
* `json_manifest_path`_
43+
* `package_json_path`_
4344
* `packages`_
4445
* `version_format`_
4546
* `version_strategy`_
@@ -1116,6 +1117,7 @@ Each package can configure the following options:
11161117
* :ref:`version <reference-framework-assets-version>`
11171118
* :ref:`version_format <reference-assets-version-format>`
11181119
* :ref:`json_manifest_path <reference-assets-json-manifest-path>`
1120+
* :ref:`package_json_path <reference-assets-package-json-path>`
11191121

11201122
.. _reference-framework-assets-version:
11211123
.. _ref-framework-assets-version:
@@ -1184,7 +1186,8 @@ option.
11841186

11851187
.. note::
11861188

1187-
This parameter cannot be set at the same time as ``version_strategy`` or ``json_manifest_path``.
1189+
This parameter cannot be set at the same time as
1190+
``version_strategy``, ``json_manifest_path`` or ``package_json_path``.
11881191

11891192
.. tip::
11901193

@@ -1317,7 +1320,7 @@ individually for each asset package:
13171320
13181321
.. note::
13191322

1320-
This parameter cannot be set at the same time as ``version`` or ``json_manifest_path``.
1323+
This parameter cannot be set at the same time as ``version``, ``json_manifest_path`` or ``package_json_path``.
13211324

13221325
.. _reference-assets-json-manifest-path:
13231326
.. _reference-templating-json-manifest-path:
@@ -1415,6 +1418,100 @@ package:
14151418
If you request an asset that is *not found* in the ``manifest.json`` file, the original -
14161419
*unmodified* - asset path will be returned.
14171420

1421+
1422+
package_json_path
1423+
..................
1424+
1425+
**type**: ``string`` **default**: ``null``
1426+
1427+
The file path to a ``package.json`` file containing a json with a version number
1428+
which is increased by using ``npm version patch --git-tag-version false`` at the
1429+
build time of the assets.
1430+
1431+
This option can be set globally for all assets and individually for each asset
1432+
package:
1433+
1434+
.. configuration-block::
1435+
1436+
.. code-block:: yaml
1437+
1438+
# config/packages/framework.yaml
1439+
framework:
1440+
assets:
1441+
# this version is applied to every asset (including packages)
1442+
package_json_path: "%kernel.project_dir%/package.json"
1443+
packages:
1444+
foo_package:
1445+
# this package uses its own version (the default file is ignored)
1446+
package_json_path: "%kernel.project_dir%/assets/different/package.json"
1447+
bar_package:
1448+
# this package uses the global version (the default file is used)
1449+
base_path: '/images'
1450+
1451+
.. code-block:: xml
1452+
1453+
<!-- config/packages/framework.xml -->
1454+
<?xml version="1.0" encoding="UTF-8" ?>
1455+
<container xmlns="http://symfony.com/schema/dic/services"
1456+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1457+
xmlns:framework="http://symfony.com/schema/dic/symfony"
1458+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
1459+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
1460+
1461+
<framework:config>
1462+
<!-- this version is applied to every asset (including packages) -->
1463+
<framework:assets package-json-path="%kernel.project_dir%/package.json">
1464+
<!-- this package uses its own version (the default file is ignored) -->
1465+
<framework:package
1466+
name="foo_package"
1467+
package-json-path="%kernel.project_dir%/assets/different/package.json" />
1468+
<!-- this package uses the global version (the default file is used) -->
1469+
<framework:package
1470+
name="bar_package"
1471+
base-path="/images" />
1472+
</framework:assets>
1473+
</framework:config>
1474+
</container>
1475+
1476+
.. code-block:: php
1477+
1478+
// config/packages/framework.php
1479+
$container->loadFromExtension('framework', array(
1480+
'assets' => array(
1481+
// this version is applied to every asset (including packages)
1482+
'package_json_path' => '%kernel.project_dir%/package.json',
1483+
'packages' => array(
1484+
'foo_package' => array(
1485+
// this package uses its own version (the default file is ignored)
1486+
'package_json_path' => '%kernel.project_dir%/assets/different/package.json',
1487+
),
1488+
'bar_package' => array(
1489+
// this package uses the global version (the default file is used)
1490+
'base_path' => '/images',
1491+
),
1492+
),
1493+
),
1494+
));
1495+
1496+
Example ``package.json`` file:
1497+
1498+
.. code-block:: js
1499+
1500+
{
1501+
"version": "1.2.3",
1502+
"scripts": {
1503+
"build": "npm run build:assets && npm run version:patch",
1504+
"build:assets": "...",
1505+
"version:patch": "npm version patch --git-tag-version false"
1506+
}
1507+
}
1508+
1509+
.. note::
1510+
1511+
This parameter cannot be set at the same time as ``version`` or ``version_strategy``.
1512+
Additionally, this option cannot be nullified at the package scope if a global package.json
1513+
file is specified.
1514+
14181515
templating
14191516
~~~~~~~~~~
14201517

templating.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,9 @@ being used and generating the correct paths accordingly.
660660

661661
The ``asset()`` function supports various cache busting techniques via the
662662
:ref:`version <reference-framework-assets-version>`,
663-
:ref:`version_format <reference-assets-version-format>`, and
664-
:ref:`json_manifest_path <reference-assets-json-manifest-path>` configuration options.
663+
:ref:`version_format <reference-assets-version-format>`,
664+
:ref:`json_manifest_path <reference-assets-json-manifest-path>` and
665+
:ref:`package_json_path <reference-assets-package-json-path>` configuration options.
665666

666667
If you need absolute URLs for assets, use the ``absolute_url()`` Twig function
667668
as follows:

0 commit comments

Comments
 (0)