Skip to content

Commit

Permalink
feat(resolve): enables passing 'aliasFields' to enhanced-resolve (#870)
Browse files Browse the repository at this point in the history
## 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)
  • Loading branch information
sverweij committed Nov 19, 2023
1 parent dfdb385 commit ba4533f
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 7 deletions.
1 change: 1 addition & 0 deletions .dependency-cruiser.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
"parser": "tsc", // acorn, swc, tsc
"enhancedResolveOptions": {
"exportsFields": ["exports"],
"aliasFields": ["browser"],
"conditionNames": ["import", "require"],
"extensions": [
".js",
Expand Down
27 changes: 27 additions & 0 deletions doc/options-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,33 @@ is of type `module`.
A list of files to consider 'main' files, defaults to ['index']. Only set this
when you have really special needs that that warrant it.
#### `aliasFields`
> Likely you will not need to use this
If you use a package that, in stead of the `main` field, or `exports` still uses
the [`browser` field](https://github.com/defunctzombie/package-browser-field-spec)
to indicate the main file in case it's for the browser, you can use:
```javascript
{
// ...
"options": {
// ...
"aliasFields": [ "browser" ]
// ...
}
}
```
In typical enhanced-resolve fashion this field was added to support this scenario,
but implemented in a more generic fashion. See
[resolve.alias](https://webpack.js.org/configuration/resolve/#resolvealiasfields)
in the webpack docs.
Defaults to an empty array (don't use any alias fields) to keep backwards
compatibility.
#### cachedInputFileSystem - `cacheDuration`
> Likely you will not need to use this
Expand Down
10 changes: 10 additions & 0 deletions src/cli/init-config/config-template.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ module.exports = {
of dependency-cruiser this will likely become the default.
*/
{{mainFieldsAttribute}}
/*
A list of alias fields in manifests (package.jsons).
Specify a field, such as browser, to be parsed according to
[this specification](https://github.com/defunctzombie/package-browser-field-spec).
Also see [resolve.alias](https://webpack.js.org/configuration/resolve/#resolvealiasfields)
in the webpack docs.
Defaults to an empty array (don't use any alias fields).
*/
// aliasFields: ["browser"],
},
reporterOptions: {
dot: {
Expand Down
10 changes: 8 additions & 2 deletions src/schema/configuration.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/schema/configuration.schema.mjs

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/schema/cruise-result.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/schema/cruise-result.schema.mjs

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion tools/schema/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default {
type: "object",
additionalProperties: false,
description:
"Webpack configuration to use to get resolve options from",
"Webpack configuration file to use to get resolve options from",
properties: {
fileName: {
type: "string",
Expand Down Expand Up @@ -298,6 +298,9 @@ export default {
"you e.g. use external packages that only expose types, and you " +
"still want references to these types to be resolved you could expand " +
"this to ['main', 'types', 'typings']",
items: {
type: "string",
},
},
mainFiles: {
type: "array",
Expand All @@ -306,6 +309,18 @@ export default {
"['index']. Only set this when you have really special needs " +
"that warrant it.",
},
aliasFields: {
type: "array",
description:
"A list of alias fields in manifests (package.jsons). " +
"Specify a field, such as browser, to be parsed according to " +
"[this specification](https://github.com/defunctzombie/package-browser-field-spec). " +
"Also see [resolve.alias](https://webpack.js.org/configuration/resolve/#resolvealiasfields) " +
"in the webpack docs. Defaults to an empty array (don't use any alias fields).",
items: {
type: "string",
},
},
cachedInputFileSystem: {
type: "object",
description:
Expand Down
10 changes: 10 additions & 0 deletions types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ export interface ICruiseOptions {
* this when you have really special needs that warrant it.
*/
mainFiles?: string[];
/**
* A list of alias fields in manifests (package.jsons).
* Specify a field, such as browser, to be parsed according to
* [this specification](https://github.com/defunctzombie/package-browser-field-spec).
* Also see [resolve.alias](https://webpack.js.org/configuration/resolve/#resolvealiasfields)
* in the webpack docs.
*
* Defaults to an empty array (don't use any alias fields).
*/
aliasFields?: string[];
/**
* Options to pass to the resolver (webpack's 'enhanced resolve') regarding
* caching.
Expand Down

0 comments on commit ba4533f

Please sign in to comment.