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

Commit

Permalink
fix: 修复 windows watch 模式生命周期不生效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Darmody committed May 11, 2020
1 parent a706082 commit 2176c40
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 41 deletions.
24 changes: 8 additions & 16 deletions packages/remax-cli/src/build/babel/appEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,34 @@ import winPath from '../../winPath';

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

export default (entry: string) => {
let skip = false;

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

StringLiteral: (path: NodePath<t.StringLiteral>, state: any) => {
const importer = winPath(state.file.opts.filename);
const { node } = path;

// 只要生命周期 Literal 存在就标记为用到了生命周期
if (!lifecycleEvents.includes(node.value)) {
return;
}

appEvents.set(entry, appEvents.get(entry)?.add(node.value) ?? new Set([node.value]));
appEvents.set(importer, appEvents.get(importer)?.add(node.value) ?? new Set([node.value]));
},
Identifier: (path: NodePath<t.Identifier>) => {
if (skip) {
return;
}

Identifier: (path: NodePath<t.Identifier>, state: any) => {
const importer = winPath(state.file.opts.filename);
const { node } = path;

// 只要生命周期 Identifer 存在就标记为用到了生命周期
if (!lifecycleEvents.includes(node.name)) {
return;
}

appEvents.set(entry, appEvents.get(entry)?.add(node.name) ?? new Set([node.name]));
appEvents.set(importer, appEvents.get(importer)?.add(node.name) ?? new Set([node.name]));
},
},
};
Expand Down
28 changes: 8 additions & 20 deletions packages/remax-cli/src/build/babel/pageEvent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as t from '@babel/types';
import { NodePath } from '@babel/traverse';
import { pageEvents } from '@remax/macro';
import { Options } from '@remax/types';
import { getPages } from '../../getEntries';
import winPath from '../../winPath';

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

export default (options: Options) => {
let skip = false;
let entry: string;

export default () => {
return {
pre(state: any) {
entry = getPages(options).find(e => e.filename === winPath(state.opts.filename))?.filename || '';
skip = !entry;
pageEvents.delete(winPath(state.opts.filename));
},
visitor: {
// 解析 class properties 编译后的代码
StringLiteral: (path: NodePath<t.StringLiteral>) => {
if (skip) {
return;
}

StringLiteral: (path: NodePath<t.StringLiteral>, state: any) => {
const { node } = path;
const importer = winPath(state.file.opts.filename);

// 只要生命周期 Literal 存在就标记为用到了生命周期
if (!lifecycleEvents.includes(node.value)) {
return;
}

pageEvents.set(entry, pageEvents.get(entry)?.add(node.value) ?? new Set([node.value]));
pageEvents.set(importer, pageEvents.get(importer)?.add(node.value) ?? new Set([node.value]));
},
Identifier: (path: NodePath<t.Identifier>) => {
if (skip) {
return;
}

Identifier: (path: NodePath<t.Identifier>, state: any) => {
const { node } = path;
const importer = winPath(state.file.opts.filename);

// 只要生命周期 Identifer 存在就标记为用到了生命周期
if (!lifecycleEvents.includes(node.name)) {
return;
}

pageEvents.set(entry, pageEvents.get(entry)?.add(node.name) ?? new Set([node.name]));
pageEvents.set(importer, pageEvents.get(importer)?.add(node.name) ?? new Set([node.name]));
},
},
};
Expand Down
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(app.filename), pageEvent(options), componentManifest(api, config), fixRegeneratorRuntime()],
usePlugins: [appEvent(), pageEvent(), componentManifest(api, config), fixRegeneratorRuntime()],
reactPreset: true,
api,
compact: process.env.NODE_ENV === 'production',
Expand Down
1 change: 0 additions & 1 deletion packages/remax-cli/src/build/webpack/config.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export default function webpackConfig(api: API, options: Options): webpack.Confi
filename: process.env.NODE_ENV === 'production' ? '[name].[chunkhash:8].css' : '[name].css',
},
]);
config.plugin('remax-define-plugin').use(RemaxPlugins.Define, [options]);

if (typeof options.configWebpack === 'function') {
options.configWebpack({ config, webpack });
Expand Down
3 changes: 0 additions & 3 deletions packages/remax-cli/src/build/webpack/plugins/Define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export default class DefinePlugin {
});
});

appEvents.clear();
pageEvents.clear();

callback();
});
});
Expand Down
5 changes: 5 additions & 0 deletions packages/remax-macro/src/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function remax({ references, state }: { references: { [name: string]: NodePath[]

references.requirePlugin?.forEach(path => requirePluginMacro(path));

const importer = state.file.opts.filename;

appEvents.delete(importer);
pageEvents.delete(importer);

references.useAppEvent?.forEach(path => useAppEventMacro(path, state));

references.usePageEvent?.forEach(path => usePageEventMacro(path, state));
Expand Down

0 comments on commit 2176c40

Please sign in to comment.