# grunt-jsdeps

> Build a dependency tree from script files using reference tags to declare dependencies

Latest version **1.0.1** (published 2016-04-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install grunt-jsdeps
pnpm add grunt-jsdeps
yarn add grunt-jsdeps
bun add grunt-jsdeps
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-04-08 |
| First published | 2015-12-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.8.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Harsh Singh |
| Maintainers | hsingh23 |
| Keywords | grunt-jsdeps, jsdeps, gruntplugin |

## Links

- npm: https://www.npmjs.com/package/grunt-jsdeps
- Repository: https://github.com/hsingh23/grunt-jsdeps
- Issues: https://github.com/hsingh23/grunt-jsdeps/issues
- npm.io page: https://npm.io/package/grunt-jsdeps

## Dependencies (1)

- [jsdeps](https://npm.io/package/jsdeps.md) ^1.0.2

## Recent versions

- 1.0.1 (latest) — 2016-04-08
- 1.0.0 — 2016-03-31
- 0.1.9 — 2016-02-12
- 0.1.7 — 2016-02-10
- 0.1.6 — 2016-02-10
- 0.1.5 — 2015-12-18
- 0.1.4 — 2015-12-18
- 0.1.3 — 2015-12-16
- 0.1.2 — 2015-12-15
- 0.1.1 — 2015-12-04
- 0.1.0 — 2015-12-04

## README

# grunt-jsdeps

> Build a dependency tree from js files with microsoft type dependencies

## Getting Started
This plugin requires Grunt `~0.4.5`

If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

```shell
npm install grunt-jsdeps --save
```

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
grunt.loadNpmTasks('grunt-jsdeps');
```

## The "createJSDeps" task

### Overview
In your project's Gruntfile, add a section named `jsdeps` to the data object passed into `grunt.initConfig()`. Note there is also an async version of this task called asyncCreateJSDeps

```js
grunt.initConfig({
  createJSDeps: {
    your_target: {
      // Target-specific file lists and/or options go here.
      options: {
        pathPrefix: ".", // from where the directory starts
        sourcePath: "test/fixtures/simple", // dir where your js files are stored - they will be recursively scanned
        format: "json", // can also be xml
        dest: "./tmp/simple.json" // name of destination file or path to it
      }
    },
  },
});
```

### Options

#### options.pathPrefix
Type: `String`
Default value: `.`

A string path of what to exclude when writing the path. For example: with pathPrefix = '/test/fixtures/simple/', the path "/test/fixtures/simple/b.js" will be written as "/b.js"


#### options.sourcePath
Type: `String`
Default value: `.`

A string path of the root directory to scan to find js files


#### options.format
Type: `String`
Default value: `json`

Can be either json or xml. The xml option is not currently supported by the update multitask.


#### options.dest
Type: `String`
Default value: `./dependency-tree.json`

The place the output file is written.

### Update Task comments
The update task is `updateJSDeps` and `asyncUpdateJSDeps`

The update task doesn't take a sourcePath, rather it takes a list of files that have changed. The `files.src` is a list of globs of what to include and exclude (to negate a path, prefix it with `!`) In addition you can speific a filter function. This can, for example, watch the files that changed and edit an enviornment variable. Then the filter function can read the env variable when the task is kicked off and only add the files that changed.  


```js
files: {
  expand: true,
  // src: ["test/fixtures/update/simple/**/*.js"]
  src: ["test/fixtures/update/simple/**/*.js", "!test/fixtures/update/simple/a.js"],
  filter: function(path) {
    return path.search("b.js") === -1;
  }
},
```


### Usage Example

#### Simple case
In this example the following options are used

```js
grunt.initConfig({
  jsdeps: {
    simple: {
      options: {
        pathPrefix: ".",
        sourcePath: "test/fixtures/simple",
        format: "json",
        dest: "./tmp/simple.json"
      }
    }
  }
});
```

where the following files have these references
```shell
test/fixtures/simple/a.js
1:/// <reference path="b.js" />
2:/// <reference path="folder/c.js" />

test/fixtures/simple/folder/c.js
1:/// <reference path="d.js" />
```

and the destination file looks like this:
```json
[
  {
    "source": "/test/fixtures/simple/a.js",
    "dependencies": [
      "/test/fixtures/simple/b.js",
      "/test/fixtures/simple/folder/c.js"
    ]
  },
  {
    "source": "/test/fixtures/simple/folder/c.js",
    "dependencies": [
      "/test/fixtures/simple/folder/d.js"
    ]
  }
]
```

#### Relative path example
In this example the pathPrefix is set to remove the prefix "test/fixtures" from the generated file 

```js
grunt.initConfig({
  jsdeps: {
    simple: {
      options: {
        pathPrefix: "test/fixtures",
        sourcePath: "test/fixtures/simple",
        format: "json",
        dest: "./tmp/simple.json"
      }
    }
  }
});
```

where the following files have these references
```shell
test/fixtures/simple/a.js
1:/// <reference path="b.js" />
2:/// <reference path="folder/c.js" />

test/fixtures/simple/folder/c.js
1:/// <reference path="d.js" />
```

and the destination file looks like this:
```json
[
  {
    "source": "/simple/a.js",
    "dependencies": [
      "/simple/b.js",
      "/simple/folder/c.js"
    ]
  },
  {
    "source": "/simple/folder/c.js",
    "dependencies": [
      "/simple/folder/d.js"
    ]
  }
]

```

#### XML case
In this example the following options are used

```js
grunt.initConfig({
  jsdeps: {
    xml: {
      options: {
        pathPrefix: ".",
        sourcePath: "test/fixtures/simple",
        format: "xml",
        dest: "./tmp/simple.xml"
      }
    }
  }
});
```

where the following files have these references
```shell
test/fixtures/simple/a.js
1:/// <reference path="b.js" />
2:/// <reference path="folder/c.js" />

test/fixtures/simple/folder/c.js
1:/// <reference path="d.js" />
```

and the destination file looks like this:
```json
{
  "/test/fixtures/simple/a.js": [
    "/test/fixtures/simple/b.js",
    "/test/fixtures/simple/folder/c.js"
  ],
  "/test/fixtures/simple/folder/c.js": [
    "/test/fixtures/simple/folder/d.js"
  ]
}
```

---
_Source: https://npm.io/package/grunt-jsdeps · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
