- Run the TypeScript installer
bundle exec rails webpacker:install:typescript
After that, a new file called hello_typescript.ts
will be present in your packs
directory (or rather the source_entry_path
of your webpacker.yml
configuration). You're now ready to write TypeScript.
If you update your App to webpacker >= 5.1
and had TypeScript installed before, you need to add some new/remove some old configurations:
-
Remove old packages:
yarn remove ts-loader
-
Add new packages:
yarn add @babel/preset-typescript
-
Remove old configuration files:
- Delete this file:
config/webpack/loaders/typescript.js
- Delete this file:
-
Remove the following lines from
config/webpack/environment.js
:const typescript = require('./loaders/typescript')
environment.loaders.prepend('typescript', typescript)
-
Add the TypeScript preset to your
babel.config.js
:- This line
['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]
has to be added as the last item to thepresets
array in yourbabel.config.js
- This line
-
Remove old packages:
yarn remove ts-loader pnp-webpack-plugin
-
Follow point 3 and 4 from the
TypeScript with Vue components
section
- Setup react using Webpacker react installer. Then run the TypeScript installer
bundle exec rails webpacker:install:typescript
- Rename the generated
hello_react.js
tohello_react.tsx
. Make the file valid TypeScript and now you can use TypeScript, JSX with React.
- Setup Vue using the Webpacker Vue installer. Then run the TypeScript installer
bundle exec rails webpacker:install:typescript
- Rename generated
hello_vue.js
tohello_vue.ts
. - Install the right Babel preset:
yarn add babel-preset-typescript-vue
- Change the generated
babel.config.js
from
["@babel/preset-typescript", { "allExtensions": true, "isTSX": true }]
to
["babel-preset-typescript-vue", { "allExtensions": true, "isTSX": true }]
and now you can use <script lang="ts">
in your .vue
component files. See the babel-preset-typescript-vue docs for more info.
After you have installed Angular using bundle exec rails webpacker:install:angular
you would need to follow these steps to add HTML templates support:
- Use
yarn
to add html-loader
yarn add html-loader
- Add html-loader to
config/webpack/environment.js
environment.loaders.append('html', {
test: /\.html$/,
use: [{
loader: 'html-loader',
options: {
minimize: true,
removeAttributeQuotes: false,
caseSensitive: true,
customAttrSurround: [ [/#/, /(?:)/], [/\*/, /(?:)/], [/\[?\(?/, /(?:)/] ],
customAttrAssign: [ /\)?\]?=/ ]
}
}]
})
- Add
.html
toconfig/webpacker.yml
extensions:
- .elm
- .coffee
- .html
- Setup a custom
d.ts
definition
// app/javascript/hello_angular/html.d.ts
declare module "*.html" {
const content: string
export default content
}
- Add a template.html file relative to
app.component.ts
<h1>Hello {{name}}</h1>
- Import template into
app.component.ts
import { Component } from '@angular/core'
import templateString from './template.html'
@Component({
selector: 'hello-angular',
template: templateString
})
export class AppComponent {
name = 'Angular!'
}
That's all. Voila!