wiledal / gulp-include

Enables functionality similar to that of snockets / sprockets or other file insertion compilation tools.
158 stars 68 forks source link

Add aliases option to allow named paths #90

Open henrahmagix opened 5 years ago

henrahmagix commented 5 years ago

Hi,

I wrote this option for my uses, maybe other users would like it too? =)

It allows users with many identical require paths across their files to declare the path once and reference with a name, e.g.

//= require vendor/jquery/jquery.js

becomes

//= require jquery

with options

include({
  aliases: {
    jquery: __dirname + '/vendor/jquery/jquery.js'
  }
});

I've also updated the error message to include the filepath of the include/require, and the top-level filepath at the beginning of the recursion, to aid in debugging when multiple top-level files all include/require the same file, e.g.

Error: No files found matching module "jquery" /Users/me/project/js/vendor/jquery/jquery.js (source: /Users/me/project/js/deep/file_deep.js, starting from /Users/me/project/js/file.js)

for a project structured like so:

project/js
├── vendor
│   └── jquery
│   │   └── jquery.js
├── file.js
└── deep
    └── file_deep.js
KenEucker commented 5 years ago

This is super useful, I think, and would be a great enhancement to this plugin. Let's get this in there after the dependency updates and hopefully we don't see too many collisions with other code that is also coming in.

This PR will need tests written for the use cases this feature presents. If you could provide those tests in your branch, @henrahmagix, that would be super awesome.