Skip to content

Commit 840f4c6

Browse files
default vue and typing
- Includes a default vue - Bug in VUE_DEV flag - Nodemon - Better typing
1 parent b5e0c18 commit 840f4c6

File tree

7 files changed

+82
-27
lines changed

7 files changed

+82
-27
lines changed

lib/renderer/renderer.js

+37-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const uglify = require("uglify-js");
1212
const LRU = require("lru-cache");
1313
const processStyle = require("./process-style").processStyle;
1414
const Stream = require("stream");
15-
15+
const Layout = require("../models/layout").Layout;
1616
const Utils = require("../utils");
1717

1818
/**
@@ -21,23 +21,53 @@ const Utils = require("../utils");
2121
* @prop {String} style
2222
*/
2323

24+
/**
25+
* @typedef VueOptionsType
26+
* @prop {String} title
27+
* @prop {Object} head
28+
* @prop {Object[]} head.scripts
29+
* @prop {Object[]} head.metas
30+
* @prop {Object[]} head.styles
31+
* @prop {Layout} template
32+
*/
33+
34+
/**
35+
* @typedef ConfigObjectType
36+
* @prop {{max: number, maxAge: number}} cacheOptions - cacheoptions for LRU cache
37+
* @prop {String} rootPath
38+
* @prop {String} vueVersion
39+
* @prop {VueOptionsType} head
40+
*/
41+
2442
class Renderer {
2543
/**
26-
*
27-
* @param {object} options - options for constructor
44+
* @param {ConfigObjectType} options - options for constructor
2845
* @property {{max: number, maxAge: number}} cacheOptions - cacheoptions for LRU cache
2946
* @property {LRU} lruCache - LRU Cache
3047
* @property {vueServerRenderer} renderer - instance of vue server renderer
31-
* @property {String} - rootPath
48+
* @property {String} rootPath
49+
* @property {String} vueVersion
3250
*/
33-
constructor(options = {}) {
51+
constructor(options) {
3452
this.cacheOptions = options.cacheOptions || {
3553
max: 500,
3654
maxAge: 1000 * 60 * 60,
3755
};
3856
this.lruCache = LRU(this.cacheOptions);
3957
this.rootPath = options.rootPath;
40-
this.head = options.head;
58+
this.head = options.head || {};
59+
this.vueVersion = options.vueVersion || "2.5.13";
60+
let script = {};
61+
if (process.env.VUE_DEV && process.env.VUE_DEV === "true") {
62+
script = {src: `https://cdn.jsdelivr.net/npm/vue@${this.vueVersion}/dist/vue.js`};
63+
} else {
64+
script = {src: `https://cdn.jsdelivr.net/npm/vue@${this.vueVersion}/dist/vue.min.js`};
65+
}
66+
if (this.head.scripts) {
67+
this.head.scripts.push(script);
68+
} else {
69+
this.head.scripts = [script];
70+
}
4171
}
4272
/**
4373
* @param {object} oldData
@@ -353,7 +383,7 @@ class Renderer {
353383
* renderToStream returns a stream from res.renderVue to the client
354384
* @param {string} vueFile - full path to .vue component
355385
* @param {object} data - data to be inserted when generating vue class
356-
* @param {{head: Object, template: string}} vueOptions - vue options to be used when generating head
386+
* @param {VueOptionsType} vueOptions - vue options to be used when generating head
357387
* @return {Promise<Stream>}
358388
*/
359389
RenderToStream(vueFile, data, vueOptions) {

lib/utils/layout.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @ts-check
2-
const Layout = require("../models/layout");
2+
const Layout = require("../models/layout").Layout;
33

44
/**
55
* BuildLayout
6-
* @param {{head: string, template: Object, css: string, script: string}} layoutObject
6+
* @param {{head: string, template: Layout, css: string, script: string}} layoutObject
77
* @returns {{start: string, end: string}}
88
*/
99
function BuildLayout(layoutObject) {
@@ -13,7 +13,7 @@ function BuildLayout(layoutObject) {
1313
};
1414

1515
if (layoutObject) {
16-
const layout = new Layout.Layout(layoutObject.template);
16+
const layout = new Layout(layoutObject.template);
1717
finishedLayout.start = `${layout.html.start}<head>${layoutObject.head}<style>${layoutObject.css}</style></head>${layout.body.start}${layout.template.start}`;
1818
finishedLayout.end = `${layout.template.end}${BuildScript(layoutObject.script)}${layout.body.end}${layout.html.end}`;
1919
} else {
@@ -31,7 +31,7 @@ function BuildLayout(layoutObject) {
3131
function BuildScript(script) {
3232
let debugToolsString = "";
3333

34-
if (process.env.VUE_DEV) {
34+
if (process.env.VUE_DEV && process.env.VUE_DEV === "true") {
3535
debugToolsString = "Vue.config.devtools = true;";
3636
}
3737
const javaScriptString = `(function () {'use strict';var createApp = function () {return new Vue(${script})};if (typeof module !== 'undefined' && module.exports) {module.exports = createApp} else {this.app = createApp()}}).call(this);${debugToolsString}app.$mount('#app');`;

nodemon.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"tests/example/"
1414
],
1515
"env": {
16-
"DEBUG": "true"
16+
"DEBUG": true,
17+
"VUE_DEV": true
1718
},
1819
"ext": "js json vue"
1920
}

package-lock.json

+31-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"codecov": "^3.0.0",
3939
"express": "^4.16.2",
4040
"generate-release": "^0.14.0",
41+
"nodemon": "^1.14.12",
4142
"nsp": "^3.1.0",
4243
"nyc": "^11.4.1",
4344
"tslint": "^5.9.1",
@@ -46,7 +47,7 @@
4647
},
4748
"scripts": {
4849
"release": "generate-release",
49-
"debug": "DEBUG=true node --inspect tests/example/index.js",
50+
"debug": "nodemon --inspect tests/example/index.js",
5051
"start": "node tests/example/index.js",
5152
"debug-windows": "npm run build-windows && node --inspect tests/example/index.js",
5253
"start-windows": "npm run build-windows && node tests/example/index.js",

tests/example/index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ const evrOptions = {
77
rootPath: path.normalize(__dirname),
88
head: {
99
title: "Test",
10-
scripts: [
11-
{ src: "https://unpkg.com/[email protected]/dist/vue.js" },
12-
],
1310
},
1411
};
1512

@@ -65,5 +62,6 @@ app.get("/example2", function(req, res) {
6562
});
6663

6764
app.listen(3000, function() {
68-
console.log("Example app listening on port 3000!");
65+
// tslint:disable-next-line:no-console
66+
console.log("Example app listening on port 3000!");
6967
});

0 commit comments

Comments
 (0)