- Setup react using webpacker react installer. Then add required depedencies for using typescript with React:
yarn add ts-loader typescript @types/react @types/react-dom
- Add a
tsconfig.json
to project root:
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es6", "dom"],
"module": "es6",
"moduleResolution": "node",
"sourceMap": true,
"jsx": "react",
"target": "es5"
},
"exclude": [
"**/*.spec.ts",
"node_modules",
"vendor"
"public"
],
"compileOnSave": false
}
- Finally add
.tsx
to the list of extensions inconfig/webpacker.yml
and rename your generatedhello_react.js
using react installer tohello_react.tsx
and make it valid typescript and now you can use typescript, JSX with React.
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.set('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!