-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): integrated replaceCode plugin
- Loading branch information
Showing
4 changed files
with
60 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Plugin } from "vite"; | ||
|
||
// https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/replace | ||
interface ViteReplacement { | ||
from: string | RegExp; | ||
to: string | Function; | ||
} | ||
|
||
export interface VitePluginReplaceConfig { | ||
replacements: ViteReplacement[]; | ||
} | ||
|
||
function execSrcReplacements(src: string, replacements: ViteReplacement[]) { | ||
replacements.forEach((replacement) => { | ||
if ( | ||
(typeof replacement.from === "string" || | ||
replacement.from instanceof RegExp) === false | ||
) { | ||
throw new Error( | ||
`[vite-plugin-replace]: The replacement option 'from' is not of type 'string' or 'RegExp'.` | ||
); | ||
} else if ( | ||
(typeof replacement.to === "string" || | ||
replacement.to instanceof Function) === false | ||
) { | ||
throw new Error( | ||
`[vite-plugin-replace]: The replacement option 'to' is not of type 'string' or 'Function'` | ||
); | ||
} else { | ||
src = src.replace(replacement.from, replacement.to as string); // W3C - Function is allowed! | ||
} | ||
}); | ||
return src; | ||
} | ||
|
||
export function replaceCodePlugin(config: VitePluginReplaceConfig): Plugin { | ||
if (config === undefined) { | ||
config = { | ||
replacements: [], | ||
}; | ||
} else if ((typeof config === "object" || config !== null) === false) { | ||
throw new Error( | ||
`[vite-plugin-replace]: The configuration is not of type 'Object'.` | ||
); | ||
} else if (Array.isArray(config.replacements) === false) { | ||
throw new Error( | ||
`[vite-plugin-replace]: The configuration option 'replacement' is not of type 'Array'.` | ||
); | ||
} | ||
return { | ||
name: "transform-file", | ||
transform(src) { | ||
return { | ||
code: execSrcReplacements(src, config.replacements), | ||
map: null, | ||
}; | ||
}, | ||
}; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8cdf31e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
star-nexus – ./
star-nexus-larchliu.vercel.app
star-nexus-git-main-larchliu.vercel.app
star-nexus.vercel.app