-
Notifications
You must be signed in to change notification settings - Fork 258
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
Question: can I force dep-cruiser to resolve .browser.js
files over regular .js
files?
#861
Comments
Well, I think I've figured it out. After some hunting I discovered that Which works in the same way (as far as I can tell) as an
|
Nope, now I have additional issues. 😂 Fails to resolve
Which I think is a bug with dep-cruiser? It should use the The version of emotion-js/emotion@515f254#diff-ea92ceb68d9de797ff6ccd926051ee30fd069382b1d5d0989626bc25582182deR2 |
After further investigation this is not limited to Any package that specifies a |
Here's the spec for the Not quite the same as an |
It's what I'd indeed expect would happen - with the The
Another approach that might be better cut for your use caseYour initial query was to resolve e.g. That is something that can be accomplished with the extensions option. Normally you'd use this for extensions like .ts, .cts, .d.ts, .js, .cjs, .jsx etc - but it works for other things as well. When the resolver gets the the module name, it'd plonk the first extension from that array the name and do an // ...
enhancedResolveOptions: {
extensions: [".browser.js", ".js"] // add your own list of extensions you have in use to this array
}
// ... might be the only thing you need. |
I'd rather avoid the need for me to manually manipulate the imports (I'm already doing a lot of that), and have the browser files resolved from the I've tried with Presumably some config is required for If so, I can put together a small replication to demonstrate the issue. |
Update: discovered that this needs to be manually configured with the import fs from 'node:fs';
import { cruise } from 'dependency-cruiser';
import enhancedResolve from 'enhanced-resolve';
const DEFAULT_CACHE_DURATION = 4000;
const dependencies = await cruise(
pathnames,
{
baseDir: process.cwd(),
builtInModules: {
override: [],
add: [],
},
enhancedResolveOptions: {
mainFields: ['module', 'main'],
exportsFields: ['exports'],
conditionNames: ['import', 'require', 'default'],
},
},
{
fileSystem: new enhancedResolve.CachedInputFileSystem(
fs,
DEFAULT_CACHE_DURATION
),
aliasFields: ['browser'], // this bit
resolveDeprecations: true,
}
); Have a few questions related to this: Why are It would be nice if these had default values so I don't have to manually reference If not, could dep-cruiser expose the |
Hi @JakeSidSmith - oh wow! Thanks for the research. It'd indeed make sense to add this 'aliasFields' property by default to what dependency-cruiser passes to enhanced-resolve or - in the least - provide an easy way to pass it. On your questions
In hindsight in the main function these two could've been combined when we'd design it from the ground up again, possibly moving the logic to combine the two to the cli.
|
## Description - enables passing the `aliasFields` attribute to the resolver (enhanced-resolve) ## Motivation and Context Addresses the issue @JakeSidSmith found in his research into fixing #861 ## How Has This Been Tested? - [x] green ci ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] Documentation only change - [ ] Refactor (non-breaking change which fixes an issue without changing functionality) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change)
Hi @JakeSidSmith thanks again for your help - the addition is part of the freshly released [email protected] Regarding the webpackConfig/ ResolveOptions - the options specified in .dependency-cruiser.js config files indeed only give the option to specify a webpack configuration file and nothing else. The |
I've checked the typings on the main functions, and it seems they're ok, but I can see where the confusion might stem from as there are three spots to enter things to pass to enhanced resolve ... export function cruise(
pFileAndDirectoryArray: string[],
pCruiseOptions?: ICruiseOptions,
pResolveOptions?: IResolveOptions,
pTranspileOptions?: ITranspileOptions
): Promise<IReporterOutput>; Two in interface ICruiseOptions {
// ...
/**
* Webpack configuration to use to get resolve options from
* This is the option to point to a webpack.conf.js (fileName
* attribute) and pass arguments and an object of webpack
* env variables. All webpack options (including all those of
* enhanced-resolve) can be passed in that file.
*/
webpackConfig?: IWebpackConfig;
// ....
/**
* Options used in module resolution that for dependency-cruiser's
* use cannot go in a webpack config.
* E.g. when there isn't/ you don't want to have a webpack.conf.js
* but still need to pass things onto it.
*/
enhancedResolveOptions?: {
}
} And one separately ( import type { ResolveOptions, CachedInputFileSystem } from "enhanced-resolve";
/**
* an object with options to pass to the resolver
* see https://github.com/webpack/enhanced-resolve#resolver-options
* for a complete list. Extended with some sauce of our own for practical
* purposes
*/
interface IResolveOptions extends ResolveOptions {
/**
*
* Without bustTheCache (or with the value `false`) the resolver
* is initialized only once per session. If the attribute
* equals `true` the resolver is initialized on each call
* (which is slower, but might is useful in some situations,
* like in executing unit tests that verify if different
* passed options yield different results))
*/
bustTheCache?: boolean;
/**
* We're exclusively using CachedInputFileSystem, hence the
* rude override
*/
fileSystem: CachedInputFileSystem;
/**
* If true also tries to find out whether an external dependency has
* been deprecated. Flagged because that's a relatively expensive
* operation. Typically there's no need to set it as dependency-cruiser
* will derive this from the rule set (if there's at least one rule
* looking for deprecations it flips this flag to true)
*/
resolveDeprecations: boolean;
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Update: see comments. Issue was sort of resolved, but resulted in a potential bug.
Summary
I would like dependencies that can resolve to a
.browser.js
equivalent to resolve to this if said file it exists, even if the actual import/require statement was for.js
Context
I have a dependency on
socket.io-client
which at some point down the tree relies onengine.io
.engine.io
has imports that reference your average.js
files, but has both.js
and.browser.js
files on disc.I'd like to resolve to the
.browser.js
ones, as the standard.js
files can include imports of node builtins, and those won't exist in the browser.E.g. https://github.com/socketio/engine.io-client/blob/main/lib/transports/polling.ts#L6
Should import: https://github.com/socketio/engine.io-client/blob/main/lib/transports/xmlhttprequest.browser.ts
I have some custom babel transforms that handle switching out
./whatever.js
for./whatever.browser.js
if the browser equivalent exists, but this isn't defined in a.babelrc
(I'm using the babel node API), so I can't use dep-cruiser'sbabelConfig
option.Am I missing an obvious way to handle this?
Environment
My config
The text was updated successfully, but these errors were encountered: