@primate/vue 0.5.0 → 0.7.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.
Files changed (3) hide show
  1. package/README.md +4 -45
  2. package/package.json +3 -3
  3. package/src/module.js +18 -16
package/README.md CHANGED
@@ -1,47 +1,6 @@
1
- # Primate Vue module
1
+ # Vue
2
2
 
3
- ## Install
3
+ This handler module supports SSR and serves Vue SFC components with the .vue
4
+ extension.
4
5
 
5
- `npm i @primate/vue`
6
-
7
- ## Configure
8
-
9
- Add to `primate.config.js`
10
-
11
- ```js
12
- import vue from "@primate/vue";
13
-
14
- export default {
15
- modules: [vue()],
16
- };
17
- ```
18
-
19
- ## Use
20
-
21
- Create a [SFC][sfc] component in `components`, e.g. `PostIndex.vue`.
22
-
23
- ```vue
24
- <template>
25
- <h1>All posts</h1>
26
- <div v-for="post in posts">
27
- <h2><a :href="`/vue/post/view/${post.id}`">{{post.title}}</a></h2>
28
- </div>
29
- </template>
30
- ```
31
-
32
- Create a route, for example in `routes/vue.js` (mapping `/vue`).
33
-
34
- ```js
35
- import {view} from "primate";
36
-
37
- const posts = [{
38
- id: 1,
39
- title: "First post",
40
- }];
41
-
42
- export default {
43
- get() {
44
- return view("PostIndex.vue", {posts});
45
- },
46
- };
47
- ```
6
+ ## [Documentation](https://primatejs.com/modules/vue)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/vue",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Primate Vue module",
5
5
  "homepage": "https://primatejs.com/modules/vue",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
@@ -15,11 +15,11 @@
15
15
  "directory": "packages/vue"
16
16
  },
17
17
  "dependencies": {
18
- "runtime-compat": "^0.17.0",
18
+ "runtime-compat": "^0.24.1",
19
19
  "vue": "^3.3.4"
20
20
  },
21
21
  "peerDependencies": {
22
- "primate": "0.19"
22
+ "primate": "0.21"
23
23
  },
24
24
  "type": "module",
25
25
  "main": "./src/exports.js"
package/src/module.js CHANGED
@@ -1,39 +1,41 @@
1
+ import {Response, Status, MediaType} from "runtime-compat/http";
1
2
  import {createSSRApp} from "vue";
2
3
  import {renderToString} from "vue/server-renderer";
3
4
  import {parse} from "vue/compiler-sfc";
4
- import {File} from "runtime-compat/fs";
5
5
 
6
6
  const render = (template, props) => {
7
7
  const app = createSSRApp({data: () => props, template});
8
8
  return renderToString(app);
9
9
  };
10
10
 
11
- const handler = path => (component, props = {}, {status = 200} = {}) =>
12
- async (app, headers) => {
13
- const body = await render(
14
- await File.read(`${path.join(`${component}.js`)}`), props);
11
+ const handler = (name, props = {}, {status = Status.OK} = {}) => async app => {
12
+ const {build, config} = app;
13
+ const target = build.paths.server.join(config.build.app).join(`${name}.js`);
14
+ const body = await render(await target.text(), props);
15
15
 
16
- // -> spread into new Response()
17
- return [await app.render({body}), {
18
- status,
19
- headers: {...headers, "Content-Type": "text/html"},
20
- }];
21
- };
16
+ return new Response(await app.render({body}), {
17
+ status,
18
+ headers: {...app.headers(), "Content-Type": MediaType.TEXT_HTML},
19
+ });
20
+ };
22
21
 
23
- export default ({directory} = {}) => ({
22
+ export default _ => ({
24
23
  name: "@primate/vue",
25
24
  register(app, next) {
26
- app.register("vue", handler(directory ?? app.paths.components));
25
+ app.register("vue", handler);
27
26
  return next(app);
28
27
  },
29
28
  async compile(app, next) {
29
+ const source = app.build.paths.components;
30
+ const target = app.build.paths.server.join(app.config.build.app);
31
+ await target.file.create();
30
32
  const vue = ".vue";
31
- const path = directory ?? app.paths.components;
32
- const components = await path.list(filename => filename.endsWith(vue));
33
+ const components = await source.list(filename => filename.endsWith(vue));
33
34
  await Promise.all(components.map(async component => {
34
35
  const file = await component.file.read();
35
36
  const {descriptor} = parse(file);
36
- await File.write(`${component.path}.js`, descriptor.template.content);
37
+ const to = target.join(`${component.path}.js`.replace(source, ""));
38
+ await to.file.write(descriptor.template.content);
37
39
  }));
38
40
 
39
41
  return next(app);