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

add exclude and include options #23

Merged
merged 3 commits into from
Jul 13, 2021
Merged

Conversation

Nauja
Copy link
Contributor

@Nauja Nauja commented Jun 6, 2021

I added two new options for filtering dependencies returned by gulp-resolve-dependencies and wrote tests.

With exclude it is possible to make gulp-resolve-dependencies ignore dependencies from certain directories:

it('should exclude depencencies from fixtures/libs', function(done) {
    gulp.src(__dirname + '/fixtures/main.js')
        .pipe(resolveDependencies({
            exclude: [path.resolve(__dirname, "fixtures/libs/**/*")]
        }))
        .pipe(concat('mainexclude.js'))
        .pipe(gulp.dest(__dirname + '/results/'))
        .pipe(es.wait(function() {
            assertFilesEqual('mainexclude.js');
            done();
        }));
});

With include it is possible to make gulp-resolve-dependencies return only dependencies from certain directories:

it('should only include depencencies from fixtures/test', function(done) {
    gulp.src(__dirname + '/fixtures/main.js')
        .pipe(resolveDependencies({
            include: [path.resolve(__dirname, "fixtures/test/**/*")]
        }))
        .pipe(concat('maininclude.js'))
        .pipe(gulp.dest(__dirname + '/results/'))
        .pipe(es.wait(function() {
            assertFilesEqual('maininclude.js');
            done();
        }));
});

Those paths are tested with:

filePath = config.resolvePath(...)
minimatch(filePath, excludedPath/includedPath)

When the log option is set, it will output included or excluded dependencies to console:

Log('[' + AnsiColors.green(PLUGIN_NAME) + '] File not included:', filePath);
Log('[' + AnsiColors.green(PLUGIN_NAME) + '] File excluded:', filePath);

@backflip
Copy link
Owner

backflip commented Jun 7, 2021

Hey @Nauja,
thank you so much for taking the time to submit these two thought-out and well-documented PRs!

There is one main thing I'm unsure about and that is whether they belong into the library or rather into "userland". Right now, my gut feeling is that they are rather edge-cases (I don't remember feedback regarding this functionality being missing) and would probably prefer for them to be solved via a custom resolvePath option. The option would have to be extended to allow for this (e.g. via continuing when returning a falsy value).

This would keep the library small and easy to maintain for me.

Would this be an alternative for you? Goes without saying that I would be totally fine with you forking and publishing your changes as an extended version (à la gulp-resolve-dependencies-extended) as well.

What are your thoughts?

@Nauja
Copy link
Contributor Author

Nauja commented Jun 8, 2021

Yes indeed it would be possible to separate my modifications in another library and keep this one as is. This really depends on what you want for your users. For this PR, I think that the ability to exclude or include some directories or files from returned dependencies is not too far from the purpose of the library and thus can be included directly into it. But you are right that it's not really difficult to add glob or minimatch to your project and override the resolvePath option to write it by hand on a case by case basis.

Me I would prefer to have it directly in the library. Imagine if typescript or webpack didn't have options for excluding or including files and you had to code them yourself or find another library for it, and try to figure out how to plug it in your workflow. This could be the same for many options and it would make those libraries smaller, but in the end it would force users to find how to achieve simple tasks by themselves.

I can think of one issue I faced that would be resolved by this PR. Let's say you are trying to resolve dependencies on index.js containing multiple import name from "module-name", mixing modules relatives to your index.js file and modules from node_modules:

import $ from "jquery";
import mylib from "./mylib";

...

As the default resolvePath is:

return path.join(path.dirname(path.resolve(targetFile.path)), match);

The jquery dependency doesn't pass this check:

// Check existence
if (!fs.existsSync(filePath)) {
    stream.emit('error', new Error(PLUGIN_NAME + ': File not found: ' + filePath));

    continue;
}

And it makes your gulp workflow fail.

With exclude, you can just configure gulp-resolve-dependencies in gulpfile.js to ignore dependencies under node_modules and you're done.

@backflip backflip merged commit 3dc3e1a into backflip:master Jul 13, 2021
@backflip
Copy link
Owner

@Nauja, thanks again, published as 4.1.0: v4.0.0...v4.1.0

@Nauja Nauja deleted the feature/exclude branch July 14, 2021 17:50
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

Successfully merging this pull request may close these issues.

2 participants