55
danielfishe r.com JavaScri pt & Build Daniel Fisher

2015 - Basta! 2015, DE: JavaScript und build

Embed Size (px)

Citation preview

Page 1: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.comJavaScript

& BuildDaniel Fisher

Page 2: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

I’m a technician & always concerned about bandwidthSo I ask you to send one packet instead of two

DANIEL

My name is Any further questions? Just Ask!

Page 3: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

softwareI design, develop, deploy, teach, train, coach and speak

HTML5 & WEB, DATA ACCESS & PERFORMANCE, SCALABLE & TESTABLE DESIGN, DISTRIBUTED SYSTEMS & SERVICES, SECURITY & TRUST

lennybacon.com my blog url@lennybacon my twitter [email protected] my smtp

find my services at danielfisher.com

Page 4: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Agenda• NPM• Grunt• Gulp• Jasmine• JsHint• TsLint• NPM Revisisted

Page 5: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

NODE PACKAGE MANAGER

Page 6: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

What is npm?• Node.js Package Manager– Open Source founded by

Isaac Z. Schlueter– The package manager for

Javascript.– Installed with node.js!– Nested dependency trees– Install modules used in a node.js environment,

or development tools built using node.js such Karma, lint, minifiers and so on

https://www.npmjs.com/

Page 7: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Install a package:: Local Installationnpm install --save {package name}:: --save adds package to packages.json

:: Global Installationnpm install --global {package name}npm install -g {package name}

Page 8: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Uninstall a package:: Local Installationnpm uninstall --save {package name}

:: Global Installationnpm uninstall -g {package name}

Page 9: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Update all packages:: Local Installationnpm update

:: Global Installationnpm install --global {package name}npm install -g {package name}

Page 11: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

GRUNT

Page 12: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

What is Grunt?• The JavaScript Task Runner• Most contributions by

Ben Alman (Cowboy)

http://gruntjs.com/

Page 13: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Install Gruntnpm install --global grunt-cli

Page 14: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Validate Grunt Installationgrunt --version

Page 15: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

packages.json//Run: npm init to create package.json{ "name": "my-project-name", "version": "0.1.0", "devDependencies": { "grunt": "~0.4.5", "grunt-contrib-jshint": "~0.10.0", "grunt-contrib-nodeunit": "~0.4.1", "grunt-contrib-uglify": "~0.5.0" }}

Page 16: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Add dependencies:: Add package to the devDependenciesnpm install {package} --save-dev

Search available plugins: https://github.com/gruntjs

Page 17: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Restore dependenciesnpm install

Page 18: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Gruntfile.js Minify JSmodule.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { options: { banner: '/*! <%= pkg.name %> ' + '<%= grunt.template.today("yyyy-mm-dd") %> */\n' }, build: { src: 'src/<%= pkg.name %>.js', dest: 'build/<%= pkg.name %>.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['uglify']);};

Page 19: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.comGruntfile.js Bundle & Minify

JSmodule.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { options: { banner: '...' }, js: { files: { 'build/all.js': 'src/**/*.js' }, options: { preserveComments: false } } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['uglify']);};

Page 20: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Custom tasksgrunt.registerTask( 'custom', 'Log some stuff.', function() { grunt.log.write('Logging from ' + this.name).ok(); });

Page 21: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Custom async tasksgrunt.registerTask( 'custom', 'Log some stuff.', function() { var done = this.async(); setTimeout( function() { done(); }, 1000 ); });

Page 22: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Access configuration datagrunt.log.writeln( 'The meta.name property is: ' + grunt.config('meta.name'));

Page 23: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Error handling// Fail synchronously.grunt.log.error('This is an error message.');return false;

// Fail asynchronously.done(false);

// Fail task if "foo" task failed or never ran.grunt.task.requires('foo');

// Fail task if "meta.name" config prop is missinggrunt.config.requires('meta.name');

Page 24: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Detect file changesnpm install grunt-contrib-watch --save-dev

Page 25: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Detect file changes grunt.initConfig({ //... watch: { js: { files: ['src/js/vendor/**/*.js'], tasks: ['concat:js', 'uglify:js'] } }});grunt.loadNpmTasks('grunt-contrib-watch');grunt.event.on( 'watch', function(action, filepath, target) { grunt.log.writeln(target + ': ' + filepath + ' has ' + action); } );

Page 26: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Detect file changesgrunt watch

Page 29: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

GULP

Page 30: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

What is Gulp?• A node.js Task Runner– Most contributions by

Eric Schoffstall (contra)– Grunt plug-ins often perform n tasks

• Gulp plug-ins are designed to do only thing.

– Grunt requires plug-ins for basics• Gulp has them built-in.

– Grunt uses JSON-like data configuration files • Gulp uses leaner, simpler JavaScript code.

Page 31: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Install Gulpnpm install --global gulp

Page 32: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Run Gulpgulp

:: Run a specific taskgulp {taskname}

Page 33: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Add pluginsnpm install gulp-jshint --save-devnpm install gulp-changed --save-devnpm install gulp-imagemin --save-devnpm install gulp-minify-html --save-dev npm install gulp-concat --save-dev npm install gulp-strip-debug --save-dev npm install gulp-uglify --save-devnpm install gulp-minify-css --save-dev npm install gulp-autoprefixer –save-dev

Page 34: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Gulpfile.js Minify Imagesvar changed = require('gulp-changed'); var imagemin = require('gulp-imagemin'); gulp.task( 'imagemin', function() { var imgSrc = './src/images/**/*', imgDst = './build/images'; gulp.src(imgSrc) .pipe(changed(imgDst)) .pipe(imagemin()) .pipe(gulp.dest(imgDst)); });

Page 35: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Gulpfile.js Minify HTMLvar changed = require('gulp-changed');var minifyHTML = require('gulp-minify-html'); gulp.task( 'htmlpage', function() { var htmlSrc = './src/*.html', htmlDst = './build'; gulp.src(htmlSrc) .pipe(changed(htmlDst)) .pipe(minifyHTML()) .pipe(gulp.dest(htmlDst)); });

Page 36: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.comGulpfile.js Bundle & Minify

JSvar concat = require('gulp-concat');var stripDebug = require('gulp-strip-debug');var uglify = require('gulp-uglify'); gulp.task( 'scripts', function() { gulp.src(['./src/js/lib.js', './src/js/*.js']) .pipe(concat('script.js')) .pipe(stripDebug()) .pipe(uglify()) .pipe(gulp.dest('./build/scripts/')); });

Page 37: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Gulpfile.js Minify CSSvar concat = require('gulp-concat');var autoprefix = require('gulp-autoprefixer');var minifyCSS = require('gulp-minify-css'); gulp.task( 'styles', function() { gulp.src(['./src/styles/*.css']) .pipe(concat('styles.css')) .pipe(autoprefix('last 2 versions')) .pipe(minifyCSS()) .pipe(gulp.dest('./build/styles/')); });

Page 38: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Default task with watchergulp.task( 'default', ['jshint', 'scripts'], function() { gulp.watch( './src/scripts/*.js', function(){ gulp.run('jshint', 'scripts'); } ); });

Page 39: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

JASMINE

Page 40: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

What is Jasmine• A JavaScript Testing Framework–Most contributions by

Gregg Van Hove (slackersoft).– Behavior Driven Development.– Does not rely on browsers, DOM, or any

JavaScript framework.– Suited for websites, Node.js projects, or

anywhere that JavaScript can run.

• https://jasmine.github.io/

Page 42: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

JSHINT

Page 43: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

What is JSHint• Static Code analysis for JavaScript–Maintained by Mike Pennisi

(jugglinmike) and others.

• http://jshint.com/

Page 44: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

JS Hint for Gulp• https://

www.npmjs.com/package/gulp-jshint

Page 45: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

TSLINT

Page 46: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

What is TSLint?• Static Code analysis for

TypeScript–Maintained by Palantir

Technologies from New York–Most contributions by Ashwin

Ramaswamy (ashwinr)

• https://github.com/palantir/tslint

Page 47: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

TS Lint for Gulp• https://

www.npmjs.com/package/gulp-tslint

Page 48: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

NPM REVISITED

Page 49: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Creating a NPMnpm init

Page 50: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Create a user accountnpm adduser

Page 51: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Publish a packagenpm publish

Page 52: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Unpublish a packagenpm unpublish {package}@{version}

Page 53: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Custom Registrynpm install sinopiasinopa

Page 54: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Configure the clientnpm set registry=http://localhost:4873npm set always-auth truenpm adduser registry=http://localhost:4873

C:\Users\D.Fisher\AppData\Roaming\sinopia\htpasswd

C:\Users\D.Fisher\.npmrc

Page 55: 2015 - Basta! 2015, DE: JavaScript und build

danielfisher.com

Thank you!danielfisher.com my companylennybacon.com my blog url@lennybacon my twitter [email protected] my smtp