Skip to content

Commit

Permalink
Merge pull request #702 from HerringtonDarkholme/watch
Browse files Browse the repository at this point in the history
partially fix #355, a crude watcher
  • Loading branch information
HerringtonDarkholme authored Feb 20, 2018
2 parents 481602d + a7f9248 commit 3f260a5
Show file tree
Hide file tree
Showing 4 changed files with 618 additions and 10 deletions.
2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
},
"homepage": "https://github.com/vuejs/vetur/tree/master/server",
"dependencies": {
"@types/chokidar": "^1.7.4",
"bootstrap-vue-helper-json": "^1.1.1",
"chokidar": "^2.0.2",
"element-helper-json": "^2.0.2",
"eslint": "^4.13.1",
"eslint-plugin-vue": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion server/src/modes/script/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export function getJavascriptMode(
jsDocuments.onDocumentRemoved(document);
},
dispose() {
serviceHost.getService().dispose();
serviceHost.dispose();
jsDocuments.dispose();
}
};
Expand Down
28 changes: 27 additions & 1 deletion server/src/modes/script/serviceHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LanguageModelCache } from '../languageModelCache';
import { createUpdater, parseVue, isVue } from './preprocess';
import { getFileFsPath, getFilePath } from '../../utils/paths';
import * as bridge from './bridge';
import * as chokidar from 'chokidar';

// Patch typescript functions to insert `import Vue from 'vue'` and `new Vue` around export default.
// NOTE: this is a global hack that all ts instances after is changed
Expand Down Expand Up @@ -68,6 +69,19 @@ export function getServiceHost(workspacePath: string, jsDocuments: LanguageModel
...parsedConfig.options
};
compilerOptions.allowNonTsExtensions = true;
const watcher = chokidar.watch(workspacePath, {
ignoreInitial: true,
ignored: defaultIgnorePatterns(workspacePath)
});

watcher
.on('change', filterNonScript(path => {
const ver = versions.get(path) || 0;
versions.set(path, ver + 1);
}))
.on('add', filterNonScript(path => {
files.push(path);
}));

function updateCurrentTextDocument(doc: TextDocument) {
const fileFsPath = getFileFsPath(doc.uri);
Expand Down Expand Up @@ -198,7 +212,10 @@ export function getServiceHost(workspacePath: string, jsDocuments: LanguageModel
return {
updateCurrentTextDocument,
getScriptDocByFsPath,
getService: () => jsLanguageService
dispose: () => {
watcher.close();
jsLanguageService.dispose();
},
};
}

Expand Down Expand Up @@ -257,3 +274,12 @@ function getParsedConfig(workspacePath: string) {
[{ extension: 'vue', isMixedContent: true }]
);
}

function filterNonScript(func: (path: string) => void) {
return (path: string) => {
if (!/(tsx?|vue|jsx?)$/.test(path)) {
return;
}
func(path);
};
}
Loading

0 comments on commit 3f260a5

Please sign in to comment.