webpack one build step to rule them all
play

webpack: One Build Step To Rule Them All Sean Larkin: webpack core - PowerPoint PPT Presentation

webpack: One Build Step To Rule Them All Sean Larkin: webpack core team | @thelarkinn UX Developer @ Mutual of Omaha Come work here! Home of the highly controversial license plate DRAFT. Where most people think Nebraska is... THATS WHERE


  1. webpack: One Build Step To Rule Them All Sean Larkin: webpack core team | @thelarkinn

  2. UX Developer @ Mutual of Omaha Come work here!

  3. Home of the highly controversial license plate DRAFT.

  4. Where most people think Nebraska is...

  5. THATS WHERE LINCOLN, NE IS!! Where Nebraska really is...

  6. Background Former Tech Support Rep. gone rogue turned Software Engineer / Web Developer who got tired of never being able to really help the customer he served. Written in... Languages: Ruby, Objective-C, Swift, Javascript. Also… Woodworker, chicken farmer, IoT.

  7. Projects webpack core team angular-cli core team

  8. @TheLarkInn Github - Medium - Codepen - Stack Overflow - LinkedIn - Twitter “Watching @TheLarkInn @qconsf teach me about #webpack has changed my life. #webpackAllTheThings”

  9. ASK ME ANYTHING http://github.com/thelarkinn/ama

  10. JavaScript Modules

  11. Qualities of JavaScript Modules: Don’t pollute global scope Reusable Encapsulated Organized Convenient

  12. JavaScript Modules... How to use them? Script Tag Global Variable Namespace (require/import)

  13. JavaScript Modules... What they look like...

  14. CommonJS //loading module var _ = require(‘lodash’); //declaring module module .exports = someValue;

  15. AMD define(‘myAwesomeLib’, [‘lodash’, ‘someDep’], function (_, someDep) { return { … } } );

  16. AMD + CommonJS define( function(require, exports, module) { var _ = require(‘lodash’); //..do things module .exports = someLib; });

  17. ES2015/TypeScript import {Component} from ‘angular2/core’ @Component({ selector:’info’ }) export class InfoComponent{}

  18. So let’s talk about making them work together… ...for the browser.

  19. Every library is different... And has their own loading requirements… And their own module “shape”... And their own way of using them in the “browser”

  20. And this is just for JavaScript... Each and every other filetype until now has had to have specific ways to process it.

  21. Wouldn’t it be nice...

  22. WEBPACK

  23. Webpack is a module bundler and not a task runner . Every asset is a dependency of another (js, css, html, jpg, icon, svg, etc...). Static build tool (NOT A MODULE LOADER)!

  24. But how?

  25. module.exports = { entry: { vendor: './src/vendors.ts', Webpack - How to main: './src/main.browser.ts' }, use it? output: { path: 'dist/', filename: '[name].bundle.js', sourceMapFilename: '[name].map', chunkFilename: '[id].chunk.js' }, resolve: { extensions: ['.ts', '.js'], modules: ['node_modules'] webpack.config.js }, module: { Yes, its a module too!!! { enforce: 'pre' test: /\.js$/, loader: 'source-map-loader', exclude: [ // these packages have problems with their sourcemaps root('node_modules/rxjs') ] } ], loaders: [ { test: /\.ts$/, loader: 'awesome-typescript-loader', exclude: [/\.(spec|e2e)\.ts$/] }, { test: /\.json$/, loader: 'json-loader' }, { test: /\.css$/, loader: 'raw-loader' }, { test: /\.html$/, loader: 'raw-loader', exclude: [root('src/index.html')] } ] },

  26. Webpack - How to use it? $> webpack <entry.js> <result.js> --colors --progress Webpack CLI $> webpack-dev-server --port=9000

  27. Webpack - How to var webpack = require("webpack"); use it? // returns a Compiler instance webpack({ // configuration object here! }, function(err, stats) { Node API // … // compilerCallback console.error(err); });

  28. The Core Concepts

  29. Entry

  30. The Core Concepts: Entry The “contextual ENTRY_FILE.js bootstrap.js root” of your application. app.component.ts The first javascript file to load to external.lib.js some.component.ts some.component.js “kick-off” your app external.lib.dep.js some.component.sass in the browser. external.lib.dep.css

  31. The Core Concepts: Entry browser.main.ts //webpack.config.js module .exports = { entry: ‘./browser.main.ts’, //... } //browser.main.ts import { Component app.component.ts app.js } from ‘@angular/core’; ng2-material.lib.js external.lib.js import { App some.component.ts some.component.js } from ‘./app.component’; bootstrap(App,[]); external.lib.dep.js ng-core.class.js some.component.sass //app.component.ts @Component({...}) external.lib.dep.css export class App {};

  32. The Core Concepts: Entry browser.main.ts //webpack.config.js module .exports = { entry: ‘./browser.main.ts’, //... } //browser.main.ts import {Component} from ‘@angular/core’; app.component.ts app.js import {App} from ng2-material.lib.js external.lib.js ‘./app.component’; some.component.ts some.component.js bootstrap(App,[]); external.lib.dep.js ng-core.class.js some.component.sass //app.component.ts @Component({...}) export class App {}; external.lib.dep.css

  33. The Core Concepts Entry Tells webpack WHAT (files) to load for the browser; Compliments the Output property.

  34. Output

  35. The Core Concepts: Output //webpack.config.js ./dist/ module .exports = { entry: ‘./browser.main.ts’, bundle.js output: { path: ‘./dist’, filename: ‘./bundle.js’, }, //... } browser.main.ts //Generates bundle.js app.component.ts external.lib.js some.component.ts external.lib.dep.js some.component.sass external.lib.dep.css

  36. The Core Concepts Entry Output Tells Webpack WHERE and HOW to distribute bundles (compilations). Works with Entry.

  37. Loaders

  38. The Core Concepts: Loaders entry.js module: { Tells webpack how loaders: [ to load files in your content base. {test: /\.ts$/, loader: ‘ts’}, app.component.ts Loaders are also javascript modules {test: /\.js$/, loader: ‘babel’}, ( function ) that takes external.lib.js the source file, and {test: /\.css$/, loader: ‘css’} returns it in a ], external.es6.dep.js ‘loaded’ [modified] } state. external.lib.dep.css

  39. The Core Concepts: Loaders module: { test preLoaders:[], //lint A regular expression that loaders: [ instructs the compiler which files to run the loader against. { test: regex, loader loader: string, A string of the loader names loaders: Array<string>, you want to run. include: Array<regex>, exclude: Array<regex>, loaders }, An array of strings ], representing the modules you want to run. If using ‘loader’, postLoaders:[] //coverage, docs, etc. provide a string of multiple } loaders separated by ‘!’. IE: ‘style!css!less`

  40. The Core Concepts: Loaders module: { include preLoaders:[], //lint An array of regular expression loaders: [ that instruct the compiler { which folders/files to include. test: /\.ts$/, Will only search paths provided loader: string, with the include. loaders: [ ‘awesome-typescript-loader’, exclude ‘ng2-asset-loader` ], An array of regular expression include: /some_dir_name/, that instructs the compiler exclude: [/\.(spec|e2e)\.ts$/], which folders/files to ignore. }, ], postLoaders:[] //coverage, docs, etc. }

  41. The Core Concepts: Loaders Chaining Loaders loaders: [ { test: /\.less$/, loader:’style!css!less’ }, { test: /\.less$/, loaders:[‘style’, ‘css’, less’]} ] less-loader css-loader style-loader style.less style.css *.js inlineStyleInBrowser.js

Recommend


More recommend