Combine each module using grunt and then compress

Looking to achieve this using Grunt. My current file structure is as follows:

/app
--app.js
--/module1
----module1.js
----module1Model.js
--/module2
----module2.js
----module2Model.js

How can I bundle and minify each module into a single file to appear like:

/app
--app.min.js
--module1.min.js
--module2.min.js

We aim for this process to be scalable, accommodating additional modules 3 and 4 seamlessly. Appreciate any assistance provided!

Answer №1

Consider using the Uglify tool for the solution, and feel free to customize the "dist" and "src" directories to fit your needs:

uglify:{
        liveproject:{
            files:{
                'dist/js/main.min.js' : ['src/js/*.js'],
                'dist/js/m1.min.js' : ['src/js/easing.js','src/js/modernizer.js'],
            }   

        }

    },

Answer №2

I found the solution in this thread: Using grunt to merge multiple files from different directories into one renamed file in a new directory

grunt.registerTask('prepareModule', 'process dist module for concat', function() {

        //retrieve the concat task object
        var concat = grunt.config.get('concat') || {};

        //loop through all files/folders in build/src/app/
        grunt.file.expand(grunt.config('build_dir') + '/src/app/*').forEach(function(dir) {

            //extract the last part of the path
            var dirName = dir.substr(dir.lastIndexOf('/') + 1);

            //filter out folders only
            if (dirName.slice(-3) !== '.js') {

                //add target for each module
                concat[dirName] = {
                    src: [grunt.config('build_dir') + '/src/app/' + dirName + '/*.js'],
                    dest: grunt.config('dist_dir') + '/src/app/' + dirName + '/' + dirName + '.js'
                };
            }
        });

        grunt.config.set('concat', concat);

    });

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

What causes the Request.Params of IHttpHandler to be accurate on the initial call, but become null on the subsequent call?

My HTML/Javascript page utilizes a jQuery ajax call to communicate with a .NET ajax handler (.ASHX). The issue arises in subsequent calls where the parameters passed in the data object are unexpectedly NULL despite being correct during the first call. Her ...

The redirection to the HTML page happens too soon, causing the ASYNC functions to fail in saving the user's image and data to the server

Hey everyone! I'm facing an issue with async/await functions. Here's the code snippet from my backend where I'm trying to save details of a newly registered user. I'm puzzled as to why the Redirect() function is executing before the f ...

What benefits come from dynamically loading and unloading JavaScript and CSS stylesheets?

Introduction: Currently, I am in the process of developing a website that will utilize ajax to change content without refreshing the main frame and images every time. The main frame has its own site.css stylesheet. Query 1: Considering the use of a sing ...

Redirect user to new page upon successful login using next-auth middleware

I recently implemented the next-auth middleware to protect all pages on my website. I followed the official documentation on next-auth (next-auth) and verified that it successfully redirects users who are not logged in. However, I encountered an issue whe ...

Determine the status of a script in PHP by incorporating AJAX

I am having trouble with my file upload page in the application. I want to display "Uploading" while the file is uploading and then show "Processing" while the file is being processed. Eventually, after the script completes, my page should redirect to a sp ...

Exploring the contrast between router.pathname and router.route within Next.js

Essentially, my goal is to utilize the NextJS router to access the page url by doing the following: import { useRouter } from "next/router"; const SomeComp = props => { const router = useRouter(); } Yet, when I console.log() the propertie ...

Creating interactive navigation bar effects with scroll and click functionality using jQuery

Is there a way to make the navigation highlight when a user clicks on it and also scrolls to the corresponding section? Currently, I am facing an issue where only the third navigation event highlights, most likely because when navigating to the fourth or f ...

Using a for loop in conjunction with Observable.forkJoin

In my component, I am striving to populate an array known as processes, containing individual process objects that each have a list of tasks. Currently, I am dealing with two API calls: /processes and /process/{processId}/tasks I utilize the /processes ...

How does a browser automatically fill in email and password fields?

When using a text input and a password input in my single page app, Chrome often prompts to remember the information for autofill. However, I am encountering an issue where it doesn't actually autofill the information. Does anyone know how to trouble ...

Unable to assign the value of a Ruby variable to a JavaScript variable

I have a Ruby variable that contains latitude and longitude coordinates: @json = [{"lat":37.8690058,"lng":-122.2555342},{"lat":37.8739362,"lng":-122.2653001},{"lat":37.8701101,"lng":-122.2578559}] When I attempt to use this variable in a JavaScript scrip ...

The text does not change in the input field even with the .value method

I'm encountering an issue where I want to use .value to set the text of an input field. However, after setting the value of the input field, the text disappears when clicked on. I would like to find a solution where the text does not disappear upon cl ...

The most effective method for transferring asynchronous data to pages in Next.js

My current directory structure: - components - NavBar - Header - Layout - pages - pages - demo.js - _app.js - index.js // index.js import React from 'react'; import NewLayout from "../../components/NewLayout/NewLayou ...

The content contained within the .each loop within the click event is only executed a single time

While working on coding a menu opening animation, I encountered an issue today. Upon clicking the menu button, the menu opens and the elements inside receive an added class (resulting in a fade-in effect). Clicking the menu button again should close the ...

Issue with Prisma ORM in Next.js 13: Outdated Data Shown When Navigating Instead of Latest Data

In my project, I am utilizing Next.js 13 and Prisma ORM to fetch data in a server component and then pass it down to child components using a context. The page route is structured like so: /item/[id], and the id is used in the layout.tsx file to retrieve t ...

What is the best way to iterate through a designated key in every object within a JSON dataset and store it in a variable called `data` for use with

I am looking to create a line chart displaying stock prices, utilizing JSON data to populate the chart. Let's say I have some JSON data: [ { "close": 116.59, "high": 117.49, "low": 116.22, "open& ...

Combining Laravel and vue.js for seamless file uploading functionality

Successfully uploading files using vue.js 1.0 with a POST request looks like: store () { this.storingMessage = true; var form = new FormData(); form.append('subject', this.message.subject); form.ap ...

Utilizing the `this` keyword within a click handler that is passed through an intermediary function

If I initially have a click event handler in jQuery set up like this jQuery('#btn').click(_eventHandler); And handling the event as follows function _eventHandler(e){ jQuery(this).text('Clicked'); } Does the this keyword serve a ...

Unexpected Behavior of JavaScript Execution in WebDriver

Currently, I am using the JBehave-Web distribution (3.3.4) with Webdriver to conduct testing on an application. I have encountered a peculiar issue: My challenge lies in interacting with a modalPanel from Richfaces, as it consistently throws an ElementNot ...

Navigate to the previous or next page using JavaScript

I've been struggling to figure out how to navigate one page up and down using simple links, but I haven't found a solution that works for me. The URL I am working with is: . When the "next page" button is clicked, it should go to ?page=2, and whe ...

Is there a way to integrate Prettify with Blogger/BlogSpot?

I am currently utilizing blogger.com as a platform to showcase programming texts and I am interested in incorporating Prettify (similar to Stack Overflow) to enhance the appearance of my code samples. What is the best method for installing the Prettify sc ...