Giter VIP home page Giter VIP logo

grunt-amdcheck's Introduction

grunt-amdcheck Known Vulnerabilities

Uses AST to find and remove unused dependencies in AMD modules.

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-amdcheck --save-dev

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

grunt.loadNpmTasks('grunt-amdcheck');

The "amdcheck" task

Overview

When an AMD-based project grows, so does the number of its JS files. Some dependencies of previously used modules may no longer be necessary. The AMD module loader (e.g. requirejs) loads those useless dependencies from the network, which can increase initial page load time. This grunt plugin can detect and remove those useless dependencies without modifying source files.

example

source.js

define('module1', ['p1', 'p2'], function (a, b) {
  return a;
});

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

optimized-source.js

define('module1', ['p1'], function (a) {
  return a;
});

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

Options

excepts

Type: Array
Default value: []

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

exceptsPaths

Type: Array
Default value: []

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

NOTE: exceptsPaths, followed by a comma-separated list of module paths can be declared before each module definition. This exception is only applied to 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;
});

strict

Type: boolean
Default value: false

When strict is true, throws an error if at least one unused dependency path or dependency name was found.

logModuleId

Type: boolean
Default value: false

Logs the id of the module if the module id is specified.

logDependencyPaths

Type: boolean
Default value: false

Logs the list of dependency paths of the module.

logDependencyNames

Type: boolean
Default value: false

Logs the list of dependency names of the module.

logUnusedDependencyPaths

Type: boolean
Default value: true

Logs the list of unused dependency paths of the module.

logUnusedDependencyNames

Type: boolean
Default value: false

Logs the list of unused dependency names of the module.

removeUnusedDependencies

Type: boolean
Default value: true

Removes detected unused dependencies and saves the new files.

saveFilesWithUnusedDependenciesOnly

Type: boolean
Default value: false

When removing unused dependencies (i.e. removeUnusedDependencies: true), only write out files that have unused dependencies.

Usage Examples

grunt.initConfig({
  amdcheck: {
    dev: {
      options: {
        excepts: ['module'],
        exceptsPaths: ['require', /^jquery\./]
      },
      files: [
        {
          expand: true,
          cwd: 'js/',
          src: ['**/*.js'],
          dest: 'build/'
        }
      ]
    }
  }
});

Release History

  • 2016-04-13   v1.3.2   Update grunt peerDep.
  • 2015-05-13   v1.3.1   Option 'strict' added.
  • 2015-05-12   v1.3.0   Option 'logFilePath' removed (only filepaths with unused deps will be logged).
  • 2014-08-22   v1.2.5   Fix a bug when there are comments between paths and dependencies.
  • 2014-08-16   v1.2.0   Entirely uses AST.
  • 2014-07-20   v1.0.0   Uses AST to find out unused dependencies.
  • 2014-04-21   v0.3.0   Added option to only write out files that have unused dependencies.
  • 2014-03-19   v0.2.2   Update the dependency version.
  • 2014-01-22   v0.2.1   exceptsPaths can also be defined before each module definition.
  • 2014-01-13   v0.2.0   Extracted the core as a separate package (amdextract).
  • 2014-01-12   v0.1.7   Bug fix on commented dependencies. Add the new exceptsPaths option. Options excepts and exceptsPaths can take RegExps.
  • 2014-01-11   v0.1.6   Bug fix on commented dependencies.
  • 2014-01-05   v0.1.5   Can remove unused dependencies. Add some logging options.
  • 2014-01-02   v0.1.1   Works on files with multiple modules. Can detect module id if provided.
  • 2014-01-01   v0.1.0   Works on files with a single module.

grunt-amdcheck's People

Contributors

mahdi-shojaee avatar mokkabonna avatar tomwayson avatar tonythomson avatar vladikoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

grunt-amdcheck's Issues

Unused variables without pathes

I think there should be flag to check if there are variables that has no path, I've tried to run amdcheck and at first it gave me a error Warning: Cannot read property 'path' of undefined because there was a variable without a path in the file.

Add `strict` option to warn/fail on unused dependencies

Currently, if I run grunt amdcheck against our large-ish project, I get the following output:

...

Total unused dependencies: 89 in 64 files.
Total processed files: 251

Done, without errors.

It may be nice to have some option (for example, strict: true or allowUnusedDependencies: false) which will fail the Grunt task if I have any unused dependencies in my project. This would let us break the build if a developer adds an unused dependency in a commit.

We could/should keep the current behavior the same, but it'd be nice to have a stricter option.

Only log files with unused dependencies

I'm trying to use this grunt plugin but cannot find the right config settings to remove some of the verbose output.
Currently if I run grunt amdcheck in our project, I get about 317 lines of results:

app/scripts/head/boot.js (no module)
app/scripts/head/start.js (no module)
app/scripts/head/startup-styles.js (1 module)
Unused paths: lib/environment
app/scripts/lib/able.js (1 module)
app/scripts/lib/app-start.js (1 module)
app/scripts/lib/assertion.js (1 module)
app/scripts/lib/auth-errors.js (1 module)
...

I'm trying to find the right config combination to only show results for files with unused paths:

app/scripts/head/startup-styles.js (1 module)
Unused paths: lib/environment

Currently I'm hacking using something like this (which reduces my output to 132 lines):

$ grunt amdcheck | grep "Unused paths:" -B 1 | grep -v '\-\-'
app/scripts/head/startup-styles.js (1 module)
Unused paths: lib/environment
app/scripts/lib/channels/fx-desktop.js (1 module)
Unused paths: lib/auth-errors
app/scripts/lib/config-loader.js (1 module)
Unused paths: underscore
app/scripts/lib/fxa-client.js (1 module)
Unused paths: underscore, lib/xhr
app/scripts/lib/mailcheck.js (1 module)
Unused paths: mailcheck
app/scripts/lib/oauth-client.js (1 module)
Unused paths: lib/promise, lib/session
...

My current config looks like this:

module.exports = function (grunt) {
  'use strict';

  grunt.config('amdcheck', {
    app: {
      options: {
        removeUnusedDependencies: false,
        logFilePath: true
        // logUnusedDependencyPaths: true
      },
      files: [{
        expand: true,
        cwd: 'app/',
        src: [
          '**/*.js',
          '!bower_components/**'
        ]
      }]
    }
  });
}

Can't install from npm

Typo in the version field?

npm ERR! Error: Invalid version: "1.2.3 "
npm ERR!     at Object.module.exports.fixVersionField (/usr/local/lib/node_modules/npm/node_modules/read-package-json/node_modules/normalize-package-data/lib/fixer.js:178:13)
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/read-package-json/node_modules/normalize-package-data/lib/normalize.js:29:38
npm ERR!     at Array.forEach (native)
npm ERR!     at normalize (/usr/local/lib/node_modules/npm/node_modules/read-package-json/node_modules/normalize-package-data/lib/normalize.js:28:15)
npm ERR!     at final (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:310:33)
npm ERR!     at then (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:124:33)
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:299:40
npm ERR!     at fs.js:266:14
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:103:5
npm ERR!     at Object.oncomplete (fs.js:107:15)
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Darwin 13.1.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "grunt-amdcheck"
npm ERR! cwd /Users/scottnonnenberg/Development/thehelp/thehelp-core
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.3.11
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/scottnonnenberg/Development/thehelp/thehelp-core/npm-debug.log
npm ERR! not ok code 0

Strange through, because this is what I get from 'npm info grunt-amdcheck'

{ name: 'grunt-amdcheck',
  description: 'Finds and removes unused dependencies in AMD modules.',
  'dist-tags': { latest: '0.2.3' },
  versions: 
   [ '0.2.0',
     '0.2.1',
     '0.2.2',
     '0.2.3' ],
  maintainers: 'mehdishojaei <[email protected]>',
  time: 
   { modified: '2014-03-27T21:39:48.409Z',
     created: '2014-01-18T16:11:00.889Z',
     '0.2.0': '2014-01-18T16:11:00.889Z',
     '0.2.1': '2014-01-21T21:10:51.570Z',
     '0.2.2': '2014-03-19T13:24:03.680Z',
     '0.2.3': '2014-03-27T21:39:48.409Z' },
  readmeFilename: 'README.md',
  users: { mehdishojaei: true },
  homepage: 'https://github.com/mehdishojaei/grunt-amdcheck',
  keywords: 
   [ 'gruntplugin',
     'AMD',
     'dependency',
     'unused',
     'useless',
     'excess',
     'requirejs' ],
  repository: 
   { type: 'git',
     url: 'https://github.com/mehdishojaei/grunt-amdcheck.git' },
  contributors: 'Mehdi Shojaei <[email protected]>',
  author: 'Mehdi Shojaei',
  bugs: { url: 'https://github.com/mehdishojaei/grunt-amdcheck/issues' },
  version: '0.2.3',
  licenses: 
   { type: 'MIT',
     url: 'https://github.com/mehdishojaei/grunt-amdcheck/blob/master/LICENSE-MIT' },
  main: 'Gruntfile.js',
  engines: { node: '>= 0.8.0' },
  scripts: {},
  dependencies: { amdextract: '~1.0.0' },
  devDependencies: { grunt: '~0.4.0' },
  peerDependencies: { grunt: '~0.4.0' },
  dist: 
   { shasum: '778e794843be4930a7a65639bf74365fd63af0e1',
     tarball: 'http://registry.npmjs.org/grunt-amdcheck/-/grunt-amdcheck-0.2.3.tgz' },
  directories: {} }

Better Error Logging

I think this repo is a great idea, I implemented it in our code base and ran into an error

source/javascripts/modules/add-button.js (1 module)
Unused paths: jquery.mobile
Unused dependencies: __mobile

Warning: Line 56: Unexpected token < Use --force to continue.

Aborted due to warnings.

Which tells me nothing about the file that it failed on to go fix the file it is having trouble parsing.

Would be good to have better error logging.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.