This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import path from 'path'; | ||
|
||
let isFirstCheckFailed = true; | ||
export const checkChokidar = () => { | ||
try { | ||
require(path.resolve(process.cwd(), './node_modules/chokidar')); | ||
return true; | ||
} catch (e) { | ||
if (isFirstCheckFailed) { | ||
isFirstCheckFailed = false; | ||
console.log( | ||
'\n 安装 `chokidar` 获得更好的开发体验~~ 😎\n\n > npm install chokidar --save \n' | ||
); | ||
} | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const COLORS = { | ||
red: '\x1b[31m', | ||
green: '\x1b[32m', | ||
blue: '\x1b[34m', | ||
}; | ||
const RESET = '\x1b[0m'; | ||
|
||
export const output = ( | ||
content: string | string[], | ||
color: 'red' | 'green' | 'blue' | ||
) => { | ||
const message = Array.isArray(content) ? content : [content]; | ||
console.log(`${COLORS[color]}%s${RESET}`, ...message); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import chokidar from 'chokidar'; | ||
import { RollupOptions, watch, RollupWatcher } from 'rollup'; | ||
import { output } from './utils/output'; | ||
import { checkChokidar } from './utils/checkChokidar'; | ||
import { CliOptions } from '../getConfig'; | ||
import { Context } from '../types'; | ||
import build from './index'; | ||
|
||
let isBundleRunning = false; | ||
let isFirstRunWatcher = true; | ||
|
||
const rollupWatchFiles = ['src/**', 'app.js']; | ||
// 配置重新build的路径 | ||
const extraFiles = ['src/app.config.js']; | ||
// chokidar config | ||
const chokidarConfig = { | ||
usePolling: true, | ||
}; | ||
|
||
let extraFilesWatcher: RollupWatcher | null; | ||
let watcher: RollupWatcher | null; | ||
|
||
export default function runWather( | ||
rollupOptions: RollupOptions, | ||
cli: CliOptions, | ||
context?: Context | ||
) { | ||
if (isBundleRunning) { | ||
return; | ||
} | ||
|
||
isBundleRunning = true; | ||
|
||
watcher = watch([ | ||
{ | ||
...rollupOptions, | ||
watch: { | ||
chokidar: checkChokidar() && chokidarConfig, | ||
include: rollupWatchFiles, | ||
}, | ||
}, | ||
]); | ||
|
||
const watchEventHandle = (event: any) => { | ||
switch (event.code) { | ||
case 'START': | ||
output('🚚 编译...', 'blue'); | ||
break; | ||
case 'END': | ||
isBundleRunning = false; | ||
output('💡 完成', 'green'); | ||
break; | ||
case 'ERROR': | ||
case 'FATAL': | ||
isBundleRunning = false; | ||
const { error } = event; | ||
const name = error.code === 'PLUGIN_ERROR' ? error.plugin : error.code; | ||
output(`\n🚨 [${name}]: ${error.message}`, 'red'); | ||
throw error; | ||
default: | ||
break; | ||
} | ||
}; | ||
|
||
watcher.on('event', watchEventHandle); | ||
|
||
if (isFirstRunWatcher) { | ||
isFirstRunWatcher = false; | ||
console.log('\x1b[34m%s\x1b[0m', '🚀 启动 watch'); | ||
} | ||
|
||
const close = (err?: Error) => { | ||
if (watcher) { | ||
watcher.close(); | ||
watcher = null; | ||
} | ||
if (extraFilesWatcher) { | ||
extraFilesWatcher.close(); | ||
watcher = null; | ||
} | ||
|
||
process.removeListener('uncaughtException', close); | ||
|
||
if (err) { | ||
process.exit(1); | ||
console.error(err); | ||
} | ||
}; | ||
|
||
process.on('uncaughtException', close); | ||
// 监听额外的文件 | ||
extraFilesWatcher = chokidar.watch(extraFiles); | ||
|
||
const reloadWatcher = () => { | ||
if (isFirstRunWatcher || isBundleRunning) return; | ||
close(); | ||
build(cli, context); | ||
}; | ||
|
||
extraFilesWatcher | ||
.on('add', reloadWatcher) | ||
.on('unlink', reloadWatcher) | ||
.on('change', reloadWatcher); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters