Professional Documents
Culture Documents
Enrique Barra
Contents
Node
– NPM
– Package.json
Transpile (Babel)
Webpack
Linters
Test Tools
2
Node or Node.js
Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript
engine
Node.js uses an event-driven, non-blocking I/O model that makes it
lightweight and efficient
The Node run-time environment includes everything you need to
execute a program written in JavaScript
Useful commands:
– To install all packages that are indicated in package.json
• npm install
– To install a single package (will leave its code in node_modules):
• npm install package_name
– To install a package and save its name and version to the package.json:
• npm install --save package_name
• npm install --save-dev package_name
– To install a package globally in the system (usually sudo is needed in Unix
systems):
• sudo npm install -g package_name
– To exec a script (the script name has to exist in the package.json):
• npm run script_name
4
package.json
package.json is a file that holds various metadata relevant to the
project
This file is used to give information to NPM that allows it to identify
the project as well as handle the project’s dependencies
It can also contain other metadata such as a project description, the
version of the project in a particular distribution, license
information, even configuration data
Most important sections are:
– Information about the app, name, version, author, repository location,
homepage, bugs page, keywords, …
– Packages dependencies (libraries and tools) and their versions
• For production
• For development
– Scripts that can be executed
• Very useful to see the commands that are supported and that will execute the
server, tests, other tasks
6
Webpack
Webpack is a module bundler
Packages all our app JS in one single file (called bundle or main)
– all transpiled JSX and TypeScript with all dependencies
It can even include other resources (fonts, images, css, …) => less files to
worry about
Documentation: https://webpack.js.org/
7
Webpack
Allows different configs for development and production
– In development: starts a development server (webpack-dev-server),
creates a bundle.js with all the code, usually including sourcemaps
and transpiles automatically all the code. It also includes hot
reloading, this means that all changes in the code are automatically
reloaded in the browser
– In production: packages all our app in one single js and html. It can
also compress resources, uglify the code, …
8
Example: webpack.config.js
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/index',
output: {
path: path.join(__dirname, 'dist'),
filename: 'myapp.min.js',
library: 'MyApp',
libraryTarget: 'umd',
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
plugins: ['lodash'],
},
}
],
}
};
9
Compilers and transpilers
Browsers only “understand” JavaScript (and HTML and CSS)
– Not JSX
– Not Typescript
– Not ES6 or ES7 completely
For example
– The TypeScript compiler
– Babel (for JSX transpilation)
10
Example: index.html in React App
<!DOCTYPE html>
<html lang="es">
<head>
<title>My React App</title>
</head>
<body>
<div id="root">
</div>
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
11
Example: index.html in Angular App
<!doctype html>
The whole app. Created
<html lang="en">
with Webpack
<head>
<meta charset="utf-8">
<title>My Angular App</title>
<base href="/”>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js”></script>
<script type="text/javascript" src="styles.bundle.js"></script>
<script type="text/javascript" src="vendor.bundle.js"></script>
<script type="text/javascript" src="main.bundle.js"></script>
</body>
</html>
12
Testers
Types or tests : Unit tests, Integration Tests, UI Tests (A.K.A
Functional Tests)
Test Tools Types
1. Provide a testing structure (Mocha, Jasmine, Jest, Cucumber)
2. Provide assertions functions (Chai, Jasmine, Jest, Unexpected)
3. Generate and display test results (Mocha, Jasmine, Jest, Karma)
4. Generate and compare snapshots of component and data
structures to make sure changes from previous runs are intended
(Jest, Ava)
5. Provide mocks, spies, and stubs (Sinon, Jasmine, enzyme, Jest)
6. Generate code coverage reports (Istanbul, Jest, Blanket)
7. Provide a browser or browser-like environment with a control on
their scenarios execution (Protractor, Nightwatch, Phantom, Casper)
13
Linters
Linter or lint refers to tools that analyze source code to flag
programming errors, bugs, stylistic errors, and suspicious
constructs
14
Utils and Tools
Enrique Barra