Skip to content

Commit

Permalink
feat(init): adds 'module' field to enhancedResolve mainFields when cu…
Browse files Browse the repository at this point in the history
…rrent module is type:module (#846)

## Description

- If the current package.json contains a `type` attribute with the value
`module` add `module` to the list of mainFields enhanced resolve should
use in its (external) module resolution.
- Makes the default mainFields `['main', 'types', 'typings']` over just
`['main']` (or a comment) as it's _always_ useful in TypeScript project
(which is likely in the majority of all JavaScript projects out there)
and doesn't hurt in other contexts.


Additionally, a separate commit, outside this PR, documents the working
of the mainFields attribute for ESM contexts (b92b79b)


## Motivation and Context

#843 

## How Has This Been Tested?

- [x] green ci
- [x] additional automated tests

## 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)

## Checklist

- [x] 📖

  - My change doesn't require a documentation update, or ...
  - it _does_ and I have updated it

- [x] ⚖️
- The contribution will be subject to [The MIT
license](https://github.com/sverweij/dependency-cruiser/blob/main/LICENSE),
and I'm OK with that.
  - The contribution is my own original work.
- I am ok with the stuff in
[**CONTRIBUTING.md**](https://github.com/sverweij/dependency-cruiser/blob/main/.github/CONTRIBUTING.md).
  • Loading branch information
sverweij committed Sep 29, 2023
1 parent b92b79b commit 32cad15
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/cli/init-config/build-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ function buildExtensionsAttribute(pInitOptions) {
* @returns {string}
*/
function buildMainFieldsAttribute(pInitOptions) {
return pInitOptions.usesTypeScript
? `mainFields: ["main", "types", "typings"],`
: `// mainFields: ["main", "types", "typings"],`;
if (pInitOptions.isTypeModule) {
return `mainFields: ["module", "main", "types", "typings"],`;
}
return `mainFields: ["main", "types", "typings"],`;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/cli/init-config/get-user-input.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import prompts from "prompts";
import {
isLikelyMonoRepo,
isTypeModule,
getMonoRepoPackagesCandidates,
getSourceFolderCandidates,
getTestFolderCandidates,
Expand Down Expand Up @@ -33,6 +34,12 @@ const QUESTIONS = [
message: "This looks like mono repo. Is that correct?",
initial: isLikelyMonoRepo(),
},
{
name: "isTypeModule",
type: () => (isTypeModule() ? "confirm" : false),
message: "It looks like this is an ESM package. Is that correct?",
initial: isTypeModule(),
},
{
name: "sourceLocation",
type: (_, pAnswers) => (pAnswers.isMonoRepo ? "list" : false),
Expand Down Expand Up @@ -61,7 +68,7 @@ const QUESTIONS = [
initial: (_, pAnswers) => {
return !hasTestsWithinSource(
getTestFolderCandidates(),
toSourceLocationArray(pAnswers.sourceLocation)
toSourceLocationArray(pAnswers.sourceLocation),
);
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/cli/init-config/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getDefaultConfigFileName,
hasJSConfigCandidates,
getJSConfigCandidates,
isTypeModule,
} from "./environment-helpers.mjs";
import { writeRunScriptsToManifest } from "./write-run-scripts-to-manifest.mjs";

Expand All @@ -32,6 +33,7 @@ function getOneShotConfig(pOneShotConfigId) {
/** @type {import("./types").IPartialInitConfig} */
const lBaseConfig = {
isMonoRepo: isLikelyMonoRepo(),
isTypeModule: isTypeModule(),
combinedDependencies: false,
useJsConfig: hasJSConfigCandidates() && !hasTSConfigCandidates(),
jsConfig: getJSConfigCandidates().shift(),
Expand Down
6 changes: 6 additions & 0 deletions src/cli/init-config/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export interface IInitConfig {
* Whether or not the current folder houses a mono repo
*/
isMonoRepo: boolean;
/**
* Whether or not the current folder is a package is an ESM package
* by default (and resolutions of external dependencies should be
* treated as such)
*/
isTypeModule: boolean;
/**
* Whether or not you allow usage of external dependencies declared in
* package.jsons of parent folders
Expand Down
25 changes: 25 additions & 0 deletions test/cli/init-config/build-config.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,29 @@ describe("[I] cli/init-config/build-config", () => {
fileName: "./tsconfig.json",
});
});

it("generates mainFields including 'module' for package.json with a type: module field", async () => {
const lResult = await createConfigNormalized({ isTypeModule: true });

ajv.validate(configurationSchema, lResult);
ok(lResult.hasOwnProperty("options"));
deepEqual(lResult.options.enhancedResolveOptions.mainFields, [
"module",
"main",
"types",
"typings",
]);
});

it("generates mainFields including 'module' for package.json withOUT a type: module field", async () => {
const lResult = await createConfigNormalized({});

ajv.validate(configurationSchema, lResult);
ok(lResult.hasOwnProperty("options"));
deepEqual(lResult.options.enhancedResolveOptions.mainFields, [
"main",
"types",
"typings",
]);
});
});

0 comments on commit 32cad15

Please sign in to comment.