Integrate React and webpack
Overview:
This document provides a comprehensive guide for developers to configure Webpack for bundling CSS and JavaScript files, integrate React Js into the system. It includes step-by-step instructions, code snippets, and explanations to facilitate smooth implementation.
New Features Added:
Webpack: A module bundler for JavaScript applications. It is used to bundle CSS and JavaScript files efficiently.
React Js: A JavaScript library for building user interfaces, widely used for its component-based architecture.
Configuring Webpack in Our System:
Step 1: Installing Node.js
Node.js is required to create static content package on demand. Follow these steps to install Node.js:
Download and install Node.js from the official website.
Once installed, verify the installation by running node -v and npm -v commands in the terminal (Discuss in step 3) to ensure Node.js and npm (Node Package Manager) are installed correctly.
Navigate to your main project directory (e.g., ViennaAdvantage).
Step 2: Setting Up Files
Place the following files in your main project (e.g., ViennaAdvantage) directory:
webpack.config.js: Configuration file for Webpack.
package.json: File containing metadata and dependencies for the project.
babel.config.json: Configuration file for Babel, a JavaScript compiler. In this file replace name with module name and version with package version. Shown in below image
File structure image.
Step 3: Opening Terminal in Visual Studio
Visual Studio provides multiple methods to open the terminal:
Menu Option: Select View > Terminal to open the terminal as a tab.
Keyboard Shortcut: Use Ctrl + ` to quickly launch the terminal.
Context Menu: Right-click on a folder in the Solution Explorer and select Open in Terminal to open the terminal at a specific file path.
Step 4: Installing Dependencies
Run the following command in the terminal to install the required dependencies specified in the package.json file:
npm install
Step 5: Configuring Bundling for JavaScript and CSS
Create a folder named src inside the Script folder.
Inside the src folder, create separate JavaScript file for bundling.
In the JavaScript file (e.g., VISjs.js), import all script files using the ‘import’ statement.
Create a folder name src inside the Content folder
Inside the src folder, create separate css file for bundling.
In the CSS file (e.g. css-style.css), import all CSS files using the ‘@import’ directive.
7.Create a folder name dist inside the Script folder. This folder contains the all js minify files.
Step 6: Configuring webpack.config.js
Open the webpack.config.js file.
Update the following sections according to your project requirements:
Version: Define the versions of different components.(e.g 1.2.3.0) four digit.
Mode: This option in webpack configures the environment for bundling, optimizing for either development or production.
Entry Point: Specify the entry points for the webpack bundler.
Delete Previous Version css Files: Modify regular expression patterns to match the filenames of the files you want to delete.
Note: Naming convention of static file should be same as we are using earlier to create module
Step 7: Changes in Area Registration
In the Arearegistration.cs file, uncomment the minify files permanently.
add ‘dist’ after the Scripts as shown in below image.
In the last add {version} as shown in below image
Step 8: Building Files
Run the following command in the terminal: npm run Build. This will generate bundled files in the Script and Content folders.
Step 9: Change in Post-buid
In Solution Explorer, Select the ViennaAdvantage project
Press ALT+ENTER
Select Build Event
Add below code in post-build events command Line on the top as shown in image
rmdir /s /q "$(SolutionDir)\ViennaAdvantageWeb\Areas\$(TargetName)"
If you prefer not to use the terminal for bundling JS and CSS files, you can write the command npm run build in Pre-build events command line as show below:
Configuring React.Js in Our System:
Step 1: Create React Folder
Navigate to the Script folder in your system.
Create a new folder named react.
Step 2: Create Index.jsx File
Inside the react folder, create a file named index.jsx.
Add the following code to index.jsx:
Note: In above code Replace ViennaAdvantage with module prefix.
Step 3: Create pages Folder
Inside the react folder, create a new folder named pages.
This folder contains the main page which you list in AD.
Step 4: Create Component Folder
Inside the react folder, create a new folder named component.
This folder will contain all React components.
You can use these component in React pages.
Step 5: Implement React page
Create your React page inside the pages folder.
For example, if you create a pages named TestComponent.jsx, shown as below.
Step 6: Define react pages Class Name
Ensure you are logged in as a Super user.
Open the Page (Form) window from the menu.
Fill in the mandatory fields.
Add the class name with the following format: <prefix>.React.<component>.
For example, if you created a component named TestComponent.jsx, the class name will be VIS.React.TestComponent.