Skip to content

Commit 6271325

Browse files
committed
Fix vue identical templates reloading component
1 parent 618f13d commit 6271325

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
**This project is in beta.**
44

5-
Tested with Meteor 1.4.1.1
5+
Tested with Meteor 1.4.2-beta.7
66

77
This project contains new meteor packages to help build [meteor](http://meteor.com/) apps with first-class [vuejs](http://vuejs.org/) integration as the ui layer.
88

packages/vue-component/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
name: 'akryum:vue-component',
3-
version: '0.7.3',
3+
version: '0.7.4',
44
summary: 'VueJS single-file components that hot-reloads',
55
git: 'https://github.com/Akryum/meteor-vue-component',
66
documentation: 'README.md'

packages/vue-component/plugin/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Hash = function(text) {
1414
return hash.digest('hex');
1515
}
1616

17-
normalizeCarriageReturns = function(contents) {
18-
return contents.replace(rnReg, "\n").replace(rReg, "\n");
17+
normalizeCarriageReturns = function(contents, str = "\n") {
18+
return contents.replace(rnReg, str).replace(rReg, str);
1919
}
2020

2121
getFullDirname = function(inputFile) {

packages/vue-component/plugin/vue-compiler.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ VueComponentCompiler = class VueCompo extends CachingCompiler {
165165

166166
addCompileResult(inputFile, compileResult) {
167167
let inputFilePath = inputFile.getPathInPackage();
168-
let vueId = Hash(inputFile.getPackageName() + ':' + inputFile.getPathInPackage());
168+
let vueId = '__v' + FileHash(inputFile);;
169169
let isDev = isDevelopment();
170170

171171
// Style
@@ -203,6 +203,9 @@ VueComponentCompiler = class VueCompo extends CachingCompiler {
203203

204204
let templateHash;
205205
if (compileResult.template) {
206+
207+
templateHash = Hash(compileResult.template);
208+
206209
if(vueVersion === 1) {
207210
// Fix quotes
208211
compileResult.template = compileResult.template.replace(quoteReg, ''').replace(lineReg, '');
@@ -221,7 +224,6 @@ VueComponentCompiler = class VueCompo extends CachingCompiler {
221224
js += `__vue_options__.staticRenderFns = [${templateCompilationResult.staticRenderFns.map(toFunction).join(',')}];\n`;
222225
}
223226
}
224-
templateHash = Hash(compileResult.template);
225227

226228
//console.log(`template hash: ${templateHash}`);
227229
}
@@ -321,6 +323,7 @@ class Cache {
321323
template: null,
322324
watcher: null,
323325
dependencyManager: null,
326+
filePath: getFullPathInApp(inputFile),
324327
};
325328

326329
if(isDevelopment()) {
@@ -355,20 +358,6 @@ class Cache {
355358
class ComponentWatcher {
356359
constructor(cache) {
357360
this.cache = cache;
358-
359-
// Listener
360-
this._fileChanged = _.debounce(Meteor.bindEnvironment((event) => {
361-
if (event === 'change') {
362-
try {
363-
hotCompile.bind(this)();
364-
} catch (e) {
365-
console.error(e);
366-
}
367-
}
368-
}), 100, {
369-
leading: true,
370-
trailing: false
371-
});
372361
}
373362

374363
update(inputFile) {
@@ -392,25 +381,38 @@ class ComponentWatcher {
392381
}
393382

394383
_watchPath(filePath) {
384+
var inputFile = this.inputFile;
385+
var cache = this.cache;
395386
if(!this.watcher || filePath !== this.filePath) {
396387
// Fast file change detection
397388
this._closeWatcher();
398389
this.watcher = fs.watch(filePath, {
399390
persistent: false
400-
}, this._fileChanged);
391+
}, _.debounce(Meteor.bindEnvironment((event) => {
392+
if (event === 'change') {
393+
try {
394+
hotCompile(filePath, inputFile, cache);
395+
} catch (e) {
396+
console.error(e);
397+
}
398+
}
399+
}), 100, {
400+
leading: true,
401+
trailing: false
402+
}));
401403
this.watcher.on('error', (error) => console.error(error));
402404
}
403405
this.filePath = filePath;
404406
}
405407
}
406408

407-
function hotCompile() {
408-
let inputFilePath = this.inputFile.getPathInPackage();
409-
let contents = Plugin.fs.readFileSync(this.filePath, {
409+
function hotCompile(filePath, inputFile, cache) {
410+
let inputFilePath = inputFile.getPathInPackage();
411+
let contents = Plugin.fs.readFileSync(filePath, {
410412
encoding: 'utf8'
411413
});
412-
let compileResult = compileOneFileWithContents(this.inputFile, contents, babelOptions);
413-
let vueId = Hash(this.inputFile.getPackageName() + ':' + this.inputFile.getPathInPackage());
414+
let compileResult = compileOneFileWithContents(inputFile, contents, babelOptions);
415+
let vueId = '__v' + FileHash(inputFile);
414416

415417
// CSS
416418
let css = '';
@@ -422,7 +424,7 @@ function hotCompile() {
422424

423425
// Hot-reloading
424426
cssHash = Hash(css);
425-
if (this.cache.css !== cssHash) {
427+
if (cache.css !== cssHash) {
426428
global._dev_server.__addStyle({ hash: vueId, css });
427429
}
428430
}
@@ -434,11 +436,13 @@ function hotCompile() {
434436
let templateHash;
435437
if (template) {
436438
templateHash = Hash(template);
439+
} else {
440+
templateHash = cache.template;
437441
}
438442

439-
if (this.cache.js !== jsHash || this.cache.template !== templateHash) {
443+
if (cache.js !== jsHash || cache.template !== templateHash) {
440444

441-
const path = (this.inputFile.getPackageName() ? `packages/${this.inputFile.getPackageName()}` : '') + this.inputFile.getPathInPackage();
445+
const path = (inputFile.getPackageName() ? `packages/${inputFile.getPackageName()}` : '') + inputFile.getPathInPackage();
442446

443447
// Require to absolute
444448
js = js.replace(requireRelativeFileReg, `require('/${inputFilePath}/`);
@@ -454,13 +458,13 @@ function hotCompile() {
454458
if (template) {
455459
if(vueVersion === 1) {
456460
// Fix quotes
457-
compileResult.template = compileResult.template.replace(quoteReg, ''').replace(lineReg, '');
458-
js += "__vue_template__ = '" + compileResult.template + "';";
461+
template = template.replace(quoteReg, ''').replace(lineReg, '');
462+
js += "__vue_template__ = '" + template + "';";
459463

460464
// Template option
461465
js += `__vue_options__.template = __vue_template__;`;
462466
} else if(vueVersion === 2) {
463-
const templateCompilationResult = templateCompiler.compile(compileResult.template);
467+
const templateCompilationResult = templateCompiler.compile(template);
464468
if(templateCompilationResult.errors && templateCompilationResult.errors.length !== 0) {
465469
console.error(templateCompilationResult.errors);
466470
js += `__vue_options__.render = function(){};\n`;
@@ -472,7 +476,7 @@ function hotCompile() {
472476
}
473477
}
474478

475-
if(vueVersion === 2 && this.cache.js === jsHash) {
479+
if(vueVersion === 2 && cache.js === jsHash) {
476480
global._dev_server.emit('render', { hash: vueId, template:`{
477481
render: ${render},
478482
staticRenderFns: ${staticRenderFns}
@@ -484,7 +488,7 @@ function hotCompile() {
484488
}
485489

486490
// Package context
487-
js += `__vue_options__.packageName = '${this.inputFile.getPackageName()}';`;
491+
js += `__vue_options__.packageName = '${inputFile.getPackageName()}';`;
488492

489493
// Export
490494
js += `module.export('default', exports.default = __vue_script__);`;
@@ -494,9 +498,9 @@ function hotCompile() {
494498
}
495499

496500
// Cache
497-
this.cache.js = jsHash;
498-
this.cache.css = cssHash;
499-
this.cache.template = templateHash;
501+
cache.js = jsHash;
502+
cache.css = cssHash;
503+
cache.template = templateHash;
500504
}
501505

502506
class DependencyWatcher {

0 commit comments

Comments
 (0)