npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

amdextract

v3.0.0

Published

Uses AST to extract AMD modules, their parts and an optimized output without unused dependencies while keeping the original format.

Downloads

194

Readme

amdextract Build Status Known Vulnerabilities

Uses AST to extract AMD modules, their parts and an optimized output without unused dependencies while keeping the original format.

example

source.js

define('module1', ['p1', 'p2'], function (a, b) {
  /**
   * b is not used in this scope.
   */
  return (function(b) {
    return b;
  })(a);
});

define('module2', ['p1', 'p2', 'p3', 'p4'], function (a, b, c) {
	return b;
});

example.js

var fs = require('fs');
var amdextract = require('amdextract');

var content = fs.readFileSync('source.js');
var result = amdextract.parse(content);

console.log(result.results.length + ' modules detected.');

result.results.forEach(function (r) {
	console.log('Unused paths: ' + r.unusedPaths.join(', '));
});

console.log('\nOptimized output:');
console.log(result.optimizedContent);

output

2 modules detected.
Unused paths: p2
Unused paths: p1, p3, p4

Optimized output:

define('module1', ['p1'], function (a) {
  /**
   * b is not used in this scope.
   */
  return (function(b) {
    return b;
  })(a);
});

define('module2', ['p2'], function (b) {
	return b;
});

methods

parse(content[, options])

content

Type: string JavaScript source for parsing.

options

excepts

Type: Array Default value: []

An array of strings or RegExps that represent dependency names that should not take into account.

exceptsPaths

Type: Array Default value: []

An array of strings or RegExps that represent dependency paths that should not take into account.

NOTE: exceptsPaths can also be declared before each module definition as a comment of strings of module paths separated by commas. This only applies on the underlying module definition.

source.js

/* exceptsPaths: p3 */
define(['p1', 'p2', 'p3'], function (a, b, c) {
	return b;
});

optimized-source.js

/* exceptsPaths: p3 */
define(['p2', 'p3'], function (b, c) {
	return b;
});

removeUnusedDependencies

Type: Boolean Default value: false

Removes unused dependencies from content and returns optimized content as optimizedContent property of result.

returns

Returns an object with the following properties:

results

Type: Array

An array of hash objects witch have this properties for each AMD module detected in content:

  • moduleId
  • paths
  • dependencies
  • unusedPaths
  • unusedDependencies

optimizedContent

Type: String

Optimized content (original content without unused dependencies). This property is available when the value of option removeUnusedDependencies is true.

Release History

  • 2015-12-27   v2.1.14   Remove "for in" loop in traverse.
  • 2015-12-26   v2.1.13   Remove unused function "extendRange".
  • 2015-12-26   v2.1.12   Fixed indentation of samples in README.md.
  • 2015-12-26   v2.1.11   Fixed a bug in "optimizedContent" property.
  • 2015-07-25   v2.1.10   Update travis CI.
  • 2015-07-25   v2.1.9   Update dependencies.
  • 2015-04-15   v2.1.8   Update README.
  • 2014-08-22   v2.1.6   Fix a bug when there are comments between paths and dependencies.
  • 2014-08-16   v2.1.0   Entirely uses AST.
  • 2014-07-21   v2.0.3   Fix an issue related to comment detection.
  • 2014-07-21   v2.0.2   Fix an issue related to RegExp literals.
  • 2014-07-20   v2.0.0   Uses AST to find out unused dependencies.
  • 2014-04-19   v1.1.0   Keeps the original separators of paths and dependencies.
  • 2014-04-09   v1.0.4   Fix a bug when dependencies length is grater than paths length.
  • 2014-03-29   v1.0.3   Fix a bug when specifying exceptsPaths in options only.
  • 2014-03-17   v1.0.0   First release.