vite-plugin-ejs
Advanced tools
Comparing version 1.5.0 to 1.6.0
import { Plugin, ResolvedConfig } from "vite"; | ||
import ejs from "ejs"; | ||
declare type EjsRenderOptions = (ejs.Options & { | ||
async: false; | ||
async?: false; | ||
}) | undefined; | ||
@@ -10,5 +10,6 @@ declare type ViteEjsPluginDataType = Record<string, any> | ((config: ResolvedConfig) => Record<string, any>); | ||
}; | ||
declare type ViteEjsPluginOptionsFn = (config: ResolvedConfig) => ViteEjsPluginOptions; | ||
/** | ||
* Vite Ejs Plugin Function | ||
* | ||
* See https://github.com/trapcodeio/vite-plugin-ejs for more information | ||
* @example | ||
@@ -22,3 +23,3 @@ * export default defineConfig({ | ||
*/ | ||
declare function ViteEjsPlugin(data?: ViteEjsPluginDataType, options?: ViteEjsPluginOptions): Plugin; | ||
declare function ViteEjsPlugin(data?: ViteEjsPluginDataType, options?: ViteEjsPluginOptions | ViteEjsPluginOptionsFn): Plugin; | ||
export { ViteEjsPlugin, ejs }; |
22
index.js
@@ -11,3 +11,3 @@ "use strict"; | ||
* Vite Ejs Plugin Function | ||
* | ||
* See https://github.com/trapcodeio/vite-plugin-ejs for more information | ||
* @example | ||
@@ -28,2 +28,16 @@ * export default defineConfig({ | ||
config = resolvedConfig; | ||
// Add .ejs extension to watch files. | ||
// get watch config | ||
let { watch } = config.build; | ||
// if none is defined, set to empty object | ||
if (!watch) | ||
watch = {}; | ||
// check if watch.include is defined and if not, set to empty array | ||
if (!watch.include) | ||
watch.include = []; | ||
// if watch.include is not an array then convert to array | ||
if (!Array.isArray(watch.include)) | ||
watch.include = [watch.include]; | ||
// Add ejs files to watch list | ||
watch.include.push("**/*.ejs"); | ||
}, | ||
@@ -37,3 +51,7 @@ transformIndexHtml: { | ||
data = data(config); | ||
html = ejs_1.default.render(html, Object.assign({ NODE_ENV: config.mode, isDev: config.mode === "development" }, data), options === null || options === void 0 ? void 0 : options.ejs); | ||
if (typeof options === "function") | ||
options = options(config); | ||
let ejsOptions = options && options.ejs ? options.ejs : {}; | ||
html = ejs_1.default.render(html, Object.assign({ NODE_ENV: config.mode, isDev: config.mode === "development" }, data), Object.assign(Object.assign({ views: [config.root] }, ejsOptions), { async: false // Force sync | ||
})); | ||
} | ||
@@ -40,0 +58,0 @@ catch (e) { |
{ | ||
"name": "vite-plugin-ejs", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Use Ejs in your entrypoint i.e index.html", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,4 +5,11 @@ # vite-plugin-ejs | ||
### Install | ||
## Menu | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Default Data](#default-data) | ||
- [Configure EJS](#configure-ejs) | ||
### Installation | ||
```sh | ||
@@ -73,3 +80,3 @@ npm i vite-plugin-ejs | ||
### Default data | ||
### Default Data | ||
@@ -84,1 +91,48 @@ The object below is the default data of the render function. | ||
``` | ||
## Configuration | ||
The `ViteEjsPlugin` has 2 configuration | ||
- `watchEjsFiles` - default: `false` - Watch for changes in ejs files and re-render the entrypoint | ||
- `ejsOptions` - default: `{views: [viteConfig.root]}` - Options for the ejs render function | ||
### Configure EJS | ||
You can configure ejs by passing an object to the plugin. | ||
```js | ||
export default defineConfig({ | ||
plugins: [ | ||
ViteEjsPlugin( | ||
{title: 'My vue project!'}, | ||
{ | ||
ejs: { | ||
// ejs options goes here. | ||
beautify: true, | ||
}, | ||
} | ||
), | ||
], | ||
}); | ||
``` | ||
If you want to use `viteconfig` to configure ejs, you can pass a function to the plugin, the function will receive the | ||
current vite config as the first argument. | ||
```js | ||
export default defineConfig({ | ||
plugins: [ | ||
ViteEjsPlugin( | ||
{title: 'My vue project!'}, | ||
{ | ||
ejs: (viteConfig) => ({ | ||
// ejs options goes here. | ||
views: [viteConfig.publicDir] | ||
}) | ||
} | ||
), | ||
], | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7556
118
136