Open a new command prompt window and navigate to the folder that has your C#project file (.csproj file). Before we install Webpack, let us first create a package.json file. The package.json file holds metadata information and is used to give information to npm about a projectās (module)dependencies. To create a ādefaultā package.json file, type the ānpm initā command:
npm init -y
The ā-yā option uses default options.
If the command was successful, it should have created a package.json file with contents similar to this:
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
To now install Webpack (which is also a āmoduleā), type the following command:
npm install webpack -D
If the command ran successfully, it should have created a new folder named ānode_modulesā in your project folder and downloaded several modules into this folder (into various folders), of course including the Webpack package as well.
Instead of a run-time dependency, the ā ā save-devā option adds a ādev dependencyā to the package.json file. This indicates that the Webpack package is only useful during development. That would make sense, as once we have created the ābundlesā as we require, we would use them directly in production. The package.json file should look similar to this:
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
},
"devDependencies": {
"webpack": "^4.46.0",
}
}
Create the webpack.config.js file in the root folder with this code:
const path = require(āpathā);
module.exports = {
mode: ādevelopmentā,
entry: ā./wwwroot/source/app.jsā,
output: {
path: path.resolve(__dirname, āwwwroot/distā),
filename: ābundle.jsā
}
};
As you can see above, we are giving Webpack the entry point, which is our app.js file. As well as telling it where to output the bundled file.
Babel is a transpiler. The transpiler transforms ES6 code to use ES5 syntax that older browsers can understand. While Babel itself is very feature-rich and can be used independently, for our purposes, we can use the corresponding babel āloadersā to do the job.
Before we can use any loaders, we need first to install them. For our purpose, we will have to install the relevant babel loaders. Use the following command to install the babel loaders and the āpresetsā.
npm install "babel-loader" @babel/core @babel/preset-env --save-dev
If the command ran successfully, these loaders would be installed in the node_modules folder and the package.json would be modified.
Update webpack with Babel using the next code
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: ābabel-loaderā,
options: {
presets: [ā@babel/preset-envā]
}
}
}
]
}
Install React:
npm install react react-dom --save-dev
Install the Babel preset to process React JSX code:
npm install @babel/preset-react --save-dev
Add the React preset to Babel. It will convert React (JSX) code to raw JavaScript:
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react', '@babel/preset-env']
}
}
}
]
}
Comments