Reorganize external dependencies in the wwwroot directory using gulp

In my development setup using VS 2015, ASP.net vnext, Angular 2, Typescript, and gulp.js, I have successfully automated the process of moving my scripts/**/*.ts files to the wwwroot/app folder. Now, I am looking to extend this automation to include my libraries such as Angular 2. Essentially, I want a gulp task that will:

  • Inject angular.js into index.html within the
    <environment names="Development">
    node
  • Inject angular.min.js into index.html within the
    <environment names="Production">
    node

The key requirement here is for this process to be dynamic and work seamlessly with all libraries in my project:

  • <any>.min.js (for production)
  • <any>.js (for development)

I am capable of handling the minification of any.js myself. However, implementing this using data from the dependencies in the package.json file has proven challenging for me.

Therefore, I am seeking guidance on whether there are existing tools or approaches to streamline this process. Should I break down the workflow into manual steps, such as manually copying and pasting certain libraries? Or could I possibly extract the name of the dependencies and append them with .js, then retrieve these files from the node_modules folder (although this approach feels hacky and potentially unsafe)?

UPDATE

Rephrased/Clarified question:

How can I automatically incorporate my npm dependencies (excluding devDependencies) into the "Development" environment node when triggering specific events like build/clear processes?

Answer №1

If you're looking to simplify including multiple JavaScript files, there's a handy tag helper called asp-src-include at your disposal.

Picture a scenario where you have several *.js files that need to be included:

<script src="/app/app.js"></script>
<script src="/app/controller/controllerA.js"></script>
<script src="/app/controller/controllerB.js"></script>
<script src="/app/service/userservice.js"></script>

Instead of listing them individually, you can streamline the process with just one tag.

<script asp-src-include="~/app/**/*.js"></script>

When it comes to deploying for Production/Development environments, your Razor markup might resemble something like this

<environment names="Development">            
    <script asp-src-include="~/app/**/*.js"></script>
</environment>
<environment names="Staging,Production">
    <script asp-src-include="~/app/**/*.min.js"></script>
</environment>

To make use of this feature, don't forget to include

@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
in your *.cshtml or _Layout.cshtml.

Edit

Another option is using a module like gulp-npm-files which performs a similar task by copying all *.js files to designated folders. Feel free to check out the source code on GitHub if you want to create your own custom module.

However, this approach may not suit your needs entirely especially when dealing with directories containing numerous files. For instance, while working with 'angular2' (AngularJS 2.0), you might only require the compiled/minified versions located in 'angular2/bundles/' such as 'angular2.js', 'angular2.min.js', or 'angular.dev.js'.

In these cases, the dependency's package.json may not provide sufficient details on locating these specific files. This could mean manual copy tasks via gulp to organize dependencies and utilize asp-src-include for automated inclusion into your HTML files generated by razor.

Answer №2

Are you looking for a way to automate the injection of scripts into your HTML code? Consider using the convenient Wiredep module.

If you need to transfer assets to a different folder, there are numerous modules available for copying or linking files between directories. One option you might explore is Gulp-copy.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Automating a task on Raspberry Pi by setting up a cronjob to run a shell script that invokes an npm command instead of directly calling node

I have been working on creating a shell script that can be executed via a cronjob to trigger an npm nodejs application. Here is the content of my start.sh shell script: #!/bin/bash #!/usr/local/bin/npm cd /home/lharby/sites/mysite npm run start When I na ...

As a result of the Chrome 53 bug, the Yahoo Weather API encountered an insecure certificate causing the jQuery AJAX request to fail

For the past year, I've relied on the Yahoo Weather API to provide me with weather updates. I've been utilizing the following link to access the data: https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (SELE ...

How to transfer data using props through the ":to" attribute of a router link in Vue.js

I am facing an issue with creating a router-link in Vue and passing a route name as a prop. What I am trying to achieve is the following: <template> <div> <router-link :to="myProps">Login</router-link> </div> </tem ...

Guide on setting up a Redirect URL with React Router

I am aiming to trigger a redirect URL event using ReactJS. Is there a way to achieve this? I have already attempted the following: successRedirect(){ this.transitionTo('/'); } as well as successRedirect(){ this.router.transitionTo ...

Using the value from the Vuex state to set the initial value for a different parameter

I am facing an issue with utilizing an array of objects in my Vuex state to set a default value for another parameter, specifically for mainAccount: For instance: const store = new Vuex.Store({ state: { AccountNums: [ { label: 'M ...

Ensuring secure communication with PHP web service functions using Ajax jQuery, implementing authentication measures

jQuery.ajax({ type: "POST", url: 'your_custom_address.php', dataType: 'json', data: {functionname: 'subtract', arguments: [3, 2]}, success: function (obj, textstatus) { if( !('error' in obj) ) { ...

Obtaining asynchronous data with nodejs is possible through several methods

I am currently trying to retrieve data from a MySQL table using Node.js. I have a SQL routine in another Node.js file that I am calling, but I am struggling to get the callback function to return the data. I suspect that the issue may be related to calling ...

When developing libraries in webpack, which dependencies should be considered as peer dependencies?

Our team is in the process of developing reusable React components. Each component will be packaged as an npm library with webpack. We are debating whether each library should declare its dependencies as peer dependencies and rely on the application to i ...

Using AJAX to set a session variable in PHP

Upon clicking a div, this JavaScript function is executed: $('.test').click(function(e) { e.preventDefault(); $.ajax({ url: 'ajax.php', type: 'POST', data: {"id": "<? ...

The values are not being properly initialized in the componentDidMount() function

I've been refactoring my code lately and experimenting with using all the lifecycle methods available to me. In an attempt to initialize the state of a component using componentDidMount(), I've encountered some issues. Previously, I used this.pro ...

Removing characters from a string with regular expressions

I need to eliminate instances of << any words #_ from the given text. stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ "; The d ...

Populating Hidden Form Fields with Dynamic Identifiers

I'm facing an issue with a form where I need to input a value into a hidden field upon clicking a button. Despite using unique IDs for all elements, the data is not being submitted in the POST request. The IDs are unique because there are multiple f ...

The persistent redirection issue in Node.js - Middleware

Currently, I am in the process of developing a middleware using node js and express. The main objective of this middleware is to redirect users to a login page if they are not authenticated. Although the redirection to the login page works as intended, th ...

AngularJS selection controls: checkbox and dropdown menus

Can someone provide an example that includes a dropdown menu and checkboxes? The options in the checkbox list should match the data in the dropdown. Once a value is selected from the dropdown, the corresponding checkbox option should be automatically chec ...

Pressing the submit button in the Material UI table clears all selected checkboxes

Take a look at this code snippet: https://jsfiddle.net/aewhatley/Lkuaeqdr/1/. My objective is to construct a table with a submit button utilizing Material-UI components. const { Table, TableHeader, TableHeaderColumn, TableBody, TableRow, Table ...

Is it possible to line up Ajax request in Javascript?

I am seeking a way to schedule an Ajax request to occur every second. The code I currently have in place functions flawlessly. window.onload = function () { setTimeout(doStuff, 1000); // Wait before continuing } function doStuff() { ...

Detect when a user's mouse is hovering over an iframe and escape the iframe accordingly

When the iframe window is visible, there are no issues. However, if the iframe window is set to 0px X 0px in order to hide it while still loading, consider redirecting the iframe or not displaying it at all. In case something is loaded within an iframe or ...

Trouble connecting JavaScript to Node application for proper functionality

After setting up Node/Express/EJS, I am now trying to include js files, but my alert("working"); is not working as expected. Quite ironic, isn't it? The main objective is to load learnJs.js in the browser and trigger the alert so that I can confirm e ...

Adjust the width of the inline textarea using HTML and CSS to match that of the input field

Is it possible to create an inline textarea element using CSS that is selectable but still seamlessly integrated into a sentence without specifying the number of columns? Here's an example with a static number of characters (cols="11"): <p>To r ...

The map function is selectively applied to certain expressions, not all

Currently, I am attempting to display the genre Name and its corresponding gradient by utilizing the map function over an array called genres. While the map function successfully renders the genre Name, it seems to return the same component for the genre g ...