@@ -33,7 +33,8 @@ package.json file
33
33
-----------------
34
34
35
35
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 ``):
37
38
38
39
.. code-block :: json
39
40
@@ -51,6 +52,10 @@ to the ``peerDependencies``:
51
52
"dist/bootstrap5-theme.css" : true
52
53
}
53
54
}
55
+ },
56
+ "importmap" : {
57
+ "@hotwired/stimulus" : " ^3.0.0" ,
58
+ "slugify" : " ^1.6.5"
54
59
}
55
60
},
56
61
"peerDependencies" : {
@@ -87,11 +92,28 @@ In this case, the file located at ``[assets directory]/dist/controller.js`` will
87
92
}
88
93
}
89
94
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):
91
96
92
- 3. Write your Stimulus controller with TypeScript in `` src/controller.ts ``.
97
+ .. code-block :: javascript
93
98
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.
95
117
96
118
To use your controller in a template (e.g. one defined in your bundle) you can use it like this:
97
119
@@ -134,3 +156,49 @@ autoimport List of files to be imported with the controller. Useful e.g
134
156
The value must be an object with files as keys, and a boolean as value for each file to set
135
157
whether the file should be imported.
136
158
================== ====================================================================================================
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