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
close #913
  • Loading branch information
yesmeck committed May 10, 2020
1 parent a53512e commit 8cdc87c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,11 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "v3", function() { return v3; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "v5", function() { return v5; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "v6", function() { return v6; });
var v3 = [];
var v3 = __REMAX_ENTRY_INFO__;
var v5 = [];
var v6 = {};
var v6 = {
"pages/index": []
};

/***/ }),
/* 9 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2071,51 +2071,12 @@ function lowercase(str) {
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "pageEvents", function() { return pageEvents; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "appEvents", function() { return appEvents; });
var __read = undefined && undefined.__read || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o),
r,
ar = [],
e;

try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
ar.push(r.value);
}
} catch (error) {
e = {
error: error
};
} finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
} finally {
if (e) throw e.error;
}
}

return ar;
var PAGE_EVENTS = {
"pages/index": []
};

var __spread = undefined && undefined.__spread || function () {
for (var ar = [], i = 0; i < arguments.length; i++) {
ar = ar.concat(__read(arguments[i]));
}

return ar;
};

var ENTRY_INFO = [];
var PAGE_EVENTS = {};
var APP_EVENTS = [];
function pageEvents(entry) {
var entryInfo = ENTRY_INFO.find(function (e) {
return e.name === entry;
});
return ((entryInfo === null || entryInfo === void 0 ? void 0 : entryInfo.modules) || []).reduce(function (acc, cur) {
return __spread(acc, PAGE_EVENTS[cur] || []);
}, []);
function pageEvents(name) {
return PAGE_EVENTS[name];
}
function appEvents() {
return APP_EVENTS;
Expand Down Expand Up @@ -2441,7 +2402,7 @@ function generatePageId() {
function resetPageId() {
idCounter = 0;
}
function createPageConfig(Page, entry) {
function createPageConfig(Page, name) {
var app = getApp();
var config = {
data: {
Expand Down Expand Up @@ -2578,7 +2539,7 @@ function createPageConfig(Page, entry) {
return this.callLifecycle(_lifecycle__WEBPACK_IMPORTED_MODULE_2__["Lifecycle"].pullIntercept);
}
};
Object(_lifecycle__WEBPACK_IMPORTED_MODULE_2__["pageEvents"])(entry).forEach(function (eventName) {
Object(_lifecycle__WEBPACK_IMPORTED_MODULE_2__["pageEvents"])(name).forEach(function (eventName) {
if (lifecycleEvents[eventName]) {
config[eventName] = lifecycleEvents[eventName];
}
Expand Down
11 changes: 1 addition & 10 deletions packages/remax-cli/src/build/babel/pageEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ export default (options: Options) => {
return;
}

const parentNode = path.parentPath.node;

if (t.isCallExpression(parentNode) && (parentNode.callee as any)?.name === '_defineProperty') {
pageEvents.set(entry, pageEvents.get(entry)?.add(node.value) ?? new Set([node.value]));
}
pageEvents.set(entry, pageEvents.get(entry)?.add(node.value) ?? new Set([node.value]));
},
Identifier: (path: NodePath<t.Identifier>) => {
if (skip) {
Expand All @@ -66,11 +62,6 @@ export default (options: Options) => {
return;
}

// 非函数定义不处理
if (!t.isFunctionExpression(path.parentPath.node)) {
return;
}

pageEvents.set(entry, pageEvents.get(entry)?.add(node.name) ?? new Set([node.name]));
},
},
Expand Down
85 changes: 36 additions & 49 deletions packages/remax-cli/src/build/webpack/plugins/Define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,35 @@ export default class DefinePlugin {
}

apply(compiler: Compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
compilation.moduleTemplates.javascript.hooks.render.tap(PLUGIN_NAME, moduleSource => {
const source = new ReplaceSource(moduleSource);

const startA = this.getReplaceStartIndex(source, /__REMAX_ENTRY_INFO__/);
const startB = this.getReplaceStartIndex(source, /__REMAX_APP_EVENTS__/);
const startC = this.getReplaceStartIndex(source, /__REMAX_PAGE_EVENTS__/);
const startD = this.getReplaceStartIndex(source, /__REMAX_HOST_COMPONENTS__/);

if (startA) {
source.replace(startA, startA + 19, this.stringifyEntryInfo(compilation));
}
if (startB) {
source.replace(startB, startB + 19, this.stringifyAppEvents());
}
if (startC) {
source.replace(startC, startC + 20, this.stringifyPageEvents());
}

if (startD) {
source.replace(startD, startD + 24, this.stringifyHostComponents());
}

return source;
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation: compilation.Compilation) => {
compilation.hooks.optimizeChunkAssets.tapAsync(PLUGIN_NAME, (chunks, callback) => {
compilation.chunkGroups.forEach(group => {
group.chunks.forEach((chunk: any) => {
const chunkPath = chunk.name + '.js';
const source = new ReplaceSource(compilation.assets[chunkPath]);

const startB = this.getReplaceStartIndex(source, /__REMAX_APP_EVENTS__/);
const startC = this.getReplaceStartIndex(source, /__REMAX_PAGE_EVENTS__/);
const startD = this.getReplaceStartIndex(source, /__REMAX_HOST_COMPONENTS__/);

if (startB) {
source.replace(startB, startB + 19, this.stringifyAppEvents());
}
if (startC) {
source.replace(startC, startC + 20, this.stringifyPageEvents(compilation));
}

if (startD) {
source.replace(startD, startD + 24, this.stringifyHostComponents());
}
compilation.assets[chunkPath] = source;
});
});

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

callback();
});
});
}
Expand All @@ -47,40 +52,22 @@ export default class DefinePlugin {
return regExp.exec(source.source())?.index;
}

stringifyEntryInfo(compilation: compilation.Compilation) {
let entryWithModules = getPages(this.remaxOptions).map(page => {
stringifyPageEvents(compilation: compilation.Compilation) {
const events: any = {};

getPages(this.remaxOptions).map(page => {
const chunk = compilation.chunks.find(c => {
return c.name === page.name;
});

// TODO: 应该有更好的获取 modules 的方式?
const modules = getModules(chunk);

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

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

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

stringifyPageEvents() {
let events: any = {};

for (const key of pageEvents.keys()) {
// 这里 get 不可能为空
events[key] = Array.from(pageEvents.get(key)!).sort();
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions packages/remax-runtime/src/createPageConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function resetPageId() {
idCounter = 0;
}

export default function createPageConfig(Page: React.ComponentType<any>, entry: string) {
export default function createPageConfig(Page: React.ComponentType<any>, name: string) {
const app = getApp() as any;

const config: any = {
Expand Down Expand Up @@ -178,7 +178,7 @@ export default function createPageConfig(Page: React.ComponentType<any>, entry:
},
};

pageEvents(entry).forEach(eventName => {
pageEvents(name).forEach(eventName => {
if (lifecycleEvents[eventName]) {
config[eventName] = lifecycleEvents[eventName];
}
Expand Down
8 changes: 2 additions & 6 deletions packages/remax-runtime/src/lifecycle/events.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const ENTRY_INFO = __REMAX_ENTRY_INFO__;
const PAGE_EVENTS = __REMAX_PAGE_EVENTS__;
const APP_EVENTS = __REMAX_APP_EVENTS__;

export function pageEvents(entry: string) {
const entryInfo = ENTRY_INFO.find((e: any) => e.name === entry);
return ((entryInfo?.modules || []) as string[]).reduce<string[]>((acc, cur) => {
return [...acc, ...(PAGE_EVENTS[cur] || [])];
}, []);
export function pageEvents(name: string): string[] {
return PAGE_EVENTS[name];
}

export function appEvents(): string[] {
Expand Down

0 comments on commit 8cdc87c

Please sign in to comment.