Skip to content

Commit

Permalink
feat(app): integrated replaceCode plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed Jun 21, 2023
1 parent 5f828b8 commit 8cdf31e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
1 change: 0 additions & 1 deletion app/extension/chrome/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"unocss": "^0.52.7",
"unplugin-auto-import": "^0.16.4",
"unplugin-vue-components": "^0.25.0",
"vite-plugin-replace": "0.1.1",
"vue": "^3.3.4",
"ws": "^8.13.0"
}
Expand Down
59 changes: 59 additions & 0 deletions app/extension/chrome/plugins/replaceCode.ts
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,
};
},
};
}
2 changes: 1 addition & 1 deletion app/extension/chrome/vite.content.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { defineConfig } from 'vite'
import { replaceCodePlugin } from 'vite-plugin-replace'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { commonConfig, plugins, r } from './vite.config'
import { __DEV__, outputDir } from './const'
import { replaceCodePlugin } from './plugins/replaceCode'

// bundling the content script
export default defineConfig({
Expand Down
11 changes: 0 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 8cdf31e

@vercel
Copy link

@vercel vercel bot commented on 8cdf31e Jun 21, 2023

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

Please sign in to comment.