# Custom JavaScript in manifest.json and building out into a single file

**URL:** https://discourse.roots.io/t/custom-javascript-in-manifest-json-and-building-out-into-a-single-file/3316
**Category:** sage
**Tags:** gulp
**Created:** 2015-03-23T14:52:50Z
**Posts:** 8

## Post 1 by @nathobson — 2015-03-23T14:52:50Z

OK, so I’ve been trying to compile custom JS included in my main.js. After pulling my hair out a little, I found this thread: [Sage Gulp Jshint - Trouble passing plugins](https://discourse.roots.io/t/sage-gulp-jshint-trouble-passing-plugins/3181).

Long story short, I’ve got my custom scripts getting pulled in via the vendor attribute under main.js in manifest.json. It does get compiled out into the main.js file but it’s right at the bottom of the file. My intended setup was the all the vendor scripts would be included first (basically as dependencies) and then the functions that fire these scripts (which are written into the scripts/main.js file) would then come afterwards.

Ultimately, I’m trying to keep everything in one file rather than have a separate dependencies/scripts file. Is this against the intended workflow?

---

## Post 2 by @austin — 2015-03-23T19:14:08Z

The way you are envisioning is correct. The ordering is definitely supposed to be

1. Bower packages
2. Vendor packages
3. First-party code

If it is getting compiled like:

1. Bower packages
2. First-party code
3. Vendor packages

Something is not working properly.

What does your main.js dependency in manifest.json look like?

Open up an interactive node console (type `node` in project directory in CLI) and then run:

```
require('asset-builder')('./assets/manifest.json').globs.js
```

And lemme know what it spits out.

I added more tests to confirm [https://github.com/austinpray/asset-builder/pull/30](https://github.com/austinpray/asset-builder/pull/30)

---

## Post 3 by @nathobson — 2015-03-23T19:42:45Z

Thanks for the reply.

My manifest file looks like this currently (I tried vendor before and after files in manifest.json):

```
{
  "dependencies": {
    "main.js": {
      "vendor": [
        "assets/scripts/vendor/slick.js"
      ],
      "files": [
        "scripts/main.js"
      ],
      "main": true
    },
    "main.css": {
      "files": [
        "styles/main.scss"
      ],
      "main": true
    },
    "editor-style.css": {
      "files": [
        "styles/editor-style.scss"
      ]
    },
    "jquery.js": {
      "bower": ["jquery"]
    },
    "modernizr.js": {
      "bower": ["modernizr"]
    }
  },
  "config": {
    "devUrl": "http://xxxxxxx:8888"
  }
}
```

The output from node is:

```
[ { type: 'js',
    name: 'main.js',
    globs: 
     [ '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/transition.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/alert.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/button.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/carousel.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/collapse.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/dropdown.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/modal.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/tooltip.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/popover.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/scrollspy.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/tab.js',
       '/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/affix.js',
       'assets/scripts/vendor/slick.js',
       'assets/scripts/main.js' ] },
  { type: 'js',
    name: 'jquery.js',
    globs: ['/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/jquery/dist/jquery.js'] },
  { type: 'js',
    name: 'modernizr.js',
    globs: ['/Bitbucket/ckd-2015/wp-content/themes/sage/bower_components/modernizr/modernizr.js'] } ]
```

You’ll see there’s slick.js which I’ve added in manifest.json between the bootstrap and main.js files, which is the correct order.

I’ve just double checked my main.js file that gets output and it’s definitely in the order I described in my OP i.e. wrong. This obviously wont mean too much but the final main.js file is here: [http://pastebin.com/yRVSqkmX](http://pastebin.com/yRVSqkmX). The main thing is you can see the Slick (slick.js) references all towards the end of the file if you do a find for slick.

Thanks.

---

## Post 4 by @kalenjohnson — 2015-03-23T20:09:40Z

I recognize the bottom of that pastebin as the `UTIL` object at the bottom of the stock `main.js` file. I don’t think it’s out of order.

Why don’t you comment out uglify in the `gulpfile.js`and verify that anything is out of order.

---

## Post 5 by @austin — 2015-03-23T20:14:20Z

> [@nathobson](#):
>
> I’ve just double checked my main.js file that gets output and it’s definitely in the order I described in my OP i.e. wrong. This obviously wont mean too much but the final main.js file is here: [+function(t){"use strict";function e(){var t=document.createElement("bootstrap") - Pastebin.com](http://pastebin.com/yRVSqkmX). The main thing is you can see the Slick (slick.js) references all towards the end of the file if you do a find for slick.

I just ran this through a beautifier and it looks fine: [https://gist.github.com/a1a570c49c679744fd5b](https://gist.github.com/a1a570c49c679744fd5b)

---

## Post 6 by @nathobson — 2015-03-23T20:36:42Z

OK, totally my bad.

I’ve included some additional JS files and everything worked as intended. Running it through the beautifier or commenting out uglify would certainly have been clever things to do!

Thanks for the help guys, big fan of Sage. This is actually a project I’m porting over from Roots to Sage to keep up with ongoing development. Learning a lot :smile:

---

## Post 7 by @austin — 2015-03-23T20:44:19Z

:sunglasses:

post must be 20 characters

---

## Post 8 by @austin — 2015-06-04T14:09:26Z

Possibly related: [https://github.com/roots/sage/pull/1485](https://github.com/roots/sage/pull/1485)
