Skip to content

Commit a044cb2

Browse files
committed
feat: rules apply first match by default with fallthrough opt-in
1 parent be02c44 commit a044cb2

7 files changed

Lines changed: 51 additions & 12 deletions

File tree

packages/bundler-utils/src/generate.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ describe('bundler-utils', () => {
8989
expect(module).toContain('export const srcSet = [];')
9090
})
9191

92-
it('should stop after only rule', async () => {
92+
it('should stop after the first matched rule', async () => {
9393
const image = await createImage()
9494
const module = await generateSrcSetModule(image, {}, {
9595
rules: [
9696
{
97-
only: true,
9897
width: [0.5]
9998
},
10099
{
@@ -107,6 +106,24 @@ describe('bundler-utils', () => {
107106
expect(module).not.toContain('"jpg160"')
108107
})
109108

109+
it('should keep matching after a fallthrough rule', async () => {
110+
const image = await createImage()
111+
const module = await generateSrcSetModule(image, {}, {
112+
rules: [
113+
{
114+
fallthrough: true,
115+
width: [0.5]
116+
},
117+
{
118+
width: [0.25]
119+
}
120+
]
121+
}, emitToPath)
122+
123+
expect(module).toContain('"jpg320"')
124+
expect(module).toContain('"jpg160"')
125+
})
126+
110127
it('should inline placeholder data-url', async () => {
111128
const image = await createImage()
112129
const module = await generateSrcSetModule(

packages/bundler-utils/src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export async function generateSrcSetModule(
9696
})
9797
}
9898

99-
if (rule.only) {
99+
if (!rule.fallthrough) {
100100
break
101101
}
102102
}

packages/bundler-utils/src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export interface SrcSetRule extends GenerateOptions {
6060
*/
6161
match?: Matcher | Matcher[]
6262
/**
63-
* Do not apply the rest of the rules if this rule matched.
63+
* Keep matching the rest of the rules after this rule matched.
64+
* By default the first matched rule is the only one applied.
6465
*/
65-
only?: boolean
66+
fallthrough?: boolean
6667
}

packages/cli/src/run.spec.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,14 @@ describe('cli', () => {
8686
expect(written).toEqual([])
8787
})
8888

89-
it('should stop after only rule', async () => {
89+
it('should stop after the first matched rule', async () => {
9090
const dir = await createProject()
9191
const written = await runIn(dir, {
9292
src: 'images/**/*.jpg',
9393
dest: 'dist',
9494
skipOptimization: true,
9595
rules: [
9696
{
97-
only: true,
9897
width: [0.5]
9998
},
10099
{
@@ -107,6 +106,28 @@ describe('cli', () => {
107106
expect(written[0]).toContain('photo@320w.jpg')
108107
})
109108

109+
it('should keep matching after a fallthrough rule', async () => {
110+
const dir = await createProject()
111+
const written = await runIn(dir, {
112+
src: 'images/**/*.jpg',
113+
dest: 'dist',
114+
skipOptimization: true,
115+
rules: [
116+
{
117+
fallthrough: true,
118+
width: [0.5]
119+
},
120+
{
121+
width: [0.25]
122+
}
123+
]
124+
})
125+
126+
expect(written.length).toBe(2)
127+
expect(written.some(file => file.includes('photo@320w.jpg'))).toBe(true)
128+
expect(written.some(file => file.includes('photo@160w.jpg'))).toBe(true)
129+
})
130+
110131
it('should throw without matched sources', async () => {
111132
const dir = await createProject()
112133

packages/cli/src/run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function run(options: SrcSetCliOptions) {
7474
}
7575
}
7676

77-
if (rule.only) {
77+
if (!rule.fallthrough) {
7878
break
7979
}
8080
}

packages/cli/src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ export interface SrcSetCliRule extends GenerateOptions {
1313
*/
1414
match?: Matcher | Matcher[]
1515
/**
16-
* Do not apply the rest of the rules if this rule matched.
16+
* Keep matching the rest of the rules after this rule matched.
17+
* By default the first matched rule is the only one applied.
1718
*/
18-
only?: boolean
19+
fallthrough?: boolean
1920
}
2021

2122
export interface SrcSetCliOptions extends SrcSetGeneratorOptions {

packages/loader/src/loader.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,14 @@ describe('loader', () => {
155155
expect(metadata.width).toBe(16)
156156
})
157157

158-
it('should export placeholder together with only rule', async () => {
158+
it('should export placeholder together with a matched rule', async () => {
159159
const dir = await createFixtureProject(defaultEntry)
160160
const { exports } = await compile(createCompiler, dir, {
161161
skipOptimization: true,
162162
placeholder: true,
163163
rules: [
164164
{
165165
match: '**/*.jpg',
166-
only: true,
167166
width: [0.5]
168167
}
169168
]

0 commit comments

Comments
 (0)