@jitar/plugin-vite 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +33 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +86 -0
- package/package.json +39 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
# Jitar Plugin for Vite
|
|
3
|
+
|
|
4
|
+
This plugin allows you to use [Vite](https://vitejs.dev/) to build your [Jitar](https://jitar.dev/) app.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install --save-dev @jitar/vite-plugin
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
The plugin needs to be added to the vite config file.
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
// vite.config.js
|
|
18
|
+
import { defineConfig } from 'vite'
|
|
19
|
+
import jitar from '@jitar/plugin-vite'
|
|
20
|
+
|
|
21
|
+
export default defineConfig({
|
|
22
|
+
plugins: [
|
|
23
|
+
jitar(srcPath, jitarPath, jitarUrl, [segments])
|
|
24
|
+
]
|
|
25
|
+
})
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The plugin takes 4 arguments:
|
|
29
|
+
|
|
30
|
+
* **srcPath** - The path to the app source files. In most cases this is the `src` folder.
|
|
31
|
+
* **jitarPath** - The path to the source files used by Jitar. This path is relative to the source root. We like to use `shared` (which points to `src/shared`), but feel free to use something else.
|
|
32
|
+
* **jitarUrl** - The URL of the Jitar instance. Jitar uses by default `http://localhost:3000`, but can be configured differently in the Jitar config.
|
|
33
|
+
* **segments** - The segments to use for the client app. This is an array of strings. The default is an empty array.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { normalizePath } from 'vite';
|
|
3
|
+
import { Reflector, ReflectionFunction } from '@jitar/reflection';
|
|
4
|
+
const reflector = new Reflector();
|
|
5
|
+
function formatPath(path) {
|
|
6
|
+
path = normalizePath(path);
|
|
7
|
+
if (path.startsWith('/')) {
|
|
8
|
+
path = path.substring(1);
|
|
9
|
+
}
|
|
10
|
+
if (path.endsWith('/')) {
|
|
11
|
+
path = path.substring(0, path.length - 1);
|
|
12
|
+
}
|
|
13
|
+
return path;
|
|
14
|
+
}
|
|
15
|
+
function createServerConfig(jitarUrl) {
|
|
16
|
+
return {
|
|
17
|
+
build: {
|
|
18
|
+
target: 'esnext'
|
|
19
|
+
},
|
|
20
|
+
server: {
|
|
21
|
+
proxy: {
|
|
22
|
+
'/rpc': jitarUrl,
|
|
23
|
+
'/jitar': jitarUrl,
|
|
24
|
+
'/modules': jitarUrl,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function createBootstrapCode(segments) {
|
|
30
|
+
const segmentString = segments.map(segment => `'${segment}'`).join(', ');
|
|
31
|
+
return `<script type="module">const jitar = await import('/jitar/client.js'); await jitar.startClient(document.location.origin, [${segmentString}]);</script>`;
|
|
32
|
+
}
|
|
33
|
+
async function createImportCode(code, id, jitarFullPath, jitarPath) {
|
|
34
|
+
const relativeId = id
|
|
35
|
+
.replace(jitarFullPath, '')
|
|
36
|
+
.replace('.ts', '.js');
|
|
37
|
+
const module = reflector.parse(code);
|
|
38
|
+
const exported = module.exported;
|
|
39
|
+
const allKeys = [...exported.keys()];
|
|
40
|
+
const functionKeys = allKeys.filter(key => key !== 'default' && exported.get(key) instanceof ReflectionFunction);
|
|
41
|
+
let importCode = '';
|
|
42
|
+
let exportCode = '';
|
|
43
|
+
if (exported.has('default')) {
|
|
44
|
+
importCode += `const defaultExport = module.default;\n`;
|
|
45
|
+
exportCode += `export default defaultExport;\n`;
|
|
46
|
+
}
|
|
47
|
+
if (functionKeys.length > 0) {
|
|
48
|
+
importCode += functionKeys.map(key => `const ${key} = module.${key};`).join('\n');
|
|
49
|
+
exportCode += `export { ${functionKeys.join(', ')} };\n`;
|
|
50
|
+
}
|
|
51
|
+
return 'const jitar = await import(/* @vite-ignore */`/jitar/client.js`);\n'
|
|
52
|
+
+ 'const client = await jitar.getClient();\n'
|
|
53
|
+
+ `const module = await client.import('./${jitarPath}${relativeId}');\n`
|
|
54
|
+
+ importCode
|
|
55
|
+
+ exportCode;
|
|
56
|
+
}
|
|
57
|
+
export default function viteJitar(sourcePath, jitarPath, jitarUrl, segments = []) {
|
|
58
|
+
sourcePath = formatPath(sourcePath);
|
|
59
|
+
jitarPath = formatPath(jitarPath);
|
|
60
|
+
let jitarFullPath = undefined;
|
|
61
|
+
return {
|
|
62
|
+
name: 'jitar-plugin-vite',
|
|
63
|
+
enforce: 'post',
|
|
64
|
+
config() {
|
|
65
|
+
return createServerConfig(jitarUrl);
|
|
66
|
+
},
|
|
67
|
+
configResolved(resolvedConfig) {
|
|
68
|
+
jitarFullPath = path.join(resolvedConfig.root, sourcePath, jitarPath);
|
|
69
|
+
},
|
|
70
|
+
resolveId(source) {
|
|
71
|
+
if (source === '/jitar/client.js') {
|
|
72
|
+
return { id: source, external: 'absolute' };
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
},
|
|
76
|
+
async transform(code, id) {
|
|
77
|
+
if (jitarFullPath === undefined || id.includes(jitarFullPath) === false) {
|
|
78
|
+
return code;
|
|
79
|
+
}
|
|
80
|
+
return await createImportCode(code, id, jitarFullPath, jitarPath);
|
|
81
|
+
},
|
|
82
|
+
transformIndexHtml(html) {
|
|
83
|
+
return html.replace('<head>', `<head>${createBootstrapCode(segments)}`);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jitar/plugin-vite",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Vite plugin for Jitar.",
|
|
5
|
+
"author": "Masking Technology <info@masking.tech> (https://jitar.dev)",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p tsconfig.json",
|
|
17
|
+
"lint": "eslint . --ext .ts",
|
|
18
|
+
"clean": "rm -rf dist build",
|
|
19
|
+
"release": "npm run clean && npm run build && npm publish --access public"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@jitar/reflection": "^0.4.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"vite": "^4.3.5"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/MaskingTechnology/jitar"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/MaskingTechnology/jitar/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://jitar.dev",
|
|
35
|
+
"keywords": [
|
|
36
|
+
"vite",
|
|
37
|
+
"jitar"
|
|
38
|
+
]
|
|
39
|
+
}
|