From 5164bbf331a421e517556dd48c679570419124b8 Mon Sep 17 00:00:00 2001 From: Nick Ribal Date: Sat, 25 Apr 2020 15:44:39 +0200 Subject: [PATCH] Mobx6 - Add action name in prod too (#2343) * Add actionName in prod mode too * Change script names in Contributing section to match existing ones * Run build before 'perf' script * Fix prettier script, ignore list and run on repo * Add changes to changelog --- .prettierignore | 2 ++ CHANGELOG.md | 3 +++ README.md | 4 ++-- docs/best/pitfalls.md | 4 +++- package.json | 6 +++--- src/core/action.ts | 15 ++++++--------- src/utils/eq.ts | 3 ++- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/.prettierignore b/.prettierignore index 255abc918..0d199af4b 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,4 @@ /**/package.json website/**/* +dist/ +docs/assets/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c9eff6ea..3a9c38fc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # UNPUBLISHED (keep this here and add unpublished changes) - Add error when computed value declared for unspecified getter [#1867](https://github.com/mobxjs/mobx/issues/1867) by [@berrunder](https://github.com/berrunder) +- Don't omit action name in production [#2263](https://github.com/mobxjs/mobx/issues/2263) by [@elektronik2k5](https://github.com/elektronik2k5) +- Update readme to reflect correct scripts by [@elektronik2k5](https://github.com/elektronik2k5) +- `prettier` related tweaks by [@elektronik2k5](https://github.com/elektronik2k5) # 5.15.4 / 4.15.4 diff --git a/README.md b/README.md index 23a58efce..b56ceb5c8 100644 --- a/README.md +++ b/README.md @@ -344,8 +344,8 @@ And finally, kudos to all the people that believed in, tried, validated and even - Feel free to send small pull requests. Please discuss new features or big changes in a GitHub issue first. - Use `yarn test` to run the basic test suite. -- Use `yarn test:ci` for the test suite with coverage. -- and `yarn test:performance` for the performance tests. +- Use `yarn test:coverage` for the test suite with coverage. +- and `yarn perf` for the performance tests. - Please note that if you want to backport a feature / fix to MobX 4 a second PR needs to be opened to the mobx4-master branch. ## Online one-click setup for contributing diff --git a/docs/best/pitfalls.md b/docs/best/pitfalls.md index d565b8f1b..b8f23d948 100644 --- a/docs/best/pitfalls.md +++ b/docs/best/pitfalls.md @@ -209,7 +209,9 @@ export const MyComponent = observer(props =>
hi
) myComponent.displayName = "MyComponent" // 2 (MobX infers component name from function name) -export const MyComponent = observer(function MyComponent(props) { return
hi
}) +export const MyComponent = observer(function MyComponent(props) { + return
hi
+}) // 3 (transpiler will infer component name from variable name) const _MyComponent = props =>
hi
diff --git a/package.json b/package.json index 060a4cec9..61dc133b5 100644 --- a/package.json +++ b/package.json @@ -15,16 +15,16 @@ "test": "jest", "lint": "eslint src/**/*.ts", "watch": "jest --watch", - "perf": "yarn test:performance proxy && yarn test:performance legacy", + "perf": "yarn build && yarn test:performance proxy && yarn test:performance legacy", "test:mixed-versions": "jest --testRegex mixed-versions", "test:check": "yarn test:types && yarn lint", "test:types": "yarn tsc --noEmit && yarn test:flow", - "test:flow": "node_modules/.bin/flow check", + "test:flow": "flow check", "test:coverage": "yarn test -i --coverage", "test:performance": "PERSIST=true time node --expose-gc test/perf/index.js", "test:size": "yarn build && yarn import-size --report . observable computed autorun action enableDecorators", "test:codemod": "yarn jest --testRegex 'codemod/.*spec' codemod/undecorate.spec.ts", - "prettier": "prettier \"**/*.js\" \"**/*.ts\" \"docs/**/*.md\"", + "prettier": "prettier --write '**/*.{js,ts,md}'", "prebuild": "rimraf lib", "build": "tsdx build --name mobx --format esm,cjs,umd", "postbuild": "cp flow-typed/mobx.js dist/index.js.flow", diff --git a/src/core/action.ts b/src/core/action.ts index a0c6c34b3..7d72ec88a 100644 --- a/src/core/action.ts +++ b/src/core/action.ts @@ -18,7 +18,7 @@ import { allowStateReadsStart, allowStateReadsEnd } from "./derivation" let currentActionId = 0 let nextActionId = 1 const functionNameDescriptor = Object.getOwnPropertyDescriptor(() => {}, "name") -const isFunctionNameConfigurable = functionNameDescriptor && functionNameDescriptor.configurable +const isFunctionNameConfigurable = functionNameDescriptor?.configurable ?? false export function createAction(actionName: string, fn: Function, ref?: Object): Function { if (__DEV__) { @@ -26,16 +26,13 @@ export function createAction(actionName: string, fn: Function, ref?: Object): Fu if (typeof actionName !== "string" || !actionName) fail(`actions should have valid names, got: '${actionName}'`) } - const res = function() { + function res() { return executeAction(actionName, fn, ref || this, arguments) } - ;(res as any).isMobxAction = true - if (__DEV__) { - if (isFunctionNameConfigurable) { - Object.defineProperty(res, "name", { value: actionName }) - } - } - return res as any + return Object.defineProperties(res, { + ...(isFunctionNameConfigurable && { name: { value: actionName } }), + isMobxAction: { value: true } + }) } export function executeAction(actionName: string, fn: Function, scope?: any, args?: IArguments) { diff --git a/src/utils/eq.ts b/src/utils/eq.ts index 8df763070..b6d0035ee 100644 --- a/src/utils/eq.ts +++ b/src/utils/eq.ts @@ -83,7 +83,8 @@ function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) { typeof bCtor === "function" && bCtor instanceof bCtor ) && - "constructor" in a && "constructor" in b + "constructor" in a && + "constructor" in b ) { return false }