Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ interface Replacement {
| `from` | `regexp` \| `string` |
| `to` | `string` \| `string` |

### Additional config
| Attribute | Type | Description |
| -- | -- | -- |
| `enforce` | `pre` \| `post` |

## Installation

```bash
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface ViteReplacement {

export interface VitePluginReplaceConfig {
replacements: ViteReplacement[];
enforce: 'pre' | 'post'
}

function execSrcReplacements(src: string, replacements: ViteReplacement[]) {
Expand Down Expand Up @@ -37,6 +38,7 @@ export function replaceCodePlugin(config: VitePluginReplaceConfig): Plugin {
if (config === undefined) {
config = {
replacements: [],
enforce: 'pre'
};
} else if ((typeof config === "object" || config !== null) === false) {
throw new Error(
Expand All @@ -46,10 +48,15 @@ export function replaceCodePlugin(config: VitePluginReplaceConfig): Plugin {
throw new Error(
`[vite-plugin-replace]: The configuration option 'replacement' is not of type 'Array'.`
);
} else if (["pre", "post"].indexOf(config.enforce) < 0) {
throw new Error(
`[vite-plugin-replace]: The configuration option 'enforce' must be 'pre' or 'post'.`
);
}
return {
name: "transform-file",
transform(src) {
enforce: config.enforce,
transform(src: any) {
return {
code: execSrcReplacements(src, config.replacements),
map: null,
Expand Down