Skip to content

Commit 0996b73

Browse files
Fixed object dupe
- moved cache - removed dedupe package
1 parent 4b4a67c commit 0996b73

File tree

9 files changed

+47
-58
lines changed

9 files changed

+47
-58
lines changed

package-lock.json

Lines changed: 1 addition & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"babel-preset-es2015": "^6.24.1",
7070
"camel-case": "^3.0.0",
7171
"clean-css": "^4.1.7",
72-
"dedupe": "^2.1.0",
7372
"deepmerge": "^1.5.1",
7473
"html-minifier": "^3.5.3",
7574
"lru-cache": "^4.1.1",

src/index.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
// @flow
22
'use strict';
33
const Models = require('./models');
4+
const LRU = require('lru-cache');
45
const path = require('path');
56
const Utils = require('./utils');
67
const Renderer = require('./renderer');
78
const vueServerRenderer = require('vue-server-renderer').createRenderer();
89

10+
const cacheOptions = {
11+
max: 500,
12+
maxAge: 1000 * 60 * 60
13+
};
14+
const lruCache = LRU(cacheOptions);
15+
16+
917
/**
1018
* ExpressVueRenderer Class is the main init Class
1119
* init with `new ExpressVueRenderer(options)`
@@ -14,13 +22,15 @@ const vueServerRenderer = require('vue-server-renderer').createRenderer();
1422
*/
1523
class ExpressVueRenderer {
1624
GlobalOptions: Models.Defaults;
25+
Cache: LRU;
1726
/**
1827
* ExpressVueRenderer constructor
1928
* @constructor
2029
* @param {Object} options - The options passed to init the class
2130
*/
2231
constructor(options: Object) {
2332
this.GlobalOptions = new Models.Defaults(options);
33+
this.Cache = lruCache;
2434
}
2535
/**
2636
* createAppObject is an internal function used by renderToStream
@@ -31,22 +41,25 @@ class ExpressVueRenderer {
3141
*/
3242
createAppObject(componentPath: string, data: Object, vueOptions: ? Object): Promise < Models.AppClass > {
3343
return new Promise((resolve, reject) => {
34-
this.GlobalOptions.mergeDataObject(data);
44+
let Options = Object.create(this.GlobalOptions);
45+
Options.data = Models.Defaults.mergeObjects(Options.data, data);
46+
// Options.mergeDataObject(data);
3547
if (vueOptions) {
36-
this.GlobalOptions.mergeVueObject(vueOptions);
48+
Options.vue = Models.Defaults.mergeObjects(Options.vue, vueOptions);
3749
}
38-
this.GlobalOptions.component = componentPath;
39-
Utils.setupComponent(path.join(this.GlobalOptions.rootPath, componentPath), this.GlobalOptions)
50+
Options.component = componentPath;
51+
52+
Utils.setupComponent(path.join(Options.rootPath, componentPath), Options, this.Cache)
4053
.then((component) => {
4154

4255
const rendered = Renderer.renderHtmlUtil(component);
4356
if (!rendered) {
4457
reject(new Error('Renderer Error'));
4558
} else {
4659
const VueClass = rendered.app;
47-
const template = this.GlobalOptions.layout;
60+
const template = Options.layout;
4861
const script = rendered.scriptString;
49-
const head = new Utils.HeadUtils(this.GlobalOptions.vue, rendered.layout.style);
62+
const head = new Utils.HeadUtils(Options.vue, rendered.layout.style);
5063

5164
const app = new Models.AppClass(VueClass, template, script, head.toString());
5265
resolve(app);

src/models/defaults.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
// @flow
2-
const LRU = require('lru-cache');
32
const path = require('path');
43
const deepmerge = require('deepmerge');
5-
const dedupe = require('dedupe');
64
const Layout = require('./layout');
7-
const options = {
8-
max: 500,
9-
maxAge: 1000 * 60 * 60
10-
};
11-
const lruCache = LRU(options);
125

136
function concatMerge(destinationArray, sourceArray) {
147
let finalArray = destinationArray.concat(sourceArray);
158
//Dedupes dupes... obviously... but theres a problem here
16-
return dedupe(finalArray);
9+
// return dedupe(finalArray);
10+
return finalArray;
1711
}
1812

1913
class Defaults {
2014
rootPath: string;
2115
component: string;
2216
layout: Layout.Layout;
2317
options: Object;
24-
cache: LRU;
2518
vue: Object;
2619
data: Object;
2720
constructor(options: Object = {}) {
28-
this.cache = lruCache;
2921
this.options = options;
3022
this.layout = new Layout.Layout(options.layout);
3123

@@ -47,11 +39,9 @@ class Defaults {
4739
this.data = {};
4840
}
4941
}
50-
mergeVueObject(newVueObject: Object): void {
51-
this.vue = deepmerge(this.vue, newVueObject, { arrayMerge: concatMerge });
52-
}
53-
mergeDataObject(newDataObject: Object): void {
54-
this.data = deepmerge(this.data, newDataObject, { arrayMerge: concatMerge });
42+
static mergeObjects(globalObject: Object, newObject: Object): Object {
43+
const mergedObject = deepmerge(globalObject, newObject, { arrayMerge: concatMerge });
44+
return mergedObject;
5545
}
5646
}
5747

src/parser/component.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ const htmlParser = require('./html');
88
const scriptParser = require('./script');
99

1010

11-
function componentParser(templatePath: string, defaults: Object, type: string): Promise < Object > {
11+
function componentParser(templatePath: string, defaults: Object, type: string, Cache: Object): Promise < Object > {
1212
return new Promise(function (resolve, reject) {
1313
// try to get the component content from the cache
14-
const cachedComponentContentObject = defaults.cache.get(templatePath);
14+
const cachedComponentContentObject = Cache.get(templatePath);
1515
if (cachedComponentContentObject) {
16-
scriptParser(cachedComponentContentObject.parsedContent.script, defaults, type).then(parsedScriptObject => {
16+
scriptParser(cachedComponentContentObject.parsedContent.script, defaults, type, Cache).then(parsedScriptObject => {
1717
cachedComponentContentObject.script = parsedScriptObject;
1818
cachedComponentContentObject.script.template = cachedComponentContentObject.template;
1919
resolve(cachedComponentContentObject);
@@ -26,9 +26,9 @@ function componentParser(templatePath: string, defaults: Object, type: string):
2626
let error = `Could Not Find Component, I was expecting it to live here \n${templatePath} \nBut I couldn't find it there, ¯\\_(ツ)_/¯\n\n`;
2727
reject(error);
2828
} else {
29-
parseContent(content, templatePath, defaults, type).then(contentObject => {
29+
parseContent(content, templatePath, defaults, type, Cache).then(contentObject => {
3030
// set the cache for the component
31-
defaults.cache.set(templatePath, contentObject);
31+
Cache.set(templatePath, contentObject);
3232
resolve(contentObject);
3333
}).catch(error => {
3434
reject(error);
@@ -39,7 +39,7 @@ function componentParser(templatePath: string, defaults: Object, type: string):
3939
});
4040
}
4141

42-
function parseContent(content: string, templatePath: string, defaults: Object, type: string): Promise < Object > {
42+
function parseContent(content: string, templatePath: string, defaults: Object, type: string, Cache: Object): Promise < Object > {
4343
return new Promise((resolve, reject) => {
4444
const templateArray = templatePath.split('/');
4545
if (templateArray.length === 0) {
@@ -57,7 +57,7 @@ function parseContent(content: string, templatePath: string, defaults: Object, t
5757

5858
const promiseArray = [
5959
htmlParser(parsedContent.template, true),
60-
scriptParser(parsedContent.script, defaults, type),
60+
scriptParser(parsedContent.script, defaults, type, Cache),
6161
styleParser(parsedContent.styles)
6262
];
6363

src/parser/script.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function deleteCtor(script: Object): Object {
5050
return script;
5151
}
5252

53-
function scriptParser(scriptObject: ScriptObjectType, defaults: Object, type: string): Promise < Object > {
53+
function scriptParser(scriptObject: ScriptObjectType, defaults: Object, type: string, Cache: Object): Promise < Object > {
5454
return new Promise((resolve, reject) => {
5555
if (!scriptObject && !scriptObject.content) {
5656
reject(new Error('Missing Script block'));
@@ -60,7 +60,7 @@ function scriptParser(scriptObject: ScriptObjectType, defaults: Object, type: st
6060
};
6161
// caching for babel script string so time spent in babel is reduced
6262
const cacheKey = stringHash(scriptObject.content);
63-
const cachedBabelScript = defaults.cache.get(cacheKey);
63+
const cachedBabelScript = Cache.get(cacheKey);
6464
if (cachedBabelScript) {
6565
const finalScript = dataMerge(cachedBabelScript, defaults, type);
6666
resolve(finalScript);
@@ -71,10 +71,10 @@ function scriptParser(scriptObject: ScriptObjectType, defaults: Object, type: st
7171
rootPath: defaults.rootPath,
7272
defaults: defaults
7373
};
74-
Utils.requireFromString(babelScript.code, defaults.component, requireFromStringOptions)
74+
Utils.requireFromString(babelScript.code, defaults.component, requireFromStringOptions, Cache)
7575
.then(scriptFromString => {
7676
// set the cache for the babel script string
77-
defaults.cache.set(cacheKey, scriptFromString);
77+
Cache.set(cacheKey, scriptFromString);
7878

7979
const finalScript = dataMerge(scriptFromString, defaults, type);
8080
resolve(finalScript);

src/utils/checkPathUtils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ function getParamCasePath(path: string): string {
2626
}
2727

2828

29-
function getCorrectPathForFile(filePath: string, type: string, defaults: Defaults) {
29+
function getCorrectPathForFile(filePath: string, type: string, defaults: Defaults, Cache: Object) {
3030
return new Promise((resolve, reject) => {
3131
const resolvedPath = path.join(filePath);
3232
const cacheKey = stringHash('resolvedPath-' + resolvedPath);
33-
const cachedResolvedPath = defaults.cache.get(cacheKey);
33+
const cachedResolvedPath = Cache.get(cacheKey);
3434
if (cachedResolvedPath) {
3535
resolve(cachedResolvedPath);
3636
} else {
@@ -49,7 +49,7 @@ function getCorrectPathForFile(filePath: string, type: string, defaults: Default
4949
}
5050
} else {
5151
const pathObject = {path: resolvedPath, type: type};
52-
defaults.cache.set(cacheKey, pathObject);
52+
Cache.set(cacheKey, pathObject);
5353

5454
resolve(pathObject);
5555
}

src/utils/component.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const PathUtils = require('./checkPathUtils');
55

66
let types = new Models.Types();
77

8-
function setupComponent(componentPath: string, defaults: Models.Defaults) {
8+
function setupComponent(componentPath: string, defaults: Models.Defaults, Cache: Object) {
99
return new Promise((resolve, reject) => {
1010
const vueFile = componentPath.includes('.vue') ? componentPath : componentPath + '.vue';
11-
PathUtils.getCorrectPathForFile(vueFile, 'view', defaults)
11+
PathUtils.getCorrectPathForFile(vueFile, 'view', defaults, Cache)
1212
.then(path => {
13-
Parser.componentParser(path.path, defaults, types.COMPONENT)
13+
Parser.componentParser(path.path, defaults, types.COMPONENT, Cache)
1414
.then(component => {
1515
resolve(component);
1616
})

src/utils/require.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ class Options {
1818
this.appendPaths = optsObj.appendPaths || [];
1919
this.prependPaths = optsObj.prependPaths || [];
2020
this.rootPath = optsObj.rootPath || '';
21-
this.defaults = optsObj.defaults || {};
21+
this.defaults = optsObj.defaults;
2222
}
2323
}
2424

25-
function getVueObject(componentPath: string, rootPath: string, vueComponentFileMatch: string): Promise < {rendered:Object, match: string} > {
25+
function getVueObject(componentPath: string, rootPath: string, vueComponentFileMatch: string, Cache: Object): Promise < {rendered:Object, match: string} > {
2626
const GlobalOptions = new Models.Defaults({
2727
rootPath: rootPath,
2828
component: componentPath
2929
});
3030
return new Promise((resolve, reject) => {
31-
Utils.setupComponent(componentPath, GlobalOptions)
31+
Utils.setupComponent(componentPath, GlobalOptions, Cache)
3232
.then(component => {
3333
const rendered = Renderer.renderHtmlUtil(component);
3434
if (!rendered) {
@@ -75,7 +75,7 @@ function replaceRelativePaths(code: string, rootPath: string): string {
7575
}
7676

7777

78-
function requireFromString(code: string, filename: string = '', optsObj: Object = {}): Promise < Object > {
78+
function requireFromString(code: string, filename: string = '', optsObj: Object = {}, Cache: Object): Promise < Object > {
7979
return new Promise((resolve, reject) => {
8080
const options = new Options(optsObj);
8181
let promiseArray = [];
@@ -99,7 +99,7 @@ function requireFromString(code: string, filename: string = '', optsObj: Object
9999
//this is because its easier to do string replace later
100100
const vueComponentFile = vueComponentFileMatch.match(options.vueFileRegex);
101101
if (vueComponentFile && vueComponentFile.length > 0) {
102-
promiseArray.push(getVueObject(vueComponentFile[0], options.rootPath, vueComponentFileMatch));
102+
promiseArray.push(getVueObject(vueComponentFile[0], options.rootPath, vueComponentFileMatch, Cache));
103103
}
104104
}
105105
Promise.all(promiseArray)

0 commit comments

Comments
 (0)