Skip to content

Commit 2722abd

Browse files
committed
Add option for config to provide vite resolved config.
1 parent 6463cc4 commit 2722abd

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {Plugin} from "vite";
1+
import {Plugin, ResolvedConfig} from "vite";
22
import ejs from "ejs";
33

44
// ShortHand for EjsOptions or Undefined
55
type EjsRenderOptions = (ejs.Options & { async: false }) | undefined;
6-
type ViteEjsPluginDataType = Record<string, any> | ((...args: any[]) => Record<string, any>);
6+
type ViteEjsPluginDataType = Record<string, any> | ((config: ResolvedConfig) => Record<string, any>);
77
type ViteEjsPluginOptions = {ejs: EjsRenderOptions};
88
/**
99
* Vite Ejs Plugin Function
@@ -17,9 +17,16 @@ type ViteEjsPluginOptions = {ejs: EjsRenderOptions};
1717
* });
1818
*/
1919
function ViteEjsPlugin(data: ViteEjsPluginDataType = {}, options?: ViteEjsPluginOptions): Plugin {
20+
let config: ResolvedConfig;
21+
2022
return {
2123
name: "vite-plugin-ejs",
2224

25+
// Get Resolved config
26+
configResolved(resolvedConfig){
27+
config = resolvedConfig;
28+
},
29+
2330
/**
2431
* Force full reload on .html change
2532
*/
@@ -35,13 +42,13 @@ function ViteEjsPlugin(data: ViteEjsPluginDataType = {}, options?: ViteEjsPlugin
3542
enforce: "pre",
3643
transform(html) {
3744
try {
38-
if (typeof data === "function") data = data();
45+
if (typeof data === "function") data = data(config);
3946

4047
html = ejs.render(
4148
html,
4249
{
43-
NODE_ENV: process.env.NODE_ENV,
44-
isDev: process.env.NODE_ENV === "development",
50+
NODE_ENV: config.mode,
51+
isDev: config.mode === "development",
4552
...data
4653
},
4754
options?.ejs

0 commit comments

Comments
 (0)