soluml / glob-import-loader

Glob
6 stars 3 forks source link

Undesired files and file types are included in glob imports, notably making this loader unusable in SCSS imports with "**" #3

Open NetherGranite opened 1 year ago

NetherGranite commented 1 year ago

Given import '*.js', EnhancedResolver produces the following missingDependencies in the resolveContext:

C:/path/to/project/*.js
C:/path/to/project/*.js.js
C:/path/to/project/*.js.json
C:/path/to/project/*.js.node

Even worse, given import '**/*.js', EnhancedResolver produces the following missingDependencies in the resolveContext:

C:/path/to/project/**/package.json
C:/path/to/project/**/*.js
C:/path/to/project/**/*.js.js
C:/path/to/project/**/*.js.json
C:/path/to/project/**/*.js.node

This loader then feeds each of those missingDependencies to a glob resolver as seen here. This means that, for example, package.json will be included in the **/*.js import.

While this is a silent mistake for JS pipelines since it's valid to import JSON, this is a breaking issue in SCSS as things like @import 'package.json' are produced.

NetherGranite commented 1 year ago

I don't know all the edge cases or how exactly missingDependencies is populated (search results), but maybe a simple fix is ensuring missing dependencies end in the same file type as the glob import?

For example, this works for me:

// Determine the file type being resolved.
const fileTypeToResolve = pathToResolve.substring(pathToResolve.lastIndexOf('/') + 1);

// Filter down to missing dependencies with file types matching the file type being resolved.
missing = missing.filter((missingDependency) => { return missingDependency.endsWith(fileTypeToResolve); });

Or maybe there's an appropriate setting in EnhancedResolver.