Use LESS in Blazor Application
This will add a package.json file in the solution. Now, we will install gulp and related packages from the command prompt. Go to command prompt (RUN > cmd) and navigate to the project folder then type the commands below and press enter.
npm install --save-dev gulp gulp-autoprefixer gulp-clean gulp-clean-css gulp-concat gulp-less
Next, let’s add gulpfile.js into our solution. To do this, right-click the project in Solution Explorer and click “Add New Item..” In the new items dialogue, search Javascript and select Javascript File and name it as "gulpfile.js" then click Add.
And now we are ready to compile the less files. Let's create a less file and some stypes in the file for the test purpose.
Visual Studio provides a GUI-based Task Runner Explorer to link the gulp task to execute during various stages of the Visual Studio build process. To get this explorer, Click View>Other Windows>Task Runner Explorer.
This will bring the Task Runner Explorer window at the bottom with output window tabs. Refer to the below picture.
The left column will display all the gulp tasks we have scripted in gulpfile.js.
Note - If you don’t see any tasks, try clicking the refresh icon seen at the top left corner of the Task Runner window. If you are still unable to see, then you may have not installed gulp and related gulp plugins locally using the npm package manager. You can click “Restore Packages” to restore npm package by right-clicking the package.json file.
You can now right-click a task> Bindings> and select a stage to include a gulp task to execute during that stage. I have added the less task to execute during the “Before Build” stage.
When we re-build our project you can see the gulp tasks executing and the output like below in Task Runner Explorer. It may take some time to execute the tasks so wait until all your task are completed.
Once complete, gulp would have compiled the less files and minimized into the app.css file, and placed the output files in the destination folder as seen below.
npm install antd
npm install --save-dev less-plugin-npm-import
And modify the gulpfile.js file like something below.
Then add the references in the less file and use the defined styles.



Comments
Post a Comment