@umijs/plugins 4.0.0-beta.1 → 4.0.0-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.getIdentifierDeclaration = void 0;
23
+ const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
24
+ function getIdentifierDeclaration(node, path) {
25
+ if (t.isIdentifier(node) && path.scope.hasBinding(node.name)) {
26
+ let bindingNode = path.scope.getBinding(node.name).path.node;
27
+ if (t.isVariableDeclarator(bindingNode)) {
28
+ bindingNode = bindingNode.init;
29
+ }
30
+ return bindingNode;
31
+ }
32
+ return node;
33
+ }
34
+ exports.getIdentifierDeclaration = getIdentifierDeclaration;
@@ -0,0 +1,34 @@
1
+ import * as t from '@umijs/bundler-utils/compiled/babel/types';
2
+ import { IApi } from 'umi';
3
+ interface IOpts {
4
+ contentTest?: (content: string) => Boolean;
5
+ astTest?: (opts: {
6
+ node: t.Node;
7
+ content: string;
8
+ }) => Boolean;
9
+ }
10
+ export declare class Model {
11
+ file: string;
12
+ namespace: string;
13
+ id: string;
14
+ constructor(file: string, id: number);
15
+ }
16
+ export declare class ModelUtils {
17
+ api: IApi;
18
+ opts: IOpts;
19
+ count: number;
20
+ constructor(api: IApi | null, opts: IOpts);
21
+ getAllModels(opts: {
22
+ extraModels: string[];
23
+ }): Model[];
24
+ getModels(opts: {
25
+ base: string;
26
+ pattern?: string;
27
+ }): string[];
28
+ isModelValid(opts: {
29
+ content: string;
30
+ file: string;
31
+ }): boolean;
32
+ static getModelsContent(models: Model[]): string;
33
+ }
34
+ export {};
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.ModelUtils = exports.Model = void 0;
26
+ const parser = __importStar(require("@umijs/bundler-utils/compiled/babel/parser"));
27
+ const traverse_1 = __importDefault(require("@umijs/bundler-utils/compiled/babel/traverse"));
28
+ const esbuild_1 = require("@umijs/bundler-utils/compiled/esbuild");
29
+ const utils_1 = require("@umijs/utils");
30
+ const fs_1 = require("fs");
31
+ const path_1 = require("path");
32
+ const astUtils_1 = require("./astUtils");
33
+ class Model {
34
+ constructor(file, id) {
35
+ this.file = file;
36
+ this.namespace = (0, path_1.basename)(file, (0, path_1.extname)(file));
37
+ this.id = `model_${id}`;
38
+ }
39
+ }
40
+ exports.Model = Model;
41
+ class ModelUtils {
42
+ constructor(api, opts) {
43
+ this.opts = {};
44
+ this.count = 1;
45
+ this.api = api;
46
+ this.opts = opts;
47
+ }
48
+ getAllModels(opts) {
49
+ // reset count
50
+ this.count = 1;
51
+ return [
52
+ ...this.getModels({
53
+ base: (0, path_1.join)(this.api.paths.absSrcPath, 'models'),
54
+ pattern: '**/*.{ts,tsx,js,jsx}',
55
+ }),
56
+ ...this.getModels({
57
+ base: (0, path_1.join)(this.api.paths.absPagesPath),
58
+ pattern: '**/models/**/*.{ts,tsx,js,jsx}',
59
+ }),
60
+ ...this.getModels({
61
+ base: (0, path_1.join)(this.api.paths.absPagesPath),
62
+ pattern: '**/model.{ts,tsx,js,jsx}',
63
+ }),
64
+ ...opts.extraModels,
65
+ ].map((file) => {
66
+ return new Model(file, this.count++);
67
+ });
68
+ }
69
+ getModels(opts) {
70
+ return utils_1.glob
71
+ .sync(opts.pattern || '**/*.{ts,js}', {
72
+ cwd: opts.base,
73
+ absolute: true,
74
+ })
75
+ .map(utils_1.winPath)
76
+ .filter((file) => {
77
+ if (/\.d.ts$/.test(file))
78
+ return false;
79
+ if (/\.(test|e2e|spec).([jt])sx?$/.test(file))
80
+ return false;
81
+ const content = (0, fs_1.readFileSync)(file, 'utf-8');
82
+ return this.isModelValid({ content, file });
83
+ });
84
+ }
85
+ isModelValid(opts) {
86
+ const { file, content } = opts;
87
+ if (this.opts.contentTest && this.opts.contentTest(content)) {
88
+ return true;
89
+ }
90
+ // transform with esbuild first
91
+ // to reduce unexpected ast problem
92
+ const loader = (0, path_1.extname)(file).slice(1);
93
+ const result = (0, esbuild_1.transformSync)(content, {
94
+ loader,
95
+ sourcemap: false,
96
+ minify: false,
97
+ });
98
+ // transform with babel
99
+ let ret = false;
100
+ const ast = parser.parse(result.code, {
101
+ sourceType: 'module',
102
+ sourceFilename: file,
103
+ plugins: [],
104
+ });
105
+ (0, traverse_1.default)(ast, {
106
+ ExportDefaultDeclaration: (path) => {
107
+ let node = path.node.declaration;
108
+ node = (0, astUtils_1.getIdentifierDeclaration)(node, path);
109
+ if (this.opts.astTest && this.opts.astTest({ node, content })) {
110
+ ret = true;
111
+ }
112
+ },
113
+ });
114
+ return ret;
115
+ }
116
+ static getModelsContent(models) {
117
+ const imports = [];
118
+ const modelProps = [];
119
+ models.forEach((model) => {
120
+ imports.push(`import ${model.id} from '${model.file}';`);
121
+ modelProps.push(`${model.id}: { namespace: '${model.namespace}', model: ${model.id} },`);
122
+ });
123
+ return `
124
+ ${imports.join('\n')}
125
+
126
+ export const models = {
127
+ ${modelProps.join('\n')}
128
+ }`;
129
+ }
130
+ }
131
+ exports.ModelUtils = ModelUtils;
@@ -0,0 +1,5 @@
1
+ export declare function resolveProjectDep(opts: {
2
+ pkg: any;
3
+ cwd: string;
4
+ dep: string;
5
+ }): string | undefined;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveProjectDep = void 0;
4
+ const path_1 = require("path");
5
+ const plugin_utils_1 = require("umi/plugin-utils");
6
+ function resolveProjectDep(opts) {
7
+ var _a, _b;
8
+ if (((_a = opts.pkg.dependencies) === null || _a === void 0 ? void 0 : _a[opts.dep]) ||
9
+ ((_b = opts.pkg.devDependencies) === null || _b === void 0 ? void 0 : _b[opts.dep])) {
10
+ return (0, path_1.dirname)(plugin_utils_1.resolve.sync(`${opts.dep}/package.json`, {
11
+ basedir: opts.cwd,
12
+ }));
13
+ }
14
+ }
15
+ exports.resolveProjectDep = resolveProjectDep;
@@ -0,0 +1,6 @@
1
+ import { IApi } from 'umi';
2
+ export declare function withTmpPath(opts: {
3
+ api: IApi;
4
+ path: string;
5
+ noPluginDir?: boolean;
6
+ }): string;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withTmpPath = void 0;
4
+ const utils_1 = require("@umijs/utils");
5
+ const path_1 = require("path");
6
+ function withTmpPath(opts) {
7
+ return (0, utils_1.winPath)((0, path_1.join)(opts.api.paths.absTmpPath, opts.api.plugin.key && !opts.noPluginDir
8
+ ? `plugin-${opts.api.plugin.key}`
9
+ : '', opts.path));
10
+ }
11
+ exports.withTmpPath = withTmpPath;
package/libs/dva.ts ADDED
@@ -0,0 +1,10 @@
1
+ // @ts-ignore
2
+ export { create, saga, utils } from 'dva-core';
3
+ export {
4
+ connect,
5
+ Provider,
6
+ useDispatch,
7
+ useSelector,
8
+ useStore,
9
+ } from 'react-redux';
10
+ export { bindActionCreators } from 'redux';
package/libs/model.tsx ADDED
@@ -0,0 +1,140 @@
1
+ // @ts-ignore
2
+ import isEqual from 'fast-deep-equal';
3
+ import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
4
+
5
+ // @ts-ignore
6
+ const Context = React.createContext<{ dispatcher: Dispatcher }>(null);
7
+
8
+ class Dispatcher {
9
+ callbacks: Record<string, Set<Function>> = {};
10
+ data: Record<string, unknown> = {};
11
+ update = (namespace: string) => {
12
+ if (this.callbacks[namespace]) {
13
+ this.callbacks[namespace].forEach((cb) => {
14
+ try {
15
+ const data = this.data[namespace];
16
+ cb(data);
17
+ } catch (e) {
18
+ cb(undefined);
19
+ }
20
+ });
21
+ }
22
+ };
23
+ }
24
+
25
+ interface ExecutorProps {
26
+ hook: () => any;
27
+ onUpdate: (val: any) => void;
28
+ namespace: string;
29
+ }
30
+
31
+ function Executor(props: ExecutorProps) {
32
+ const { hook, onUpdate, namespace } = props;
33
+
34
+ const updateRef = useRef(onUpdate);
35
+ const initialLoad = useRef(false);
36
+
37
+ let data: any;
38
+ try {
39
+ data = hook();
40
+ } catch (e) {
41
+ console.error(
42
+ `plugin-model: Invoking '${namespace || 'unknown'}' model failed:`,
43
+ e,
44
+ );
45
+ }
46
+
47
+ // 首次执行时立刻返回初始值
48
+ useMemo(() => {
49
+ updateRef.current(data);
50
+ }, []);
51
+
52
+ // React 16.13 后 update 函数用 useEffect 包裹
53
+ useEffect(() => {
54
+ if (initialLoad.current) {
55
+ updateRef.current(data);
56
+ } else {
57
+ initialLoad.current = true;
58
+ }
59
+ });
60
+
61
+ return null;
62
+ }
63
+
64
+ const dispatcher = new Dispatcher();
65
+
66
+ export function Provider(props: {
67
+ models: Record<string, any>;
68
+ children: React.ReactNode;
69
+ }) {
70
+ return (
71
+ <Context.Provider value={{ dispatcher }}>
72
+ {Object.keys(props.models).map((namespace) => {
73
+ return (
74
+ <Executor
75
+ key={namespace}
76
+ hook={props.models[namespace]}
77
+ namespace={namespace}
78
+ onUpdate={(val) => {
79
+ dispatcher.data[namespace] = val;
80
+ dispatcher.update(namespace);
81
+ }}
82
+ />
83
+ );
84
+ })}
85
+ {props.children}
86
+ </Context.Provider>
87
+ );
88
+ }
89
+
90
+ export function useModel(namespace: string, selector?: any) {
91
+ const { dispatcher } = useContext<{ dispatcher: Dispatcher }>(Context);
92
+ const selectorRef = useRef(selector);
93
+ selectorRef.current = selector;
94
+ const [state, setState] = useState(() =>
95
+ selectorRef.current
96
+ ? selectorRef.current(dispatcher.data[namespace])
97
+ : dispatcher.data[namespace],
98
+ );
99
+ const stateRef = useRef<any>(state);
100
+ stateRef.current = state;
101
+
102
+ const isMount = useRef(false);
103
+ useEffect(() => {
104
+ isMount.current = true;
105
+ return () => {
106
+ isMount.current = false;
107
+ };
108
+ }, []);
109
+
110
+ useEffect(() => {
111
+ const handler = (data: any) => {
112
+ if (!isMount.current) {
113
+ // 如果 handler 执行过程中,组件被卸载了,则强制更新全局 data
114
+ // TODO: 需要加个 example 测试
115
+ setTimeout(() => {
116
+ dispatcher.data[namespace] = data;
117
+ dispatcher.update(namespace);
118
+ });
119
+ } else {
120
+ const currentState = selectorRef.current
121
+ ? selectorRef.current(data)
122
+ : data;
123
+ const previousState = stateRef.current;
124
+ if (!isEqual(currentState, previousState)) {
125
+ setState(currentState);
126
+ }
127
+ }
128
+ };
129
+
130
+ dispatcher.callbacks[namespace] ||= new Set();
131
+ dispatcher.callbacks[namespace].add(handler);
132
+ dispatcher.update(namespace);
133
+
134
+ return () => {
135
+ dispatcher.callbacks[namespace].delete(handler);
136
+ };
137
+ }, [namespace]);
138
+
139
+ return state;
140
+ }
package/package.json CHANGED
@@ -1,31 +1,45 @@
1
1
  {
2
2
  "name": "@umijs/plugins",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.13",
4
4
  "description": "@umijs/plugins",
5
+ "homepage": "https://github.com/umijs/umi-next/tree/master/packages/plugins#readme",
6
+ "bugs": "https://github.com/umijs/umi-next/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/umijs/umi-next"
10
+ },
11
+ "license": "MIT",
5
12
  "main": "dist/index.js",
6
13
  "types": "dist/index.d.ts",
7
14
  "files": [
8
- "dist"
15
+ "dist",
16
+ "libs"
9
17
  ],
10
18
  "scripts": {
11
19
  "build": "pnpm tsc",
12
20
  "build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
13
21
  "dev": "pnpm build -- --watch"
14
22
  },
15
- "repository": {
16
- "type": "git",
17
- "url": "https://github.com/umijs/umi-next"
23
+ "dependencies": {
24
+ "@ant-design/icons": "^4.7.0",
25
+ "@ant-design/pro-layout": "^6.31.7",
26
+ "@umijs/bundler-utils": "4.0.0-beta.13",
27
+ "antd": "^4.17.3",
28
+ "antd-dayjs-webpack-plugin": "^1.0.6",
29
+ "babel-plugin-import": "^1.13.3",
30
+ "dayjs": "^1.10.7",
31
+ "dva-core": "^2.0.4",
32
+ "fast-deep-equal": "3.1.3",
33
+ "react-redux": "^7.2.6",
34
+ "redux": "^4.1.2"
35
+ },
36
+ "devDependencies": {
37
+ "umi": "4.0.0-beta.13"
18
38
  },
19
- "authors": [
20
- "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
21
- ],
22
- "license": "MIT",
23
- "bugs": "https://github.com/umijs/umi-next/issues",
24
- "homepage": "https://github.com/umijs/umi-next/tree/master/packages/plugins#readme",
25
39
  "publishConfig": {
26
40
  "access": "public"
27
41
  },
28
- "devDependencies": {
29
- "umi": "4.0.0-beta.1"
30
- }
42
+ "authors": [
43
+ "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
44
+ ]
31
45
  }
package/dist/sass.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = (api) => {
4
- api;
5
- };