SSR React with Symfonyfor the Strong of Spirit ⚔Workshop
Webpack
Theory
Let's talk about Webpack.
Dependencies
One of the first things we will want is to use dependencies in our JavaScript code.
Webpack will allow us to use them and bundle our whole app in a single file.
Let's install it:
npm i --save-dev webpack webpack-cli
Now install our first dependency, the infamous left-pad package.
npm i --save left-pad
Tip
In 2016, Azer Koçulu, author of 250 NPM packages, unpublished all of them in the course of a dispute with NPM admins. One of these packages was left-pad, a package used in thousands of projects.
That caused a lot of despair in lots of people.
Its purpose? padding strings with a fixed number of characters.
The MI6 uses left-pad to generate codes for its secret intelligence service. Let's see that program:
This, per se, does nothing, because we have not configured Babel presets, that tell Babel the transformations to do.
We can configure Babel in several ways. One is to write a .babelrc file. Create this file with the following content:
1
2
3
{"presets":["@babel/preset-env"]}
This means "use the latest stable version as source". It is currently the same as using all these presets combined: babel-preset-es2015, babel-preset-es2016, and babel-preset-es2017
Exercise
Read about babel-preset in its documentation. Can you identify the section about targeting browsers? And the section about targeting node versions? Which option would we be useful for Server Side Rendering? Which options would be useful for Client Side Rendering?
Now let's play with the pipeline proposal:
Install it with the following command:
npm i --save-dev @babel/plugin-proposal-pipeline-operator
This proposal is in a verly stage (Stage-1), so the community is currently experimenting with it, and identifying the challenges that it poses.
In this case there are several competing implementations. We are going to use the "minimal" implementation.