-
Notifications
You must be signed in to change notification settings - Fork 2
Dependencies
This is a list of the dependencies that we used and a simple explanation of why we used them.
Webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser. webpack.config.js file is used to describe the configurations required for webpack. Below is the webpack.config.js file which we are using.
const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const outputDirectory = 'dist';
module.exports = {
entry: './src/client/index.js',
output: {
path: path.join(__dirname, outputDirectory),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.sass$/,
use: ["style-loader", "css-loader","sass-loader"]
},
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: "url-loader?limit=100000"
}
]
},
devServer: {
port: 8080,
open: true,
proxy: {
'/api': 'http://localhost:3000'
}
},
plugins: [
new CleanWebpackPlugin([outputDirectory]),
new HtmlWebpackPlugin({
template: "./public/index.html"
})
]
};
- entry: Here the application starts executing and webpack starts bundling
- output path and filename: the target directory and the filename for the bundled output
- module loaders: Module loaders are transformations that are applied on the source code of a module. We pass all the js file through babel-loader to transform JSX to Javascript. SASS files are passed through sass-loaders,css-loaders and style-loaders to load and bundle CSS files. Fonts and images are loaded through url-loader.
- Dev Server: Configurations for the webpack-dev-server which will be described in coming section.
- plugins: clean-webpack-plugin is a webpack plugin to remove the build folder(s) before building. html-webpack-plugin simplifies creation of HTML files to serve your webpack bundles. It loads the template (public/index.html) and injects the output bundle.
Webpack dev server is used along with webpack. It provides a development server that provides live reloading for the client side code. This should be used for development only.
The devServer section of webpack.config.js contains the configuration required to run webpack-dev-server which is given below.
devServer: {
port: 8080,
open: true,
proxy: {
'/api': 'http://localhost:3000'
}
}
Port specifies the Webpack dev server to listen on this particular port (3000 in this case). When open is set to true, it will automatically open the home page on startup. Proxying URLs can be useful when we have a separate API backend development server and we want to send API requests on the same domain. In our case, we have a Node.js/Express backend where we want to send the API requests to. Also it helps to solve CORS Issue,check issues part for more information.
Nodemon is a utility that will monitor for any changes in the server source code and it automatically restart the server. This is used in development only.
nodemon.json file is used to describe the configurations for Nodemon. Below is the nodemon.json file which I am using.
{
"watch": ["src/server/"]
}
Here, we tell nodemon to watch the files in the directory src/server where out server side code resides. Nodemon will restart the node server whenever a file under src/server directory is modified. NB: if you have an internal watch failed when using nodemon try this command :echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
Concurrently is used to run multiple commands concurrently. I am using it to run the webpack dev server and the backend node server concurrently in the development environment. Below are the npm script commands used.
"server": "nodemon ./src/server/server.js",
"client": "webpack-dev-server --mode development --devtool inline-source-map --history-api-fallback --hot",
"dev": "concurrently \"npm run server\" \"npm run client\""
Express is a web application framework for Node.js. It is used to build our backend API's.It defines a routing table which used to perform different actions based on HTTP methode and Url.
Babel helps us to write code in the latest version of JavaScript. If an environment does not support certain features natively, Babel will help us to compile those features down to a supported version. It also helps us to convert JSX to Javascript.
.babelrc file is used describe the configurations required for Babel. Below is the .babelrc file which I am using.
{
"presets": ["env", "react"]
}
Babel requires plugins to do the transformation. Presets are the set of plugins defined by Babel. Preset env allows to use babel-preset-es2015, babel-preset-es2016, and babel-preset-es2017 and it will transform them to ES5. Preset react allows us to use JSX syntax and it will transform JSX to Javascript.