Skip to content

Commit 262bb5d

Browse files
committed
First Commit
0 parents  commit 262bb5d

6 files changed

Lines changed: 429 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
dist
3+
*.log
4+
.idea

index.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {Plugin} from "vite";
2+
import ejs from "ejs";
3+
4+
// ShortHand for EjsOptions or Undefined
5+
type EjsRenderOptions = (ejs.Options & { async: false }) | undefined;
6+
type ViteEjsPluginDataType = Record<string, any> | ((...args: any[]) => Record<string, any>);
7+
type ViteEjsPluginOptions = {ejs: EjsRenderOptions};
8+
/**
9+
* Vite Ejs Plugin Function
10+
*
11+
* @example
12+
* export default defineConfig({
13+
* plugins: [
14+
* vue(),
15+
* ViteEjsPlugin({foo: 'bar'})
16+
* ],
17+
* });
18+
*/
19+
function ViteEjsPlugin(data: ViteEjsPluginDataType = {}, options?: ViteEjsPluginOptions): Plugin {
20+
return {
21+
name: "vite-plugin-ejs",
22+
23+
/**
24+
* Force full reload on .html change
25+
*/
26+
handleHotUpdate({file, server}) {
27+
if (file.endsWith(".html")) {
28+
server.ws.send({
29+
type: "full-reload"
30+
});
31+
}
32+
},
33+
34+
transformIndexHtml: {
35+
enforce: "pre",
36+
transform(html) {
37+
try {
38+
if (typeof data === "function") data = data();
39+
40+
html = ejs.render(
41+
html,
42+
{
43+
NODE_ENV: process.env.NODE_ENV,
44+
isDev: process.env.NODE_ENV === "development",
45+
...data
46+
},
47+
options?.ejs
48+
);
49+
} catch (e) {
50+
return e.message;
51+
}
52+
53+
return html;
54+
}
55+
}
56+
};
57+
}
58+
59+
export {ViteEjsPlugin, ejs}

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "vite-plugin-ejs",
3+
"version": "1.0.1",
4+
"description": "Use Ejs in your entrypoint i.e index.html",
5+
"main": "index.js",
6+
"repository": "https://github.com/trapcodeio/vite-plugin-ejs.git",
7+
"author": "trapcodeio",
8+
"license": "MIT",
9+
"private": false,
10+
"scripts": {
11+
"ts:build": "npx tsc -p tsconfig.json",
12+
"ts:watch": "npx tsc --watch"
13+
},
14+
"devDependencies": {
15+
"typescript": "^4.2.4",
16+
"vite": "^2.1.5"
17+
},
18+
"dependencies": {
19+
"@types/ejs": "^3.0.6",
20+
"@types/node": "^14.14.37",
21+
"ejs": "^3.1.6"
22+
}
23+
}

readme.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# vite-plugin-ejs
2+
3+
Use `ejs` template to parse your entrypoint i.e `index.html`
4+
5+
### Install
6+
7+
```sh
8+
npm i vite-plugin-ejs
9+
# or
10+
yarn add vite-plugin-ejs
11+
```
12+
13+
### Usage
14+
15+
File: **vite.config.js**
16+
17+
```javascript
18+
import {defineConfig} from "vite";
19+
import {ViteEjsPlugin} from "vite-ejs-plugin";
20+
21+
export default defineConfig({
22+
plugins: [
23+
// Without Data
24+
ViteEjsPlugin(),
25+
26+
// With Data
27+
ViteEjsPlugin({
28+
domain: "example.com",
29+
title: "My vue project!"
30+
})
31+
],
32+
});
33+
```
34+
35+
```ejs
36+
<!DOCTYPE html>
37+
<html lang="en">
38+
<head>
39+
<meta charset="UTF-8" />
40+
<link rel="icon" href="/favicon.ico" />
41+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
42+
<title><%=domain%> <%=title%></title>
43+
44+
<!-- Run Conditions-->
45+
<% if(isDev){ %>
46+
<script src="/path/to/development-only-script.js"></script>
47+
<% } else { %>
48+
<script src="/path/to/production-only-script.js" crossorigin="anonymous"></script>
49+
<% } %>
50+
</head>
51+
<body>
52+
<div id="app"></div>
53+
<script type="module" src="/src/main.ts"></script>
54+
</body>
55+
</html>
56+
```
57+
58+
Note: `isDev` is included in your data by default
59+
60+
61+
### Default data
62+
The object below is the default data of the render function.
63+
```javascript
64+
{
65+
NODE_DEV: process.env.NODE_DEV,
66+
isDev: process.env.NODE_DEV === "development"
67+
}
68+
```

tsconfig.json

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
5+
/* Basic Options */
6+
// "incremental": true, /* Enable incremental compilation */
7+
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8+
"module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9+
// "lib": [], /* Specify library files to be included in the compilation. */
10+
// "allowJs": true, /* Allow javascript files to be compiled. */
11+
// "checkJs": true, /* Report errors in .js files. */
12+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
13+
"declaration": true, /* Generates corresponding '.d.ts' file. */
14+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15+
// "sourceMap": true, /* Generates corresponding '.map' file. */
16+
// "outFile": "./", /* Concatenate and emit output to single file. */
17+
"declarationDir": "./dist", /* Redirect output structure to the directory. */
18+
"outDir": "./dist", /* Redirect output structure to the directory. */
19+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
20+
// "composite": true, /* Enable project compilation */
21+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
22+
// "removeComments": true, /* Do not emit comments to output. */
23+
// "noEmit": true, /* Do not emit outputs. */
24+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
25+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
26+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
27+
28+
/* Strict Type-Checking Options */
29+
"strict": true, /* Enable all strict type-checking options. */
30+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
31+
// "strictNullChecks": true, /* Enable strict null checks. */
32+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
33+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
34+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
35+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
36+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
37+
38+
/* Additional Checks */
39+
// "noUnusedLocals": true, /* Report errors on unused locals. */
40+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
41+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
42+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
43+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
44+
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
45+
46+
/* Module Resolution Options */
47+
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
48+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
49+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
50+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
51+
// "typeRoots": [], /* List of folders to include type definitions from. */
52+
// "types": [], /* Type declaration files to be included in compilation. */
53+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
54+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
55+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
56+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
57+
58+
/* Source Map Options */
59+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
60+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
61+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
62+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
63+
64+
/* Experimental Options */
65+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
66+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
67+
68+
/* Advanced Options */
69+
"skipLibCheck": true, /* Skip type checking of declaration files. */
70+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
71+
},
72+
"exclude": ["dist"]
73+
}

0 commit comments

Comments
 (0)