Basics
Modern Bundler of Modern Application
convert JS to modules (ES6 syntax) import / export variables, functions from another files install depedencies via yarn install babel and convert JSX for us Advoid global modules
Global modules to learn React
Global modules are not listed in package.json
hard to transfer dev tools to another team, machine Uninstall global modules
Install those global module as local module
But you can’t run command line. Instead, use ‘scripts’ within package.json to define commands.
Then use node / yarn commands to start those modules
Installing Webpack
Then you’ll need to:
create a script command "build": "webpack" in package.json create a config file webpack.config.js at the root dir Basic Config
2 main info:
Learn more about Path (core Node module)
Add —watch to keep compiling files when there are changes
Run live-server to see real-time compiling changes
Import & Export ES6
Import Function
You can’t set entry point to all files.
you have to import all other files to entry point you have to export necessary functions, vars to entry point
Export functions
ONE JS file can set ONE default Export Because Default Export is only ONE, you can call it by any name when import you should use default export if there is only ONE compoent to export
Import npm modules
install — import — use
You have to read Documenation of such modules to find out what export modules you can use.
For example, let use validator npm
WEBPACK is smart enough to ONLY create bundler with necesary JS.
Webpack with Babel
Even when you use Webpack, Babel is not default available.
Webpack only bundle JS, not convert JSX to JS.
You have to install it Babel with Webpack.
Then you can use advanced Webpack Config, called LOADER
transform files before bundle
Advanced Webpack Config
Read module rules
you can use this babel practice and apply to SCSS
Define rules:
loader (what loader to use) test (file types to transform) exclude (files to exclude)
Define babel preset
because you can’t run babel script anymore (you now run babel with webpack) Organizing Components AFTER Webpack
Components
Source
Public
RULES: ONE COMPONENT PER FILE
Remember to import every modules required (react) Remember to import sub-component (and export only parent component)
Source-mapping
Instead of tracking bug from bundler file, this setting will allow us to debug from JSX files.
devtool: 'cheap-module-eval-source-map'
Use webpack as web server (replace live-server)
Install webpack-dev-server (2.5.1)
Setup public folder
devServer: {
contentBase: path.join(__dirname,'public'),
disableHostCheck: true
}