Skip to content

Commit

Permalink
major: Change reason type from string to array (#279)
Browse files Browse the repository at this point in the history
There is a scenario where a single file is both imported as a `dependency` and read as a `asset` file so we must keep track of both usages in the Reason object.
  • Loading branch information
styfle authored Mar 18, 2022
1 parent 209b52d commit 3b377c5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/node-file-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,13 @@ export class Job {

if (!reasonEntry) {
reasonEntry = {
type: reasonType,
type: [reasonType],
ignored: false,
parents: new Set()
};
this.reasons.set(path, reasonEntry)
} else if (!reasonEntry.type.includes(reasonType)) {
reasonEntry.type.push(reasonType)
}
if (parent && this.ignoreFn(path, parent)) {
if (!this.fileList.has(path) && reasonEntry) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface NodeFileTraceOptions {
export type NodeFileTraceReasonType = 'initial' | 'resolve' | 'dependency' | 'asset' | 'sharedlib';

export interface NodeFileTraceReasons extends Map<string, {
type: NodeFileTraceReasonType;
type: NodeFileTraceReasonType[];
ignored: boolean;
parents: Set<string>;
}> {}
Expand Down
22 changes: 21 additions & 1 deletion test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ for (const { testName, isRoot } of unitTests) {
}

if (testName === 'multi-input') {
inputFileNames.push('input-2.js', 'input-3.js')
inputFileNames.push('input-2.js', 'input-3.js', 'input-4.js');
}

const { fileList, reasons } = await nodeFileTrace(
Expand Down Expand Up @@ -107,6 +107,7 @@ for (const { testName, isRoot } of unitTests) {
const normalizeInputRoot = file =>
isRoot ? join('./', unitPath, file) : join('test/unit', testName, file)

const getReasonType = file => reasons.get(normalizeInputRoot(file)).type

expect([...collectFiles(normalizeInputRoot('input.js'))].map(normalizeFilesRoot).sort()).toEqual([
"package.json",
Expand All @@ -129,6 +130,25 @@ for (const { testName, isRoot } of unitTests) {
"test/unit/multi-input/asset.txt",
"test/unit/multi-input/child-3.js",
])

expect([...collectFiles(normalizeInputRoot('input-4.js'))].map(normalizeFilesRoot).sort()).toEqual([
"package.json",
"test/unit/multi-input/child-4.js",
"test/unit/multi-input/style.module.css",
])

expect(getReasonType('input.js')).toEqual(['initial', 'dependency'])
expect(getReasonType('input-2.js')).toEqual(['initial', 'dependency'])
expect(getReasonType('input-3.js')).toEqual(['initial', 'dependency'])
expect(getReasonType('input-4.js')).toEqual(['initial', 'dependency'])
expect(getReasonType('child-1.js')).toEqual(['dependency'])
expect(getReasonType('child-2.js')).toEqual(['dependency'])
expect(getReasonType('child-3.js')).toEqual(['dependency'])
expect(getReasonType('child-4.js')).toEqual(['dependency'])
expect(getReasonType('asset.txt')).toEqual(['asset'])
expect(getReasonType('asset-2.txt')).toEqual(['asset'])
expect(getReasonType('style.module.css')).toEqual(['dependency', 'asset'])

}

let expected;
Expand Down
4 changes: 4 additions & 0 deletions test/unit/multi-input/child-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const fs = require('fs')
const { join } = require('path')

fs.readFileSync(join(__dirname, 'style.module.css'))
3 changes: 3 additions & 0 deletions test/unit/multi-input/input-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import style from './style.module.css';

import child4 from './child-4';
5 changes: 4 additions & 1 deletion test/unit/multi-input/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"test/unit/multi-input/child-1.js",
"test/unit/multi-input/child-2.js",
"test/unit/multi-input/child-3.js",
"test/unit/multi-input/child-4.js",
"test/unit/multi-input/input-2.js",
"test/unit/multi-input/input-3.js",
"test/unit/multi-input/input.js"
"test/unit/multi-input/input-4.js",
"test/unit/multi-input/input.js",
"test/unit/multi-input/style.module.css"
]
1 change: 1 addition & 0 deletions test/unit/multi-input/style.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body { color: green; }

0 comments on commit 3b377c5

Please sign in to comment.