UNPKG

1.37 kBJavaScriptView Raw
1let fs = require('fs-extra')
2let chokidar = require('chokidar')
3let Path = require('path')
4let utils = require('../utils/utils')
5let projectConfig = utils.getProjectConfig()
6let log = utils.log
7let jsCompiler = require('./compiler/compile-js')
8let cssCompiler = require('./compiler/compile-css')
9
10log.tag('监听文件变动中')
11// analyse scss & js dependence before watch
12// let sassGraph = require('sass-graph')
13// let cssTree = sassGraph.parseDir(utils.getSrcPath()).index
14
15// function cssAnylase(path){
16// let absolutePath = Path.resolve(path)
17// if(cssTree[absolutePath]){
18// let r = cssTree[absolutePath].importedBy
19// r.push(absolutePath)
20// return r
21// }
22// return [absolutePath]
23// }
24
25function jsAnylase(path){
26 // let absolutePath = Path.resolve(path)
27 return [path]
28}
29
30// todo js dependence analyse
31let watcher = chokidar.watch(utils.getSrcPath())
32watcher.on('change', path => {
33 let ext = Path.extname(path)
34 if (ext === projectConfig.css.ext) {
35 cssCompiler(path)
36 // cssAnylase(path).forEach((v)=>{
37 // cssCompiler(v)
38 // })
39 }
40 else if (ext === projectConfig.js.ext) {
41 // clear jsTree on finish compiler
42 jsCompiler.compiler(path)
43 jsCompiler.clearTrace()
44 }
45 else {
46 // copy
47 let destPath = utils.getOutputFile(path)
48 fs.copySync(path,destPath)
49 log.tag('复制文件',`${destPath}`)
50 }
51})
\No newline at end of file