Advanced Workshop by Vilva Athiban
// index.html
<html>
<head></head>
<body>
<script type="text/javascript">
console.log("Inline Javascript")
</script>
<script type="text/javascript" src="pathToJSFile.js" />
<body />
</html>
// pathToJSFile.js
console.log("test");
Not Scalable
Many Scripts -> Performance bottleneck
One large Script -> Not Maintainable
Scope Issues
IIFE - Immediately Invoked Function Expression
Avoids Scope collisions
Tools: Gulp, Grunt
(function add(a, b) {
return a + b
}) (3,5);
Dead/ Unused Code
Rebuilds
No Lazy Loading
Evolution of NodeJS creates Common JS modules
Node + modules(common js) + NPM = Awesome
// Named import/export
// add.js
exports.add = () => { /* do add */ }
//index.js
const {add} = require('./add.js');
// Default import/export
// add.js
module.exports = () => { /* do add */ }
//index.js
const anyName = require('./add.js');
No Browser Support
Loaders load and transpile at runtime
Bundlers transpile and bundle before hitting the browser
Code in Common JS => Bundle it together
Supports Browser directly
RequireJS
Debugging such code is a big issue
Code can be AMD as well, which is a problem
No Lazy load / Async bundling
Reusable & Scalable
Better Syntax than CommonJS
// Named import/export
// add.js
export const add = () => { /* do add */ };
//index.js
import {add} from './add.js'
// Default import/export
// add.js
export default () => { /* do add */ };
//index.js
import anyName from './add.js'
Node has no support (In older versions)
Slow in Browsers (when not transpiled)
Write in any Format -> Convert for Browser
Async Bundling and Lazy loading
Bigger EcoSystem
Bundle more than JS & JSON
Get equipped with Plugins and Loaders
Single Entry - Default (src/indexjs)
{
entry: "./src/home.js",
output: {
filename: 'bundle.js'
}
}
Object Syntax
{
entry : {
main: "./src/main.js",
vendor: "./src/vendor.js"
},
output: {
filename: 'bundle.js'
}
}
{
entry : {
main: "./src/main.js",
vendor: "./src/vendor.js"
},
output: {
filename: '[name].bundle.js'
}
}
Multi Entry
{
entry: {
pageOne: './src/pageOne/index.js',
pageTwo: './src/pageTwo/index.js',
pageThree: './src/pageThree/index.js'
}
output: {
filename: '[name].[contenthash].bundle.js'
}
}
Multi Entry
{
entry: {
pageOne: './src/pageOne/index.js',
pageTwo: './src/pageTwo/index.js',
pageThree: './src/pageThree/index.js'
}
output: {
filename: '[name].[contenthash].bundle.js',
path: __dirname + '/dist',
publicPath: "http://pathToCDN/dist"
}
}
To assign public path in runtime : __webpack_public_path__
Its a function
Transforms Source code to JS
Chaining of loaders executes in reverse order
module: {
rules: [
{
test: \.css$/,
use: [style-loader, css-loader]
}
]
}
Its a class/object with `apply` method
Access entire Compiler lifecycle
plugins: [
new PluginName( { ...optionsIfAny } )
]
Import
Require
Define
@import
url()
<img src="" />
__webpack__require
Node, electron, web, web-workers etc
One code - multiple targets
module.exports = [ wpConfigForNode, wpConfigForWeb, wpConfigForElectron]
Caching
Dependency Graph
Runtime
Browser Compatability
Two moving parts of HMR
Update Manifest
Updated Chunk
Webpack itself is built by many plugins
Tapable is basic utility for such plugins to access hooks of various events
Dynamic Import automates code-splitting
Vendor chunks possible with splitChunks
Lazy loading & Caching
Magic Comments
Preload/Prefetch
Hashing
Compression
Algorithm: Base64 VLQ
• Generated column
• Original file this appeared in
• Original line number
• Original column
• And if available original name
Map with comment : // sourceMappingURL