From 2176c408944cfcff56addffb618aae218d285ec3 Mon Sep 17 00:00:00 2001 From: Caihuanyu Date: Mon, 11 May 2020 22:35:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20windows=20watch=20?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E4=B8=8D?= =?UTF-8?q?=E7=94=9F=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../remax-cli/src/build/babel/appEvent.ts | 24 ++++++---------- .../remax-cli/src/build/babel/pageEvent.ts | 28 ++++++------------- .../src/build/webpack/config.mini.ts | 2 +- .../remax-cli/src/build/webpack/config.web.ts | 1 - .../src/build/webpack/plugins/Define.ts | 3 -- packages/remax-macro/src/macro.ts | 5 ++++ 6 files changed, 22 insertions(+), 41 deletions(-) diff --git a/packages/remax-cli/src/build/babel/appEvent.ts b/packages/remax-cli/src/build/babel/appEvent.ts index 144a1bcd7..d9a8ac0cf 100644 --- a/packages/remax-cli/src/build/babel/appEvent.ts +++ b/packages/remax-cli/src/build/babel/appEvent.ts @@ -5,20 +5,15 @@ 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) => { - if (skip) { - return; - } - + StringLiteral: (path: NodePath, state: any) => { + const importer = winPath(state.file.opts.filename); const { node } = path; // 只要生命周期 Literal 存在就标记为用到了生命周期 @@ -26,13 +21,10 @@ export default (entry: string) => { 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) => { - if (skip) { - return; - } - + Identifier: (path: NodePath, state: any) => { + const importer = winPath(state.file.opts.filename); const { node } = path; // 只要生命周期 Identifer 存在就标记为用到了生命周期 @@ -40,7 +32,7 @@ export default (entry: string) => { 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])); }, }, }; diff --git a/packages/remax-cli/src/build/babel/pageEvent.ts b/packages/remax-cli/src/build/babel/pageEvent.ts index b97327382..d2ccd3ee4 100644 --- a/packages/remax-cli/src/build/babel/pageEvent.ts +++ b/packages/remax-cli/src/build/babel/pageEvent.ts @@ -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 = [ @@ -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) => { - if (skip) { - return; - } - + StringLiteral: (path: NodePath, 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) => { - if (skip) { - return; - } - + Identifier: (path: NodePath, 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])); }, }, }; diff --git a/packages/remax-cli/src/build/webpack/config.mini.ts b/packages/remax-cli/src/build/webpack/config.mini.ts index ec5824ab1..256cc2a61 100644 --- a/packages/remax-cli/src/build/webpack/config.mini.ts +++ b/packages/remax-cli/src/build/webpack/config.mini.ts @@ -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', diff --git a/packages/remax-cli/src/build/webpack/config.web.ts b/packages/remax-cli/src/build/webpack/config.web.ts index 88560c343..b495b15b4 100644 --- a/packages/remax-cli/src/build/webpack/config.web.ts +++ b/packages/remax-cli/src/build/webpack/config.web.ts @@ -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 }); diff --git a/packages/remax-cli/src/build/webpack/plugins/Define.ts b/packages/remax-cli/src/build/webpack/plugins/Define.ts index 4304ae5ea..b06ab4ab6 100644 --- a/packages/remax-cli/src/build/webpack/plugins/Define.ts +++ b/packages/remax-cli/src/build/webpack/plugins/Define.ts @@ -40,9 +40,6 @@ export default class DefinePlugin { }); }); - appEvents.clear(); - pageEvents.clear(); - callback(); }); }); diff --git a/packages/remax-macro/src/macro.ts b/packages/remax-macro/src/macro.ts index 319a7c5dc..74f7424fe 100644 --- a/packages/remax-macro/src/macro.ts +++ b/packages/remax-macro/src/macro.ts @@ -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));