0

I have a javascript app where I have split the code into files in different namespaces, like this:

// file 1: src/Main.js
var Main = function () { .... 

// file 2: src/Main/SomethinElse.js
Main.SomethingElse = function () { ....

etc....

The problem with this when minified is when these files are merged the first file has to be always first and then the second one.

Right now I have a bash script that does the compiling, in there I have list of all the files in the proper order, but I have to add every new file into the list after creating it, which does not seem very nice.

Is there any build tool to deal with this or maybe some way to restructure my code in order to avoid this problem ?

1
  • Grunt and concat. You can specify the first file that needs to be loaded before all the others. Commented Sep 14, 2015 at 13:26

2 Answers 2

1

I would choose Gulp to build your project. It's easy to understand and has alot of plugins for your build. Link

If you want to use Gulp you can create a Task there like this:

gulp.task('scripts', function() {
    gulp.src([
        'src/Main.js', // take main js file first
        'src/*.js', // take all js files in src
        'src/Main/SomethinElse.js'])
        .pipe(sourcemaps.init()) // create sourcemaps for your code
        .pipe(concat('index.js')) // create one js file for example index.js
        .pipe(stripDebug()) // you can debug here
        .pipe(uglify()) // minify your js file
        .pipe(sourcemaps.write('maps/')) // sourcemap destination
        .pipe(gulp.dest('scripts/')); // destination of your js file
});

Hope i could help you.

Sign up to request clarification or add additional context in comments.

1 Comment

gulp is way more advanced than my bash script, but in the end I still have to enter file paths the same way. I was hoping for some more elegant solution, but it seems there is nothing better. At least with gulp I have the "*.js" part, which is something. Thanks for your answer and example.
0

Easy (but inelegant) way round it is to prefix your source files with a number

That way, you can be sure main is defined before main.example

00_header.js
10_main.js
20_main/00_header.js
20_main/10_example.js
99_on_ready.js

Also, on linux, sort is LC_ALL dependant - see man sort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.