-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add environments as a filter of
api.transform
(#2947)
- Loading branch information
1 parent
34ff462
commit a5c8a50
Showing
8 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
e2e/cases/plugin-api/plugin-transform-by-environments/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { build } from '@e2e/helper'; | ||
import { expect, test } from '@playwright/test'; | ||
|
||
test('should allow plugin to transform code by targets', async () => { | ||
const rsbuild = await build({ | ||
cwd: __dirname, | ||
}); | ||
|
||
const files = await rsbuild.unwrapOutputJSON(); | ||
const webJs = Object.keys(files).find( | ||
(file) => | ||
file.includes('index') && | ||
!file.includes('server') && | ||
file.endsWith('.js'), | ||
); | ||
const nodeJs = Object.keys(files).find( | ||
(file) => | ||
file.includes('index') && file.includes('server') && file.endsWith('.js'), | ||
); | ||
|
||
expect(files[webJs!].includes('target is web')).toBeTruthy(); | ||
expect(files[nodeJs!].includes('target is node')).toBeTruthy(); | ||
}); |
17 changes: 17 additions & 0 deletions
17
e2e/cases/plugin-api/plugin-transform-by-environments/myPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { RsbuildPlugin } from '@rsbuild/core'; | ||
|
||
export const myPlugin: RsbuildPlugin = { | ||
name: 'my-plugin', | ||
setup(api) { | ||
api.transform({ test: /\.js$/, environments: ['web'] }, ({ code }) => { | ||
return { | ||
code: code.replace('hello', 'target is web'), | ||
}; | ||
}); | ||
api.transform({ test: /\.js$/, environments: ['node'] }, ({ code }) => { | ||
return { | ||
code: code.replace('hello', 'target is node'), | ||
}; | ||
}); | ||
}, | ||
}; |
20 changes: 20 additions & 0 deletions
20
e2e/cases/plugin-api/plugin-transform-by-environments/rsbuild.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { myPlugin } from './myPlugin'; | ||
|
||
export default { | ||
plugins: [myPlugin], | ||
environments: { | ||
web: { | ||
output: { | ||
target: 'web', | ||
}, | ||
}, | ||
node: { | ||
output: { | ||
target: 'node', | ||
distPath: { | ||
root: 'dist/server', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; |
1 change: 1 addition & 0 deletions
1
e2e/cases/plugin-api/plugin-transform-by-environments/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('hello'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters