Skip to content

Commit e819aa6

Browse files
jmschejaviereguiluz
authored andcommitted
[Frontend] Create a UX bundle: add requirements for Asset Mapper
1 parent ce150c1 commit e819aa6

File tree

1 file changed

+72
-4
lines changed

1 file changed

+72
-4
lines changed

frontend/create_ux_bundle.rst

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ package.json file
3333
-----------------
3434

3535
Your ``package.json`` file must contain a ``symfony`` config with controllers defined, and also add required packages
36-
to the ``peerDependencies``:
36+
to the ``peerDependencies`` and ``importmap`` (the list of packages in ``importmap`` should be the same as the ones
37+
in ``peerDependencies``):
3738

3839
.. code-block:: json
3940
@@ -51,6 +52,10 @@ to the ``peerDependencies``:
5152
"dist/bootstrap5-theme.css": true
5253
}
5354
}
55+
},
56+
"importmap": {
57+
"@hotwired/stimulus": "^3.0.0",
58+
"slugify": "^1.6.5"
5459
}
5560
},
5661
"peerDependencies": {
@@ -87,11 +92,28 @@ In this case, the file located at ``[assets directory]/dist/controller.js`` will
8792
}
8893
}
8994
90-
2. Run either ``npm install`` or ``yarn install`` to install the new dependencies.
95+
2. Add the following to your ``babel.config.js`` file (should be located next to your ``package.json`` file):
9196

92-
3. Write your Stimulus controller with TypeScript in ``src/controller.ts``.
97+
.. code-block:: javascript
9398
94-
4. Run ``npm run build`` or ``yarn run build`` to transpile your TypeScript controller into JavaScript.
99+
module.exports = {
100+
presets: [
101+
['@babel/preset-env', {
102+
"loose": true,
103+
"modules": false
104+
}],
105+
['@babel/preset-typescript', { allowDeclareFields: true }]
106+
],
107+
assumptions: {
108+
superIsCallableConstructor: false,
109+
},
110+
};
111+
112+
3. Run either ``npm install`` or ``yarn install`` to install the new dependencies.
113+
114+
4. Write your Stimulus controller with TypeScript in ``src/controller.ts``.
115+
116+
5. Run ``npm run build`` or ``yarn run build`` to transpile your TypeScript controller into JavaScript.
95117

96118
To use your controller in a template (e.g. one defined in your bundle) you can use it like this:
97119

@@ -134,3 +156,49 @@ autoimport List of files to be imported with the controller. Useful e.g
134156
The value must be an object with files as keys, and a boolean as value for each file to set
135157
whether the file should be imported.
136158
================== ====================================================================================================
159+
160+
Specifics for Asset Mapper
161+
--------------------------
162+
163+
To make your bundle's assets work with Asset Mapper, you must add the ``importmap`` config like above in your
164+
``package.json`` file, and prepend some configuration to the container::
165+
166+
namespace Acme\FeatureBundle;
167+
168+
use Symfony\Component\AssetMapper\AssetMapperInterface;
169+
use Symfony\Component\DependencyInjection\ContainerBuilder;
170+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
171+
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
172+
173+
class AcmeFeatureBundle extends AbstractBundle
174+
{
175+
public function prependExtension(ContainerConfigurator $configurator, ContainerBuilder $container): void
176+
{
177+
if (!$this->isAssetMapperAvailable($container)) {
178+
return;
179+
}
180+
181+
$container->prependExtensionConfig('framework', [
182+
'asset_mapper' => [
183+
'paths' => [
184+
__DIR__ . '/../assets/dist' => '@acme/feature-bundle',
185+
],
186+
],
187+
]);
188+
}
189+
190+
private function isAssetMapperAvailable(ContainerBuilder $container): bool
191+
{
192+
if (!interface_exists(AssetMapperInterface::class)) {
193+
return false;
194+
}
195+
196+
// check that FrameworkBundle 6.3 or higher is installed
197+
$bundlesMetadata = $container->getParameter('kernel.bundles_metadata');
198+
if (!isset($bundlesMetadata['FrameworkBundle'])) {
199+
return false;
200+
}
201+
202+
return is_file($bundlesMetadata['FrameworkBundle']['path'] . '/Resources/config/asset_mapper.php');
203+
}
204+
}

0 commit comments

Comments
 (0)