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

advanced path resolver #22

Closed
wants to merge 2 commits into from
Closed

advanced path resolver #22

wants to merge 2 commits into from

Conversation

Nauja
Copy link
Contributor

@Nauja Nauja commented Jun 2, 2021

Current behavior

Hello, I am currently trying to use gulp-resolve-dependencies on a TypeScript/React project.

It detects imports correctly with the pattern:

resolveDependencies({
    pattern: /import .* from \"(.*)\";/g
})

But most dependencies are imported without an explicit extension:

import React from "react";

Also those dependencies are not relative to the file the import is in.

Expected behavior

The ability to resolve dependencies that reside in an external directory (i.e. node_modules) and with no explicit extension.

Implementation

I first created a new function for the default resolvePath option:

+function relativePathResolver(match, targetFile) {
+    return path.join(path.dirname(path.resolve(targetFile.path)), match);
+}

function resolveDependencies(config) {
    var defaults = {
        pattern: /\* @requires [\s-]*(.*\.js)/g,
        log: false,
        ignoreCircularDependencies: false,
-      resolvePath: function(match, targetFile) {
-          return path.join(path.dirname(path.resolve(targetFile.path)), match);
-      }
+     resolvePath: relativePathResolver
    },

This doesn't change the default behavior.

Then, I added a new path resolver to solve my problem:

+function advancedPathResolver(config) {
+     var defaults = {
+         paths: {},
+         extensions: [],
+         mainFiles: []
+     };
+     ...
+}

Both resolvers are exported using this trick:

+ resolveDependencies.relativePathResolver = relativePathResolver;
+ resolveDependencies.advancedPathResolver = advancedPathResolver;
module.exports = resolveDependencies;

So this doesn't change the already existing behavior.

It can be used like this:

.pipe(resolveDependencies({
    pattern: /import .* from \"(.*)\";/g,
    resolvePath: resolveDependencies.advancedPathResolver({
        paths: {
            "*": ["node_modules"]
        },
        extensions: [".js", ".ts"],
        mainFiles: ["index.js", "index.ts"]
    })
}))

Now, I tried to mimic some options available in webpack and tsconfig.

  • paths: this dict allow to search for a dependency in multiple external directories when the dependency matches a pattern.
  • extensions: a list of extensions to try when the dependency doesn't have an explicit extension.
  • mainFiles: a list of files to check when the dependency is resolved to a directory.

I added quite some tests in main.js to ensure the functions I added are working as expected and a concrete example of concatenating files with this new resolver.

Running tests output:

  gulp-resolve-dependencies
    ✓ should generate concatenated JS file
    ✓ should handle relative file paths
    ✓ should use resolvePath and generate concatenated JS file
    ✓ should throw error due to circular dependency
    ✓ should report the initial file stats
    ✓ should resolve dependencies in external directories with advancedPathResolver


  6 passing (102ms)

And I did my best to not break any existing behavior :)

Nauja added 2 commits June 2, 2021 17:58
can resolve dependencies in external directories, test multiple file extensions, and resolve directories
@backflip
Copy link
Owner

@Nauja, thank you so much for this contribution, highly appreciated! However, as briefly discussed in #23 (comment), I would prefer for this to be part of a fork as it is out of my intended scope for this library.

@backflip backflip closed this Jul 13, 2021
@Nauja
Copy link
Contributor Author

Nauja commented Jul 14, 2021

Okay, no problem, maybe I will create a plugin for that in a repository 🤔

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