Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复 watch 多执行一次的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Darmody committed May 11, 2020
1 parent 2176c40 commit f3ac54b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
11 changes: 4 additions & 7 deletions packages/remax-cli/src/build/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { Compiler } from 'webpack';
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
import { getPages } from '../getEntries';

let isFirstRunWatcher = true;
let watchCounter = 0;

export default function watch(options: Options, compiler: Compiler, watcher: any, addEntry = false) {
watchCounter += 1;
// 监听额外的文件
const pages = getPages(options);
chokidar.watch([`${options.rootDir}/app.config.{js,ts}`]).on('change', () => {
if (isFirstRunWatcher) return;
if (watchCounter <= 1) return;
if (addEntry) {
const nextPages = getPages(options);
nextPages.forEach(np => {
Expand All @@ -23,13 +24,9 @@ export default function watch(options: Options, compiler: Compiler, watcher: any
});

chokidar.watch([`${options.rootDir}/**/*.config.{js,ts}`]).on('all', () => {
if (isFirstRunWatcher) return;
if (watchCounter <= 1) return;
watcher.invalidate();
});

if (isFirstRunWatcher) {
isFirstRunWatcher = false;
}

return watcher;
}
12 changes: 6 additions & 6 deletions packages/remax-cli/src/build/webpack/plugins/Define.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as path from 'path';
import { ReplaceSource } from 'webpack-sources';
import { Compiler, compilation } from 'webpack';
import { Options } from '@remax/types';
import { appEvents, pageEvents, hostComponents } from '@remax/macro';
import getModules from '../../utils/modules';
import { getPages } from '../../../getEntries';
import winPath from '../../../winPath';

const PLUGIN_NAME = 'RemaxDefinePlugin';

Expand Down Expand Up @@ -58,10 +60,12 @@ export default class DefinePlugin {
});

// TODO: 应该有更好的获取 modules 的方式?
const modules = getModules(chunk);
const modules = getModules(chunk).filter(m =>
m.startsWith(winPath(path.join(this.remaxOptions.cwd, this.remaxOptions.rootDir)))
);

events[page.name] = modules.reduce<string[]>((acc, cur) => {
return [...acc, ...(pageEvents.get(cur) || [])];
return [...acc, ...(pageEvents.get(winPath(cur)) || [])];
}, []);
});

Expand All @@ -75,10 +79,6 @@ export default class DefinePlugin {
events = events.concat(Array.from(appEvents.get(key)!).sort());
}

if (process.env.NODE_ENV === 'test') {
events = [];
}

return JSON.stringify(events, null, 2);
}

Expand Down

0 comments on commit f3ac54b

Please sign in to comment.