Skip to content

Commit 5d83e8e

Browse files
committed
Support Partials and watch ejs files
1 parent d052d6a commit 5d83e8e

2 files changed

Lines changed: 92 additions & 8 deletions

File tree

index.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import {Plugin, ResolvedConfig} from "vite";
1+
import {Plugin, ResolvedConfig, UserConfig} from "vite";
22
import ejs from "ejs";
33

44
// ShortHand for EjsOptions or Undefined
5-
type EjsRenderOptions = (ejs.Options & { async: false }) | undefined;
5+
type EjsRenderOptions = ejs.Options & { async?: false };
6+
type EjsRenderOptionsFn = (config: ResolvedConfig) => EjsRenderOptions;
67
type ViteEjsPluginDataType = Record<string, any> | ((config: ResolvedConfig) => Record<string, any>);
7-
type ViteEjsPluginOptions = {ejs: EjsRenderOptions};
8+
type ViteEjsPluginOptions = { watchEjsFiles?: boolean, ejs?: EjsRenderOptions | EjsRenderOptionsFn };
89

910
/**
1011
* Vite Ejs Plugin Function
11-
*
12+
* See https://github.com/trapcodeio/vite-plugin-ejs for more information
1213
* @example
1314
* export default defineConfig({
1415
* plugins: [
@@ -23,8 +24,13 @@ function ViteEjsPlugin(data: ViteEjsPluginDataType = {}, options?: ViteEjsPlugin
2324
return {
2425
name: "vite-plugin-ejs",
2526

27+
config(config) {
28+
// Add .ejs extension to watch files.
29+
if (options && options.watchEjsFiles) WatchEjsFiles(config);
30+
},
31+
2632
// Get Resolved config
27-
configResolved(resolvedConfig){
33+
configResolved(resolvedConfig) {
2834
config = resolvedConfig;
2935
},
3036

@@ -34,6 +40,9 @@ function ViteEjsPlugin(data: ViteEjsPluginDataType = {}, options?: ViteEjsPlugin
3440
// config.isProduction
3541
try {
3642
if (typeof data === "function") data = data(config);
43+
let ejsOptions = options && options.ejs ? options.ejs : {};
44+
if (typeof ejsOptions === "function") ejsOptions = ejsOptions(config);
45+
3746

3847
html = ejs.render(
3948
html,
@@ -42,7 +51,11 @@ function ViteEjsPlugin(data: ViteEjsPluginDataType = {}, options?: ViteEjsPlugin
4251
isDev: config.mode === "development",
4352
...data
4453
},
45-
options?.ejs
54+
{
55+
views: [config.root], // Set views directory that can be overwritten
56+
...ejsOptions,
57+
async: false // Force sync
58+
}
4659
);
4760
} catch (e: any) {
4861
return e.message;
@@ -54,4 +67,21 @@ function ViteEjsPlugin(data: ViteEjsPluginDataType = {}, options?: ViteEjsPlugin
5467
};
5568
}
5669

70+
71+
function WatchEjsFiles(config: UserConfig) {
72+
// Add .ejs extension to watch files.
73+
// get watch config
74+
let watch = config.build ? config.build.watch : {};
75+
76+
// if none is defined, set to empty object
77+
if (!watch) watch = {}
78+
// check if watch.include is defined and if not, set to empty array
79+
if (!watch.include) watch.include = [];
80+
// if watch.include is not an array then convert to array
81+
if (!Array.isArray(watch.include)) watch.include = [watch.include];
82+
83+
// Add ejs files to watch list
84+
watch.include.push("**/*.ejs");
85+
}
86+
5787
export {ViteEjsPlugin, ejs}

readme.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
Use [ejs](https://www.npmjs.com/package/ejs) template language in your entrypoint i.e `index.html`
44

5-
### Install
5+
## Menu
6+
7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
- [Default Data](#default-data)
10+
- [Configure EJS](#configure-ejs)
11+
12+
### Installation
613

714
```sh
815
npm i vite-plugin-ejs
@@ -69,7 +76,7 @@ File: **index.html**
6976

7077
Note: `isDev` is included in your data by default
7178

72-
### Default data
79+
### Default Data
7380

7481
The object below is the default data of the render function.
7582

@@ -79,3 +86,50 @@ return {
7986
isDev: config.mode === "development"
8087
}
8188
```
89+
90+
## Configuration
91+
92+
The `ViteEjsPlugin` has 2 configuration
93+
94+
- `watchEjsFiles` - default: `false` - Watch for changes in ejs files and re-render the entrypoint
95+
- `ejsOptions` - default: `{views: [viteConfig.root]}` - Options for the ejs render function
96+
97+
98+
### Configure EJS
99+
100+
You can configure ejs by passing an object to the plugin.
101+
102+
```js
103+
export default defineConfig({
104+
plugins: [
105+
ViteEjsPlugin(
106+
{title: 'My vue project!'},
107+
{
108+
ejs: {
109+
// ejs options goes here.
110+
beautify: true,
111+
},
112+
}
113+
),
114+
],
115+
});
116+
```
117+
118+
If you want to use `viteconfig` to configure ejs, you can pass a function to the plugin, the function will receive the
119+
current vite config as the first argument.
120+
121+
```js
122+
export default defineConfig({
123+
plugins: [
124+
ViteEjsPlugin(
125+
{title: 'My vue project!'},
126+
{
127+
ejs: (viteConfig) => ({
128+
// ejs options goes here.
129+
views: [viteConfig.publicDir]
130+
})
131+
}
132+
),
133+
],
134+
});
135+
```

0 commit comments

Comments
 (0)