web: convert require to await import

This commit is contained in:
Abdullah Atta
2023-07-25 08:02:57 +05:00
committed by Abdullah Atta
parent 75e62a3fac
commit cae3f7354e

View File

@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { defineConfig } from "vite";
import { Plugin, defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import svgrPlugin from "vite-plugin-svgr";
import envCompatible from "vite-plugin-env-compatible";
@@ -89,6 +89,7 @@ export default defineConfig({
}
},
plugins: [
requireTransformPlugin(),
...(isDesktop && process.env.NODE_ENV === "production"
? []
: [
@@ -115,3 +116,17 @@ export default defineConfig({
})
]
});
function requireTransformPlugin(): Plugin {
return {
transform(code, id) {
if (/\/node_modules\//g.test(id)) return;
if (!/require/.test(code)) return;
return {
code: code.replace(/require\(/gm, "await import(")
};
},
name: "require-transform-plugin"
};
}