Merging multiple modules in one js file

Hello,

so I’ve created different modules in different js files but i want those modules in one js file(minified) without breaking the functionality.

how do i do that?

You should look into a task runner like gulp along with gulp-uglify and gulp-concat

To install these, install node https://nodejs.org/en/ either version should do.

Using the Node.js command prompt; navigate to the root of your project and run:
npm init

You should be presented with some information you need to fill in such as project name e.t.c.

Following this if you run:
npm install gulp -g --save-dev
npm install gulp-concat --save-dev
npm install gulp-uglify -save-dev

You should now have those downloaded to your machine.

In the root of your project create a new file “gulpfile.js” along with this code:

var gulp = require('gulp'),
    uglify = require('gulp-uglify'),
    concat = require('gulp-concat');


gulp.task('concat', function(){
	gulp.src(['src/**/*.js', 'src/*.js'])
	.pipe(concat('appScripts.js'))
	.pipe(uglify())
	.pipe(gulp.dest('./build'));
});

gulp.task('default', ['concat']);

Require will automatically grab the package from your node_modules file where they’re installed
and mainly using gulp we can run these tasks ‘uglify’ and ‘concat’

If I haven’t explained anything well enough feel free to ask.

thanks for replying :slight_smile:. i’ll get back to you if i encounter any issue.

Oh! I left out the most crucial part!

To use gulp you need to run gulp in the node command prompt in the same directory as the gulpfile.

It looks for the task ‘default’ as standard however you can specify specific tasks by adding in the task name, in your case you could run gulp concat for the same effect.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.