vite-plugin-conditional-import
v0.1.7
Published
A vite plugin that allows you to conditionally import modules
Downloads
1,142
Readme
A vite plugin that allows you to conditionally import modules. inspired by vite-plugin-iso-import and discuss at Environment-specific imports.
This plug-in uses the file name to determine which files need to be imported. The file name must be in the format filename.env.js
, where env
is the environment name.
eg. client import foo.client.js
, server import foo.server.js
Feature
- [x] Support for importing files based on the environment
- [x] Support static import
- [x] Support dynamic import
Install
npm install vite-plugin-conditional-import --save-exact -D
Usage
import { defineConfig } from "vite";
import conditionalImportPlugin from "vite-plugin-conditional-import";
const isClient = true;
export default defineConfig({
define: {
"import.meta.env.IS_CLIENT": isClient ? "true" : "false",
"import.meta.env.IS_SERVER": isClient ? "false" : "true",
},
plugins: [
conditionalImportPlugin({
currentEnv: isClient ? "client" : "server",
envs: ["client", "server"],
}),
],
});
import * as fooClient from "./foo.client";
import * as fooServer from "./foo.server";
const foo = import.meta.env.IS_CLIENT ? fooClient : fooServer;
foo.hello();
see example here
License
The Anti 996 License