Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aliasMapping slow performance #127

Closed
bywo opened this issue Jan 8, 2014 · 3 comments
Closed

aliasMapping slow performance #127

bywo opened this issue Jan 8, 2014 · 3 comments

Comments

@bywo
Copy link
Contributor

bywo commented Jan 8, 2014

I experienced slow performance when using aliasMappings on a directory with many js files, e.g.

aliasMappings: {
    cwd: 'public/js', // public/js has lots of js files
    src: ['**.*.js'],
    dest: 'js'
}

The slow performance stems from browserify requireing every matched file, even if the matched file doesn't end up being required.

A better solution may be to take aliasify's approach and rewrite the requires to use the correct relative path. This way, matched files don't have to be parsed unnecessarily. I haven't tested it, but I think doing so will also solve #123.

Here's an example of using aliasify with grunt-browserify to match aliasMapping's behavior:

// takes grunt-browserify aliasMappings config and converts it into an aliasify config.
function configureAliasify(aliasMappings) {
  var expandedAliases = {};
  aliases = _.isArray(aliasMappings) ? aliasMappings : [aliasMappings];
  aliases.forEach(function (alias) {
    grunt.file.expandMapping(alias.src, alias.dest, {cwd: alias.cwd}).forEach(function(file) {
      var expose = file.dest.substr(0, file.dest.lastIndexOf('.'));
      expandedAliases[expose] = './' + file.src[0];
    });
  });

  return require('aliasify').configure({
    aliases: expandedAliases
  });
}

var aliasify = configureAliasify({
  cwd: 'public/js', // public/js has lots of js files
  src: ['**.*.js'],
  dest: 'js'
});

grunt.initConfig({
  browserify: {
    options: {
      transform: [aliasify]
    }
  }
});
@jmreidy
Copy link
Owner

jmreidy commented Jan 14, 2014

This looks interesting for the v2 refactor. @bclinkbeard, would the aliasify approach here still work with your original use case for aliasMappings?

@mekwall
Copy link

mekwall commented Feb 11, 2014

I can't even get aliasMappings to work properly (using the mappings example on windows x64). @byronmwong's solution worked for me, but it's not needed to use aliasify. You merely have to generate a alias array from the aliasMappings, which is already being done by expandMapping.

// Takes aliasMappings config and converts it into an alias array
function aliasMappingsToAliasArray(aliasMappings) {
  var aliasArray = [];
  aliases = util.isArray(aliasMappings) ? aliasMappings : [aliasMappings];
  aliases.forEach(function (alias) {
    grunt.file.expandMapping(alias.src, alias.dest, {cwd: alias.cwd}).forEach(function(file) {
      var expose = file.dest.substr(0, file.dest.lastIndexOf('.'));
      aliasArray.push('./' + file.src[0] + ':' + expose);
    });
  });
  return aliasArray;
}

The problem with this solution is that when using watch it will not update the mappings when files are added and/or removed. I guess the method has to be run again when this happens.

See my answer to this question on Stackoverflow for more details.

@jmreidy
Copy link
Owner

jmreidy commented Mar 17, 2014

aliasMappings are being dropped in v2 in favor of a transform/plugin approach.

@jmreidy jmreidy closed this as completed Mar 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants