Skip to content

Commit 1d2b363

Browse files
committed
Add docs about JavaScript & TypeScript
1 parent 80adb89 commit 1d2b363

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

bundles/best_practices.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,39 @@ to the ``peerDependencies``:
604604
605605
In this case, the file located at ``[assets directory]/dist/controller.js`` will be exposed.
606606

607-
To use it in a template (e.g. one defined in your bundle) you can use it like this:
607+
.. tip::
608+
609+
You can either write raw JS in this ``dist/controller.js`` file, or you can e.g. write your controller with
610+
TypeScript and transpile it to JavaScript.
611+
612+
Here is an example to do so:
613+
614+
1. Add the following to your ``package.json`` file:
615+
616+
.. code-block:: json
617+
618+
{
619+
"scripts": {
620+
"build": "babel src --extensions .ts -d dist"
621+
},
622+
"devDependencies": {
623+
"@babel/cli": "^7.20.7",
624+
"@babel/core": "^7.20.12",
625+
"@babel/plugin-proposal-class-properties": "^7.18.6",
626+
"@babel/preset-env": "^7.20.2",
627+
"@babel/preset-typescript": "^7.18.6",
628+
"@hotwired/stimulus": "^3.2.1"
629+
"typescript": "^4.9.5"
630+
}
631+
}
632+
633+
2. Run either ``npm install`` or ``yarn install`` to install the new dependencies.
634+
635+
3. Write your Stimulus controller with TypeScript in ``src/controller.ts``.
636+
637+
4. Run ``npm run build`` or ``yarn run build`` to transpile your TypeScript controller into JavaScript.
638+
639+
To use your controller in a template (e.g. one defined in your bundle) you can use it like this:
608640

609641
.. code-block:: html+twig
610642

0 commit comments

Comments
 (0)