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 7aa80c7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
22 changes: 20 additions & 2 deletions packages/remax-cli/src/build/babel/appEvent.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import * as path from 'path';
import * as t from '@babel/types';
import { NodePath } from '@babel/traverse';
import { appEvents } from '@remax/macro';
import winPath from '../../winPath';
import { Options } from '@remax/types';

const lifecycleEvents = ['onLaunch', 'onShow', 'onHide', 'onError', 'onShareAppMessage', 'onPageNotFound'];

export default () => {
export default (options: Options) => {
let skip = false;
return {
pre(state: any) {
appEvents.delete(winPath(state.opts.filename));
const importer = winPath(state.opts.filename);

if (importer.startsWith(winPath(path.join(options.cwd, options.rootDir)))) {
skip = true;
return;
}

appEvents.delete(importer);
},
visitor: {
// 解析 class properties 编译后的代码
StringLiteral: (path: NodePath<t.StringLiteral>, state: any) => {
if (skip) {
return;
}

const importer = winPath(state.file.opts.filename);
const { node } = path;

Expand All @@ -24,6 +38,10 @@ export default () => {
appEvents.set(importer, appEvents.get(importer)?.add(node.value) ?? new Set([node.value]));
},
Identifier: (path: NodePath<t.Identifier>, state: any) => {
if (skip) {
return;
}

const importer = winPath(state.file.opts.filename);
const { node } = path;

Expand Down
20 changes: 19 additions & 1 deletion packages/remax-cli/src/build/babel/pageEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as path from 'path';
import * as t from '@babel/types';
import { NodePath } from '@babel/traverse';
import { pageEvents } from '@remax/macro';
import winPath from '../../winPath';
import { Options } from '@remax/types';

const lifecycleEvents = [
'onLoad',
Expand All @@ -24,14 +26,26 @@ const lifecycleEvents = [
'onUnload',
];

export default () => {
export default (options: Options) => {
let skip = false;
return {
pre(state: any) {
const importer = winPath(state.opts.filename);

if (importer.startsWith(winPath(path.join(options.cwd, options.rootDir)))) {
skip = true;
return;
}

pageEvents.delete(winPath(state.opts.filename));
},
visitor: {
// 解析 class properties 编译后的代码
StringLiteral: (path: NodePath<t.StringLiteral>, state: any) => {
if (skip) {
return;
}

const { node } = path;
const importer = winPath(state.file.opts.filename);

Expand All @@ -43,6 +57,10 @@ export default () => {
pageEvents.set(importer, pageEvents.get(importer)?.add(node.value) ?? new Set([node.value]));
},
Identifier: (path: NodePath<t.Identifier>, state: any) => {
if (skip) {
return;
}

const { node } = path;
const importer = winPath(state.file.opts.filename);

Expand Down
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;
}
2 changes: 1 addition & 1 deletion packages/remax-cli/src/build/webpack/config.mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default function webpackConfig(api: API, options: Options, target: Platfo
.options({
babelrc: false,
configFile: resolveBabelConfig(options),
usePlugins: [appEvent(), pageEvent(), componentManifest(api, config), fixRegeneratorRuntime()],
usePlugins: [appEvent(options), pageEvent(options), componentManifest(api, config), fixRegeneratorRuntime()],
reactPreset: true,
api,
compact: process.env.NODE_ENV === 'production',
Expand Down
8 changes: 3 additions & 5 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 @@ -61,7 +63,7 @@ export default class DefinePlugin {
const modules = getModules(chunk);

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

Expand All @@ -75,10 +77,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 7aa80c7

Please sign in to comment.