This commit is contained in:
tatianamac
2019-11-26 14:50:43 -08:00
parent 8a55660ed0
commit 6d5445ecc5
13894 changed files with 2233957 additions and 0 deletions

3
node_modules/dependency-tree/.babelrc generated vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"presets": [ [ "@babel/env", { "targets": { "node": "6" } } ] ]
}

3
node_modules/dependency-tree/.deprc generated vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"root": "./"
}

3
node_modules/dependency-tree/.jscsrc generated vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"preset": "mrjoelkemp"
}

10
node_modules/dependency-tree/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,10 @@
language: node_js
node_js:
- 6
- 7
- 8
notifications:
email: false
sudo: false

21
node_modules/dependency-tree/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Dependents
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

115
node_modules/dependency-tree/README.md generated vendored Normal file
View File

@@ -0,0 +1,115 @@
### dependency-tree [![npm](http://img.shields.io/npm/v/dependency-tree.svg)](https://npmjs.org/package/dependency-tree) [![npm](http://img.shields.io/npm/dm/dependency-tree.svg)](https://npmjs.org/package/dependency-tree)
> Get the dependency tree of a module
`npm install --save dependency-tree`
* Works for JS (AMD, CommonJS, ES6 modules), Typescript, and CSS preprocessors (CSS (PostCSS), Sass, Stylus, and Less); basically, any module type supported by [Precinct](https://github.com/mrjoelkemp/node-precinct).
- For CommonJS modules, 3rd party dependencies (npm installed dependencies) are included in the tree by default
- Dependency path resolutions are handled by [filing-cabinet](https://github.com/mrjoelkemp/node-filing-cabinet)
- Supports RequireJS and Webpack loaders
* All core Node modules (assert, path, fs, etc) are removed from the dependency list by default
### Usage
```js
var dependencyTree = require('dependency-tree');
// Returns a dependency tree object for the given file
var tree = dependencyTree({
filename: 'path/to/a/file',
directory: 'path/to/all/files',
requireConfig: 'path/to/requirejs/config', // optional
webpackConfig: 'path/to/webpack/config', // optional
tsConfig: 'path/to/typescript/config', // optional
nodeModulesConfig: {
entry: 'module'
}, // optional
filter: path => path.indexOf('node_modules') === -1, // optional
nonExistent: [] // optional
});
// Returns a post-order traversal (list form) of the tree with duplicate sub-trees pruned.
// This is useful for bundling source files, because the list gives the concatenation order.
// Note: you can pass the same arguments as you would to dependencyTree()
var list = dependencyTree.toList({
filename: 'path/to/a/file',
directory: 'path/to/all/files'
});
```
#### Options
* `requireConfig`: path to a requirejs config for AMD modules (allows for the result of aliased module paths)
* `webpackConfig`: path to a webpack config for aliased modules
* `tsConfig`: path to a typescript config (or a preloaded object representing the typescript config)
* `nodeModulesConfig`: config for resolving entry file for node_modules
* `visited`: object used for avoiding redundant subtree generations via memoization.
* `nonExistent`: array used for storing the list of partial paths that do not exist
* `filter`: a function used to determine if a module (and its subtree) should be included in the dependency tree
- The first argument given to the filter is an absolute filepath to the dependency and the second is the filepath to the currently traversed file. Should return a `Boolean`. If it returns `true`, the module is included in the resulting tree.
* `detective`: object with configuration specific to detectives used to find dependencies of a file
- for example `detective.amd.skipLazyLoaded: true` tells the AMD detective to omit inner requires
- See [precinct's usage docs](https://github.com/dependents/node-precinct#usage) for the list of module types you can pass options to.
#### Format Details
The object form is a mapping of the dependency tree to the filesystem
where every key is an absolute filepath and the value is another object/subtree.
Example:
```js
{
'/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/a.js': {
'/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/b.js': {
'/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/d.js': {},
'/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/e.js': {}
},
'/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/c.js': {
'/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/f.js': {},
'/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/g.js': {}
}
}
}
```
This structure was chosen to serve as a visual representation of the dependency tree
for use in the [Dependents](https://github.com/mrjoelkemp/sublime-dependents) plugin.
##### CLI version
* Assumes a global install: `npm install -g dependency-tree`
```
dependency-tree --directory=path/to/all/supported/files [--list-form] [-c path/to/require/config] [-w path/to/webpack/config] filename
```
Prints the dependency tree of the given filename as stringified json (by default).
* You can alternatively print out the list form one element per line using the `--list-form` option.
### How does this work?
Dependency tree takes in a starting file, extracts its declared dependencies via [precinct](https://github.com/dependents/node-precinct/), resolves each of those dependencies to a file on the filesystem via [filing-cabinet](https://github.com/dependents/node-filing-cabinet), then recursively performs those steps until there are no more dependencies to process.
In more detail, the starting file is passed to precinct to extract dependencies. Dependency-tree doesn't care about how to extract dependencies, so it delegates that work to precinct: which is a multi-language dependency extractor; we'll focus on JavaScript tree generation for this example. To do the extraction, precinct delegates the abstract-syntax-tree (AST) generation to the default parser for [node-source-walk](https://github.com/dependents/node-source-walk). Precinct uses the AST to determine what type of JS module the file is (Commonjs, AMD, or ES6) and then delegates to the "detective" that's appropriate for that module type. The "detective" contains the logic for how to extract dependencies based on the module syntax format; i.e., the way dependencies are declared in commonjs is different than in AMD (which has 4 ways of doing that, for example).
After using the detective to get the (raw, like './foobar') dependency strings, precinct passes that back to dependency-tree. Of course, in order to find the dependencies in './foobar', we need to resolve that dependency to a real file on the filesystem. To do this, dependency-tree delegates that task to filing-cabinet: which is a multi-language dependency resolver.
Filing-cabinet reuses (for performance) the AST that precinct made node-source-walk generate. It then does a similar check on the AST to see which module type (commonjs, amd, or es6) is being used in the file (again, we're assuming a regular JS file for this example) and then delegates to the appropriate resolver for that module type. We need different resolvers because a dependency name in AMD could be aliased via a requirejs config. Similarly, commonjs has its own algorithm for resolving dependencies.
So after the appropriate resolver finds the file on the filesystem, filing-cabinet has successfully mapped a raw dependency name to a file on the filesystem. Now, dependency-tree has a file that it can also traverse (repeating exactly what was done for the starting file).
At the end of traversing every file (in a depth-first fashion), we have a fully populated dependency tree. :dancers:
### FAQ
#### Why aren't some some dependencies being detected?
If there are bugs in [precinct](https://github.com/dependents/node-precinct) or if the `requireConfig`/`webpackConfig`/`tsConfig` options are incomplete,
some dependencies may not be resolved. The optional array passed to the `nonExistent` option will be populated with paths
that could not be resolved. You can check this array to see where problems might exist.
You can also use the `DEBUG=*` env variable along with the cli version to see debugging information explaining where resolution went wrong.
Example: `DEBUG=* dependency-tree -w path/to/webpack.config.json path/to/a/file`

39
node_modules/dependency-tree/bin/cli.js generated vendored Executable file
View File

@@ -0,0 +1,39 @@
#!/usr/bin/env node
'use strict';
const dependencyTree = require('../');
const program = require('commander');
program
.version(require('../package.json').version)
.usage('[options] <filename>')
.option('-d, --directory <path>', 'location of files of supported filetypes')
.option('-c, --require-config <path>', 'path to a requirejs config')
.option('-w, --webpack-config <path>', 'path to a webpack config')
.option('-t, --ts-config <path>', 'path to a typescript config')
.option('--list-form', 'output the list form of the tree (one element per line)')
.parse(process.argv);
let tree;
const options = {
filename: program.args[0],
root: program.directory,
config: program.requireConfig,
webpackConfig: program.webpackConfig,
tsConfig: program.tsConfig
};
if (program.listForm) {
tree = dependencyTree.toList(options);
tree.forEach(function(node) {
console.log(node);
});
} else {
tree = dependencyTree(options);
console.log(JSON.stringify(tree));
}

199
node_modules/dependency-tree/index.js generated vendored Normal file
View File

@@ -0,0 +1,199 @@
'use strict';
const precinct = require('precinct');
const path = require('path');
const fs = require('fs');
const cabinet = require('filing-cabinet');
const debug = require('debug')('tree');
const Config = require('./lib/Config');
/**
* Recursively find all dependencies (avoiding circular) traversing the entire dependency tree
* and returns a flat list of all unique, visited nodes
*
* @param {Object} options
* @param {String} options.filename - The path of the module whose tree to traverse
* @param {String} options.directory - The directory containing all JS files
* @param {String} [options.requireConfig] - The path to a requirejs config
* @param {String} [options.webpackConfig] - The path to a webpack config
* @param {String} [options.nodeModulesConfig] - config for resolving entry file for node_modules
* @param {Object} [options.visited] - Cache of visited, absolutely pathed files that should not be reprocessed.
* Format is a filename -> tree as list lookup table
* @param {Array} [options.nonExistent] - List of partials that do not exist
* @param {Boolean} [options.isListForm=false]
* @param {String|Object} [options.tsConfig] Path to a typescript config (or a preloaded one).
* @return {Object}
*/
module.exports = function(options) {
const config = new Config(options);
if (!fs.existsSync(config.filename)) {
debug('file ' + config.filename + ' does not exist');
return config.isListForm ? [] : {};
}
const results = traverse(config);
debug('traversal complete', results);
dedupeNonExistent(config.nonExistent);
debug('deduped list of nonExistent partials: ', config.nonExistent);
let tree;
if (config.isListForm) {
debug('list form of results requested');
tree = Array.from(results);
} else {
debug('object form of results requested');
tree = {};
tree[config.filename] = results;
}
debug('final tree', tree);
return tree;
};
/**
* Executes a post-order depth first search on the dependency tree and returns a
* list of absolute file paths. The order of files in the list will be the
* proper concatenation order for bundling.
*
* In other words, for any file in the list, all of that file's dependencies (direct or indirect) will appear at
* lower indices in the list. The root (entry point) file will therefore appear last.
*
* The list will not contain duplicates.
*
* Params are those of module.exports
*/
module.exports.toList = function(options) {
options.isListForm = true;
return module.exports(options);
};
/**
* Returns the list of dependencies for the given filename
*
* Protected for testing
*
* @param {Config} config
* @return {Array}
*/
module.exports._getDependencies = function(config) {
let dependencies;
const precinctOptions = config.detectiveConfig;
precinctOptions.includeCore = false;
try {
dependencies = precinct.paperwork(config.filename, precinctOptions);
debug('extracted ' + dependencies.length + ' dependencies: ', dependencies);
} catch (e) {
debug('error getting dependencies: ' + e.message);
debug(e.stack);
return [];
}
const resolvedDependencies = [];
for (let i = 0, l = dependencies.length; i < l; i++) {
const dep = dependencies[i];
const result = cabinet({
partial: dep,
filename: config.filename,
directory: config.directory,
ast: precinct.ast,
config: config.requireConfig,
webpackConfig: config.webpackConfig,
nodeModulesConfig: config.nodeModulesConfig,
tsConfig: config.tsConfig
});
if (!result) {
debug('skipping an empty filepath resolution for partial: ' + dep);
config.nonExistent.push(dep);
continue;
}
const exists = fs.existsSync(result);
if (!exists) {
config.nonExistent.push(dep);
debug('skipping non-empty but non-existent resolution: ' + result + ' for partial: ' + dep);
continue;
}
resolvedDependencies.push(result);
}
return resolvedDependencies;
};
/**
* @param {Config} config
* @return {Object|Set}
*/
function traverse(config) {
let subTree = config.isListForm ? new Set() : {};
debug('traversing ' + config.filename);
if (config.visited[config.filename]) {
debug('already visited ' + config.filename);
return config.visited[config.filename];
}
let dependencies = module.exports._getDependencies(config);
debug('cabinet-resolved all dependencies: ', dependencies);
// Prevents cycles by eagerly marking the current file as read
// so that any dependent dependencies exit
config.visited[config.filename] = config.isListForm ? [] : {};
if (config.filter) {
debug('using filter function to filter out dependencies');
debug('unfiltered number of dependencies: ' + dependencies.length);
dependencies = dependencies.filter(function(filePath) {
return config.filter(filePath, config.filename);
});
debug('filtered number of dependencies: ' + dependencies.length);
}
for (let i = 0, l = dependencies.length; i < l; i++) {
const d = dependencies[i];
const localConfig = config.clone();
localConfig.filename = d;
if (localConfig.isListForm) {
for (let item of traverse(localConfig)) {
subTree.add(item);
}
} else {
subTree[d] = traverse(localConfig);
}
}
if (config.isListForm) {
subTree.add(config.filename);
config.visited[config.filename].push(...subTree);
} else {
config.visited[config.filename] = subTree;
}
return subTree;
}
// Mutate the list input to do a dereferenced modification of the user-supplied list
function dedupeNonExistent(nonExistent) {
const deduped = new Set(nonExistent);
nonExistent.length = deduped.size;
let i = 0;
for (const elem of deduped) {
nonExistent[i] = elem;
i++;
}
}

38
node_modules/dependency-tree/lib/Config.js generated vendored Normal file
View File

@@ -0,0 +1,38 @@
'use strict';
const path = require('path');
const debug = require('debug')('tree');
class Config {
constructor(options) {
this.filename = options.filename;
this.directory = options.directory || options.root;
this.visited = options.visited || {};
this.nonExistent = options.nonExistent || [];
this.isListForm = options.isListForm;
this.requireConfig = options.config || options.requireConfig;
this.webpackConfig = options.webpackConfig;
this.nodeModulesConfig = options.nodeModulesConfig;
this.detectiveConfig = options.detective || options.detectiveConfig || {};
this.tsConfig = options.tsConfig;
this.filter = options.filter;
if (!this.filename) { throw new Error('filename not given'); }
if (!this.directory) { throw new Error('directory not given'); }
if (this.filter && typeof this.filter !== 'function') { throw new Error('filter must be a function'); }
debug('given filename: ' + this.filename);
this.filename = path.resolve(process.cwd(), this.filename);
debug('resolved filename: ' + this.filename);
debug('visited: ', this.visited);
}
clone () {
return new Config(this);
}
}
module.exports = Config;

90
node_modules/dependency-tree/package.json generated vendored Normal file
View File

@@ -0,0 +1,90 @@
{
"_args": [
[
"dependency-tree@7.0.2",
"/Users/tatiana/selfdefined"
]
],
"_from": "dependency-tree@7.0.2",
"_id": "dependency-tree@7.0.2",
"_inBundle": false,
"_integrity": "sha512-yh3qCLHl/o/ZUPq14HUF6AGEbNTMyCAu92D6AmY3SEynJHkB25o3hTDzvt3Tu/KpR0093ATyrhr4aGwcx8NnVw==",
"_location": "/dependency-tree",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "dependency-tree@7.0.2",
"name": "dependency-tree",
"escapedName": "dependency-tree",
"rawSpec": "7.0.2",
"saveSpec": null,
"fetchSpec": "7.0.2"
},
"_requiredBy": [
"/@11ty/eleventy"
],
"_resolved": "https://registry.npmjs.org/dependency-tree/-/dependency-tree-7.0.2.tgz",
"_spec": "7.0.2",
"_where": "/Users/tatiana/selfdefined",
"author": {
"name": "Joel Kemp",
"email": "joel@mrjoelkemp.com"
},
"bin": {
"dependency-tree": "bin/cli.js"
},
"bugs": {
"url": "https://github.com/mrjoelkemp/node-dependency-tree/issues"
},
"dependencies": {
"commander": "^2.19.0",
"debug": "^4.1.1",
"filing-cabinet": "^2.3.1",
"precinct": "^6.1.1"
},
"description": "Get the dependency tree of a module",
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@babel/register": "^7.0.0",
"jscs": "^3.0.7",
"jscs-preset-mrjoelkemp": "~2.0.0",
"mocha": "^5.0.0",
"mock-fs": "^4.7.0",
"resolve": "^1.9.0",
"rewire": "^4.0.1",
"sinon": "^6.1.5"
},
"engines": {
"node": ">=6.0.0"
},
"homepage": "https://github.com/mrjoelkemp/node-dependency-tree",
"keywords": [
"dependency",
"tree",
"graph",
"module",
"ast",
"requirejs",
"AMD",
"commonjs",
"es6",
"sass",
"stylus",
"less",
"typescript"
],
"license": "MIT",
"main": "index.js",
"name": "dependency-tree",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/mrjoelkemp/node-dependency-tree.git"
},
"scripts": {
"test": "jscs index.js test/test.js && ./node_modules/.bin/mocha --require @babel/register test/test.js"
},
"version": "7.0.2"
}

8
node_modules/dependency-tree/test/example/amd/a.js generated vendored Normal file
View File

@@ -0,0 +1,8 @@
define([
'./b',
'./c'
], function(b, c) {
'use strict';
return 'cool';
});

7
node_modules/dependency-tree/test/example/amd/b.js generated vendored Normal file
View File

@@ -0,0 +1,7 @@
define([
'./c'
], function(c) {
'use strict';
return c;
});

1
node_modules/dependency-tree/test/example/amd/c.js generated vendored Normal file
View File

@@ -0,0 +1 @@
define({});

View File

@@ -0,0 +1,2 @@
var b = require('./b');
var c = require('./c');

View File

@@ -0,0 +1,3 @@
var path = require('path');
module.exports = {};

View File

@@ -0,0 +1,3 @@
var fs = require('fs');
module.exports = {};

View File

@@ -0,0 +1,2 @@
var b = require('./b');
var c = require('./c');

View File

@@ -0,0 +1,2 @@
var d = require('./d');
var e = require('./e');

View File

@@ -0,0 +1,2 @@
var f = require('./f');
var g = require('./g');

View File

View File

View File

View File

View File

@@ -0,0 +1,3 @@
var path = require('path');
var debug = require('debug');
var notReal = require('not-real');

View File

@@ -0,0 +1,6 @@
{
"version": "1.0.0",
"compilerOptions": {
"module": "commonjs"
}
}

2
node_modules/dependency-tree/test/example/ts/a.ts generated vendored Normal file
View File

@@ -0,0 +1,2 @@
import b from './b';
import c from './c';

1
node_modules/dependency-tree/test/example/ts/b.ts generated vendored Normal file
View File

@@ -0,0 +1 @@
export default function() {};

1
node_modules/dependency-tree/test/example/ts/c.ts generated vendored Normal file
View File

@@ -0,0 +1 @@
export default function() {};

3
node_modules/dependency-tree/test/example/ts/d.tsx generated vendored Normal file
View File

@@ -0,0 +1,3 @@
import c from './c';
export default <div>Hi</div>;

View File

@@ -0,0 +1 @@
var foo = require('F');

View File

@@ -0,0 +1 @@
var cabinet = require('filing-cabinet');

906
node_modules/dependency-tree/test/test.js generated vendored Normal file
View File

@@ -0,0 +1,906 @@
import assert from 'assert';
import sinon from 'sinon';
import mockfs from 'mock-fs';
import path from 'path';
import precinct from 'precinct';
import rewire from 'rewire';
import Config from '../lib/Config';
const dependencyTree = rewire('../');
describe('dependencyTree', function() {
this.timeout(8000);
function testTreesForFormat(format, ext = '.js') {
it('returns an object form of the dependency tree for a file', function() {
const root = `${__dirname}/example/${format}`;
const filename = `${root}/a${ext}`;
const tree = dependencyTree({filename, root});
assert(tree instanceof Object);
const aSubTree = tree[filename];
assert.ok(aSubTree instanceof Object);
const filesInSubTree = Object.keys(aSubTree);
assert.equal(filesInSubTree.length, 2);
});
}
function mockStylus() {
mockfs({
[__dirname + '/example/stylus']: {
'a.styl': `
@import "b"
@require "c.styl"
`,
'b.styl': '@import "c"',
'c.styl': ''
}
});
}
function mockSass() {
mockfs({
[__dirname + '/example/sass']: {
'a.scss': `
@import "_b";
@import "_c.scss";
`,
'_b.scss': 'body { color: blue; }',
'_c.scss': 'body { color: pink; }'
}
});
}
function mockLess() {
mockfs({
[__dirname + '/example/less']: {
'a.less': `
@import "b.css";
@import "c.less";
`,
'b.css': 'body { color: blue; }',
'c.less': 'body { color: pink; }'
}
});
}
function mockes6() {
mockfs({
[__dirname + '/example/es6']: {
'a.js': `
import b from './b';
import c from './c';
`,
'b.js': 'export default function() {};',
'c.js': 'export default function() {};',
'jsx.js': `import c from './c';\n export default <jsx />;`,
'foo.jsx': `import React from 'react';\n import b from 'b';\n export default <jsx />;`,
'es7.js': `import c from './c';\n export default async function foo() {};`
}
});
}
afterEach(function() {
mockfs.restore();
});
it('returns an empty object for a non-existent filename', function() {
mockfs({
imaginary: {}
});
const root = __dirname + '/imaginary';
const filename = root + '/notafile.js';
const tree = dependencyTree({filename, root});
assert(tree instanceof Object);
assert(!Object.keys(tree).length);
});
it('handles nested tree structures', function() {
const directory = __dirname + '/example/extended';
const filename = directory + '/a.js';
const tree = dependencyTree({filename, directory});
assert(tree[filename] instanceof Object);
// b and c
const subTree = tree[filename];
assert.equal(Object.keys(subTree).length, 2);
const bTree = subTree[directory + '/b.js'];
const cTree = subTree[directory + '/c.js'];
// d and e
assert.equal(Object.keys(bTree).length, 2);
// f ang g
assert.equal(Object.keys(cTree).length, 2);
});
it('does not include files that are not real (#13)', function() {
mockfs({
[__dirname + '/onlyRealDeps']: {
'a.js': 'var notReal = require("./notReal");'
}
});
const directory = __dirname + '/onlyRealDeps';
const filename = directory + '/a.js';
const tree = dependencyTree({filename, directory});
const subTree = tree[filename];
assert.ok(!Object.keys(subTree).some(dep => dep.indexOf('notReal') !== -1));
});
it('does not choke on cyclic dependencies', function() {
mockfs({
[__dirname + '/cyclic']: {
'a.js': 'var b = require("./b");',
'b.js': 'var a = require("./a");'
}
});
const directory = __dirname + '/cyclic';
const filename = directory + '/a.js';
const spy = sinon.spy(dependencyTree, '_getDependencies');
const tree = dependencyTree({filename, directory});
assert(spy.callCount === 2);
assert(Object.keys(tree[filename]).length);
dependencyTree._getDependencies.restore();
});
it('excludes Nodejs core modules by default', function() {
const directory = __dirname + '/example/commonjs';
const filename = directory + '/b.js';
const tree = dependencyTree({filename, directory});
assert(Object.keys(tree[filename]).length === 0);
assert(Object.keys(tree)[0].indexOf('b.js') !== -1);
});
it('traverses installed 3rd party node modules', function() {
const directory = __dirname + '/example/onlyRealDeps';
const filename = directory + '/a.js';
const tree = dependencyTree({filename, directory});
const subTree = tree[filename];
assert(Object.keys(subTree).some(dep => dep === require.resolve('debug')));
});
it('returns a list of absolutely pathed files', function() {
const directory = __dirname + '/example/commonjs';
const filename = directory + '/b.js';
const tree = dependencyTree({filename, directory});
for (let node in tree.nodes) {
assert(node.indexOf(process.cwd()) !== -1);
}
});
it('excludes duplicate modules from the tree', function() {
mockfs({
root: {
// More than one module includes c
'a.js': `import b from "b";
import c from "c";`,
'b.js': 'import c from "c";',
'c.js': 'export default 1;'
}
});
const tree = dependencyTree.toList({
filename: 'root/a.js',
directory: 'root'
});
assert(tree.length === 3);
});
describe('when given a detective configuration', function() {
it('passes it through to precinct', function() {
const spy = sinon.spy(precinct, 'paperwork');
const directory = __dirname + '/example/onlyRealDeps';
const filename = directory + '/a.js';
const detectiveConfig = {
amd: {
skipLazyLoaded: true
}
};
dependencyTree({
filename,
directory,
detective: detectiveConfig
});
assert.ok(spy.calledWith(filename, detectiveConfig));
spy.restore();
});
});
describe('when given a list to store non existent partials', function() {
describe('and the file contains no valid partials', function() {
it('stores the invalid partials', function() {
mockfs({
[__dirname + '/onlyRealDeps']: {
'a.js': 'var notReal = require("./notReal");'
}
});
const directory = __dirname + '/onlyRealDeps';
const filename = directory + '/a.js';
const nonExistent = [];
const tree = dependencyTree({filename, directory, nonExistent});
assert.equal(nonExistent.length, 1);
assert.equal(nonExistent[0], './notReal');
});
});
describe('and the file contains all valid partials', function() {
it('does not store anything', function() {
mockfs({
[__dirname + '/onlyRealDeps']: {
'a.js': 'var b = require("./b");',
'b.js': 'export default 1;'
}
});
const directory = __dirname + '/onlyRealDeps';
const filename = directory + '/a.js';
const nonExistent = [];
const tree = dependencyTree({filename, directory, nonExistent});
assert.equal(nonExistent.length, 0);
});
});
describe('and the file contains a mix of invalid and valid partials', function() {
it('stores the invalid ones', function() {
mockfs({
[__dirname + '/onlyRealDeps']: {
'a.js': 'var b = require("./b");',
'b.js': 'var c = require("./c"); export default 1;',
'c.js': 'var crap = require("./notRealMan");'
}
});
const directory = __dirname + '/onlyRealDeps';
const filename = directory + '/a.js';
const nonExistent = [];
const tree = dependencyTree({filename, directory, nonExistent});
assert.equal(nonExistent.length, 1);
assert.equal(nonExistent[0], './notRealMan');
});
});
describe('and there is more than one reference to the invalid partial', function() {
it('only includes the non-existent partial once', function() {
mockfs({
[__dirname + '/onlyRealDeps']: {
'a.js': 'var b = require("./b");\nvar crap = require("./notRealMan");',
'b.js': 'var c = require("./c"); export default 1;',
'c.js': 'var crap = require("./notRealMan");'
}
});
const directory = __dirname + '/onlyRealDeps';
const filename = directory + '/a.js';
const nonExistent = [];
const tree = dependencyTree({filename, directory, nonExistent});
assert.equal(nonExistent.length, 1);
assert.equal(nonExistent[0], './notRealMan');
});
});
});
describe('throws', function() {
beforeEach(function() {
this._directory = __dirname + '/example/commonjs';
this._revert = dependencyTree.__set__('traverse', () => []);
});
afterEach(function() {
this._revert();
});
it('throws if the filename is missing', function() {
assert.throws(function() {
dependencyTree({
filename: undefined,
directory: this._directory
});
});
});
it('throws if the root is missing', function() {
assert.throws(function() {
dependencyTree({filename});
});
});
it('throws if a supplied filter is not a function', function() {
assert.throws(function() {
const directory = __dirname + '/example/onlyRealDeps';
const filename = directory + '/a.js';
const tree = dependencyTree({
filename,
directory,
filter: 'foobar'
});
});
});
it('does not throw on the legacy `root` option', function() {
assert.doesNotThrow(function() {
const directory = __dirname + '/example/onlyRealDeps';
const filename = directory + '/a.js';
const tree = dependencyTree({
filename,
root: directory
});
});
});
});
describe('on file error', function() {
beforeEach(function() {
this._directory = __dirname + '/example/commonjs';
});
it('does not throw', function() {
assert.doesNotThrow(() => {
dependencyTree({
filename: 'foo',
directory: this._directory
});
});
});
it('returns no dependencies', function() {
const tree = dependencyTree({filename: 'foo', directory: this._directory});
assert(!tree.length);
});
});
describe('when a filter function is supplied', function() {
it('uses the filter to determine if a file should be included in the results', function() {
const directory = __dirname + '/example/onlyRealDeps';
const filename = directory + '/a.js';
const tree = dependencyTree({
filename,
directory,
// Skip all 3rd party deps
filter: (filePath, moduleFile) => {
assert.ok(require.resolve('debug'));
assert.ok(moduleFile.match('test/example/onlyRealDeps/a.js'));
return filePath.indexOf('node_modules') === -1;
}
});
const subTree = tree[filename];
assert.ok(Object.keys(tree).length);
const has3rdPartyDep = Object.keys(subTree).some(dep => dep === require.resolve('debug'));
assert.ok(!has3rdPartyDep);
});
});
describe('memoization (#2)', function() {
beforeEach(function() {
this._spy = sinon.spy(dependencyTree, '_getDependencies');
});
afterEach(function() {
dependencyTree._getDependencies.restore();
});
it('accepts a cache object for memoization (#2)', function() {
const filename = __dirname + '/example/amd/a.js';
const directory = __dirname + '/example/amd';
const cache = {};
cache[__dirname + '/example/amd/b.js'] = [
__dirname + '/example/amd/b.js',
__dirname + '/example/amd/c.js'
];
const tree = dependencyTree({
filename,
directory,
visited: cache
});
assert.equal(Object.keys(tree[filename]).length, 2);
assert(this._spy.neverCalledWith(__dirname + '/example/amd/b.js'));
});
it('returns the precomputed list of a cached entry point', function() {
const filename = __dirname + '/example/amd/a.js';
const directory = __dirname + '/example/amd';
const cache = {
// Shouldn't process the first file's tree
[filename]: []
};
const tree = dependencyTree({
filename,
directory,
visited: cache
});
assert(!tree.length);
});
});
describe('module formats', function() {
describe('amd', function() {
testTreesForFormat('amd');
});
describe('commonjs', function() {
testTreesForFormat('commonjs');
describe('when given a CJS file with lazy requires', function() {
beforeEach(function() {
mockfs({
[__dirname + '/cjs']: {
'foo.js': 'module.exports = function(bar = require("./bar")) {};',
'bar.js': 'module.exports = 1;'
}
});
});
it('includes the lazy dependency', function() {
const directory = __dirname + '/cjs';
const filename = directory + '/foo.js';
const tree = dependencyTree({filename, directory});
const subTree = tree[filename];
assert.ok(`${directory}/bar.js` in subTree);
});
});
describe('when given a CJS file with module property in package.json', function() {
beforeEach(function() {
mockfs({
[__dirname + '/es6']: {
['module.entry.js']: 'import * as module from "module.entry"',
['node_modules']: {
['module.entry']: {
'index.main.js': 'module.exports = function() {};',
'index.module.js': 'module.exports = function() {};',
'package.json': '{ "main": "index.main.js", "module": "index.module.js" }'
}
}
}
});
});
it('it includes the module entry as dependency', function() {
const directory = __dirname + '/es6';
const filename = directory + '/module.entry.js';
const tree = dependencyTree({
filename,
directory,
nodeModulesConfig: {
entry: 'module'
}
});
const subTree = tree[filename];
assert.ok(`${directory}/node_modules/module.entry/index.module.js` in subTree);
});
});
});
describe('es6', function() {
beforeEach(function() {
this._directory = __dirname + '/example/es6';
mockes6();
});
testTreesForFormat('es6');
it('resolves files that have jsx', function() {
const filename = `${this._directory}/jsx.js`;
const {[filename]: tree} = dependencyTree({
filename,
directory: this._directory
});
assert.ok(tree[`${this._directory}/c.js`]);
});
it('resolves files with a jsx extension', function() {
const filename = `${this._directory}/foo.jsx`;
const {[filename]: tree} = dependencyTree({
filename,
directory: this._directory
});
assert.ok(tree[`${this._directory}/b.js`]);
});
it('resolves files that have es7', function() {
const filename = `${this._directory}/es7.js`;
const {[filename]: tree} = dependencyTree({
filename,
directory: this._directory
});
assert.ok(tree[`${this._directory}/c.js`]);
});
describe('when given an es6 file using CJS lazy requires', function() {
beforeEach(function() {
mockfs({
[__dirname + '/es6']: {
'foo.js': 'export default function(bar = require("./bar")) {};',
'bar.js': 'export default 1;'
}
});
});
describe('and mixedImport mode is turned on', function() {
it('includes the lazy dependency', function() {
const directory = __dirname + '/es6';
const filename = directory + '/foo.js';
const tree = dependencyTree({
filename,
directory,
detective: {
es6: {
mixedImports: true
}
}
});
const subTree = tree[filename];
assert.ok(`${directory}/bar.js` in subTree);
});
it('also works for toList', function() {
const directory = __dirname + '/es6';
const filename = directory + '/foo.js';
const results = dependencyTree.toList({
filename,
directory,
detective: {
es6: {
mixedImports: true
}
}
});
assert.equal(results[0], `${directory}/bar.js`);
assert.equal(results[1], filename);
});
});
describe('and mixedImport mode is turned off', function() {
it('does not include the lazy dependency', function() {
const directory = __dirname + '/es6';
const filename = directory + '/foo.js';
const tree = dependencyTree({
filename,
directory
});
const subTree = tree[filename];
assert.ok(!(`${directory}/bar.js` in subTree));
});
});
});
describe('when given an es6 file using dynamic imports', function() {
beforeEach(function() {
mockfs({
[__dirname + '/es6']: {
'foo.js': 'import("./bar");',
'bar.js': 'export default 1;'
}
});
});
it('includes the dynamic import', function() {
const directory = __dirname + '/es6';
const filename = directory + '/foo.js';
const tree = dependencyTree({
filename,
directory
});
const subTree = tree[filename];
assert.ok(!(`${directory}/bar.js` in subTree));
});
});
});
describe('sass', function() {
beforeEach(function() {
mockSass();
});
testTreesForFormat('sass', '.scss');
});
describe('stylus', function() {
beforeEach(function() {
mockStylus();
});
testTreesForFormat('stylus', '.styl');
});
describe('less', function() {
beforeEach(function() {
mockLess();
});
testTreesForFormat('less', '.less');
});
describe('typescript', function() {
testTreesForFormat('ts', '.ts');
it('utilizes a tsconfig', function() {
const directory = path.join(__dirname, 'example/ts');
const tsConfigPath = path.join(directory, '.tsconfig');
const results = dependencyTree.toList({
filename: `${__dirname}/example/ts/a.ts`,
directory,
tsConfig: tsConfigPath
});
assert.equal(results[0], path.join(directory, 'b.ts'));
assert.equal(results[1], path.join(directory, 'c.ts'));
assert.equal(results[2], path.join(directory, 'a.ts'));
});
it('supports tsx files', function() {
const directory = path.join(__dirname, 'example/ts');
const results = dependencyTree.toList({
filename: `${__dirname}/example/ts/d.tsx`,
directory
});
assert.equal(results[0], path.join(directory, 'c.ts'));
});
});
});
describe('toList', function() {
function testToList(format, ext = '.js') {
it('returns a post-order list form of the dependency tree', function() {
const directory = __dirname + '/example/' + format;
const filename = directory + '/a' + ext;
const list = dependencyTree.toList({
filename,
directory
});
assert(list instanceof Array);
assert(list.length);
});
}
it('returns an empty list on a non-existent filename', function() {
mockfs({
imaginary: {}
});
const directory = __dirname + '/imaginary';
const filename = directory + '/notafile.js';
const list = dependencyTree.toList({
filename,
directory
});
assert(list instanceof Array);
assert(!list.length);
});
it('orders the visited files by last visited', function() {
const directory = __dirname + '/example/amd';
const filename = directory + '/a.js';
const list = dependencyTree.toList({
filename,
directory
});
assert(list.length === 3);
assert(list[0] === directory + '/c.js');
assert(list[1] === directory + '/b.js');
assert(list[list.length - 1] === filename);
});
describe('module formats', function() {
describe('amd', function() {
testToList('amd');
});
describe('commonjs', function() {
testToList('commonjs');
});
describe('es6', function() {
beforeEach(function() {
mockes6();
});
testToList('es6');
});
describe('sass', function() {
beforeEach(function() {
mockSass();
});
testToList('sass', '.scss');
});
describe('stylus', function() {
beforeEach(function() {
mockStylus();
});
testToList('stylus', '.styl');
});
describe('less', function() {
beforeEach(function() {
mockLess();
});
testToList('less', '.less');
});
describe('typescript', function() {
testToList('ts', '.ts');
});
});
});
describe('webpack', function() {
beforeEach(function() {
// Note: not mocking because webpack's resolver needs a real project with dependencies;
// otherwise, we'd have to mock a ton of files.
this._root = path.join(__dirname, '../');
this._webpackConfig = this._root + '/webpack.config.js';
this._testResolution = name => {
const results = dependencyTree.toList({
filename: `${__dirname}/example/webpack/${name}.js`,
directory: this._root,
webpackConfig: this._webpackConfig,
filter: filename => filename.indexOf('filing-cabinet') !== -1
});
assert.ok(results.some(filename => filename.indexOf('node_modules/filing-cabinet') !== -1));
};
});
it('resolves aliased modules', function() {
this.timeout(5000);
this._testResolution('aliased');
});
it('resolves unaliased modules', function() {
this.timeout(5000);
this._testResolution('unaliased');
});
});
describe('requirejs', function() {
beforeEach(function() {
mockfs({
root: {
'lodizzle.js': 'define({})',
'require.config.js': `
requirejs.config({
baseUrl: './',
paths: {
F: './lodizzle.js'
}
});
`,
'a.js': `
define([
'F'
], function(F) {
});
`,
'b.js': `
define([
'./lodizzle'
], function(F) {
});
`
}
});
});
it('resolves aliased modules', function() {
const tree = dependencyTree({
filename: 'root/a.js',
directory: 'root',
config: 'root/require.config.js'
});
const filename = path.resolve(process.cwd(), 'root/a.js');
const aliasedFile = path.resolve(process.cwd(), 'root/lodizzle.js');
assert.ok('root/lodizzle.js' in tree[filename]);
});
it('resolves non-aliased paths', function() {
const tree = dependencyTree({
filename: 'root/b.js',
directory: 'root',
config: 'root/require.config.js'
});
const filename = path.resolve(process.cwd(), 'root/b.js');
const aliasedFile = path.resolve(process.cwd(), 'root/lodizzle.js');
assert.ok('root/lodizzle.js' in tree[filename]);
});
});
describe('Config', function() {
describe('when cloning', function() {
describe('and a detective config was set', function() {
it('retains the detective config in the clone', function() {
const detectiveConfig = {
es6: {
mixedImports: true
}
};
const config = new Config({
detectiveConfig,
filename: 'foo',
directory: 'bar'
});
const clone = config.clone();
assert.deepEqual(clone.detectiveConfig, detectiveConfig);
});
});
});
});
});

10
node_modules/dependency-tree/webpack.config.js generated vendored Normal file
View File

@@ -0,0 +1,10 @@
'use strict';
module.exports = {
entry: './index.js',
resolve: {
alias: {
F: './node_modules/filing-cabinet'
}
}
};