Книга: Angular 2 Cookbook
Назад: Configuring TypeScript for a minimum viable Angular 2 application
Дальше: Composing application files for a minimum viable Angular 2 application

Performing in-browser transpilation with SystemJS

It can be often useful to be able to deliver TypeScript files directly to the browser and to defer the transpilation to JavaScript until then. While this method has performance drawbacks, it is extremely useful when prototyping and performing experimentations.

Note

The code, links, and a live example of this are available at .

Getting ready

Create an empty project directory and create the following package.json inside it:

[package.json]      {     "scripts": {       "lite-server": "lite-server"     },     "devDependencies": {       "lite-server": "^2.2.2",       "systemjs": "^0.19.38",       "typescript": "^2.0.3"     }   }   

Running npm install should get you ready to write code.

How to do it...

The TypeScript npm package comes bundled with a transpiler. When combined with SystemJS as the designated transpilation utility, this allows you to serve TypeScript files to the client; SystemJS will transpile them into browser-compatible JavaScript.

First, create the index.html file. This file will import the two required JS libraries: system.js and typescript.js. Next, it specifies the typescript as the desired transpiler and imports the top-level main.ts file:

[index.html]      <html>   <head>     <script src="node_modules/systemjs/dist/system.js">     </script>     <script src="node_modules/typescript/lib/typescript.js">     </script>     <script>       System.config({         transpiler: 'typescript'       });       System.import('main.ts');     </script>   </head>   <body>     <h1 id="text"></h1>   </body>   </html>   

Next, create the top-level TypeScript file:

[main.ts]      import {article} from './article.ts';      document.getElementById('text')     .innerHTML = article;   

Finally, create the dependency TypeScript file:

[article.ts]      export const article = "Cool story, bro";   

With this, you should be able to start a development server with npm run lite-server and see the TypeScript application running normally in your browser at localhost:3000.

How it works...

SystemJS is able to resolve module dependencies as well as apply the transpiler to the module before it reaches the browser. If you look at the transpiled files in a browser inspector, you can see the emitted files exist as vanilla JavaScript IIFEs (instantaneously invoked function expressions) as well as their coupled source maps. With these tools, it is possible to build a surprisingly complex application without any sort of backend file management.

There's more...

Unless you're experimenting or doing a rough project, doing transpilation in the browser isn't preferred. Any computation you can do on the server should be done whenever possible. Additionally, all the clients transpiling their own files all perform highly redundant operations since all of them transpile the same files.

See also

  • Composing package.json for a minimum viable Angular 2 application describes how all the pieces work for the core node project file
  • Configuring TypeScript for a minimum viable Angular 2 application talks about how to configure the compilation to support an Angular 2 project
  • Composing application files for a minimum viable Angular 2 application walks you through how to create an extremely simple Angular 2 app from scratch
  • Migrating the minimum viable Angular 2 application to Webpack bundling describes how to integrate Webpack into your Angular application build process
  • Incorporating shims and polyfills into webpack gives you a handy way of managing Angular 2 polyfill dependencies
  • HTML generation with html-webpack-plugin shows you how you can configure an npm package to add compiled files to your HTML automatically
  • Setting up an application with Angular CLI gives a description of how to use the CLI, what it gives you, and what these individual pieces do
Назад: Configuring TypeScript for a minimum viable Angular 2 application
Дальше: Composing application files for a minimum viable Angular 2 application

thank you
Flame
cant read the code since it is all on a single line. Also this comments section is russian
Rakuneque
DATA COLLECTION AND ANALYSIS Two reviewers extracted data and assessed methodological quality independently lasix torsemide conversion Many others were in that space already