Modern JavaScript Explained#
JS package management (npm)#
- why: to overcome the cumbersomeness how conventional JS program install, load 3rd party packages 
- node_modulesfolder is the place where 3rd party packages are installed and located; while- packages.jsonfile specifies the package dependencies with corresponding versions- with only - package.jsonfile and- npm installcommand, all the dependencies will be installed without sharing the- node_modulesfolder, which can be very large
 
JS module bundler (webpack)#
- what: to assist import and export code/module/package across JS files, without resorting to global variables (as is done with the “old-school” JS way) 
- on the server side, - node.jshas become the population implementation of CommonJS
- on the browser side (front end) it works differently since browser doesn’t have access to the file system and this is where the module bundler comes in 
- A JavaScript module bundler is a tool that gets around the problem with a build step (which has access to the file system) to create a final output that is browser compatible (which doesn’t need access to the file system). It finds all - requirestatements and replace them with the actual contents of each required file. The final result is a single bundled JS file.
- how it works: after installation, it can be used as follows - $ ./node_modules/.bin/webpack index.js --mode=development - This statement runs the webpack tool installed in the - node_modulesfolder, start with the- index.jsfile, find any- requirestatements, and replace them with the appropriate code to create a single file (by default is- dist/main.js)
 
- To avoid the tediousness of running above command with all the options every time when - index.jsis updated, we can use- webpack.config.jsto specify all the options inside and only run the following command (better but still tedious)- $ ./node_modules/.bin/webpack
Transpiling code for new language features (babel)#
- Transpiling code means converting the code in one language to code in another similar language. 
- This is an important part of frontend development — since browsers are slow to add new features, new languages were created with experimental features that transpile to browser compatible languages. 
- For transpiling, nowadays most people use babel or TypeScript - We can configure webpack to use - babel-loaderthrough- webpack.config.js
 
npm scripts (task runner)#
- why: to automate different parts of the build process, e.g. run webpack whenever the - index.jsfile gets updated
- This is done through specifying some option under - scriptssection in- package.jsonfile- Note that the scripts in - package.jsoncan run webpack without having to specify the full path- ./node_modules/.bin/webpack, since- node.jsknows the location of each npm module path.
 
