@umijs/plugins 4.0.0-beta.12 → 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.
- package/dist/access.js +73 -1
- package/dist/{dayjs.d.ts → analytics.d.ts} +0 -0
- package/dist/analytics.js +67 -0
- package/dist/antd.js +0 -25
- package/dist/dva.d.ts +3 -9
- package/dist/dva.js +58 -106
- package/dist/initial-state.js +112 -1
- package/dist/layout.js +445 -1
- package/dist/model.d.ts +0 -9
- package/dist/model.js +71 -74
- package/dist/moment2dayjs.d.ts +3 -0
- package/dist/moment2dayjs.js +96 -0
- package/dist/utils/{getIdentifierDeclaration.d.ts → astUtils.d.ts} +0 -0
- package/dist/utils/{getIdentifierDeclaration.js → astUtils.js} +0 -0
- package/dist/utils/modelUtils.d.ts +34 -0
- package/dist/utils/modelUtils.js +131 -0
- package/libs/dva.ts +10 -0
- package/libs/model.tsx +140 -0
- package/package.json +12 -6
- package/dist/dayjs.js +0 -5
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("@umijs/utils");
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
/*
|
|
6
|
+
As long as moment2dayjs is registered, moment will be replaced by dayjs.
|
|
7
|
+
The presets that can adapt to antd is registered by default.
|
|
8
|
+
When the user configures preset and plugins at the same time, we will merge them.
|
|
9
|
+
*/
|
|
10
|
+
exports.default = (api) => {
|
|
11
|
+
api.describe({
|
|
12
|
+
key: 'moment2dayjs',
|
|
13
|
+
config: {
|
|
14
|
+
schema(joi) {
|
|
15
|
+
return joi.object({
|
|
16
|
+
preset: joi.string(),
|
|
17
|
+
plugins: joi.array(),
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
enableBy: api.EnableBy.config,
|
|
22
|
+
});
|
|
23
|
+
const presets = {
|
|
24
|
+
antd: [
|
|
25
|
+
'isSameOrBefore',
|
|
26
|
+
'isSameOrAfter',
|
|
27
|
+
'advancedFormat',
|
|
28
|
+
'customParseFormat',
|
|
29
|
+
'weekday',
|
|
30
|
+
'weekYear',
|
|
31
|
+
'weekOfYear',
|
|
32
|
+
'isMoment',
|
|
33
|
+
'localeData',
|
|
34
|
+
'localizedFormat',
|
|
35
|
+
],
|
|
36
|
+
antdv3: [
|
|
37
|
+
'isSameOrBefore',
|
|
38
|
+
'isSameOrAfter',
|
|
39
|
+
'advancedFormat',
|
|
40
|
+
'customParseFormat',
|
|
41
|
+
'weekday',
|
|
42
|
+
'weekYear',
|
|
43
|
+
'weekOfYear',
|
|
44
|
+
'isMoment',
|
|
45
|
+
'localeData',
|
|
46
|
+
'localizedFormat',
|
|
47
|
+
'badMutable',
|
|
48
|
+
],
|
|
49
|
+
};
|
|
50
|
+
const getDayjsPlugins = (api) => {
|
|
51
|
+
let { preset = 'antd', plugins = [] } = api.config.moment2dayjs || {};
|
|
52
|
+
switch (preset) {
|
|
53
|
+
case 'antd':
|
|
54
|
+
return Array.from(new Set(presets['antd'].concat(plugins)));
|
|
55
|
+
case 'antdv3':
|
|
56
|
+
return Array.from(new Set(presets['antdv3'].concat(plugins)));
|
|
57
|
+
case 'none':
|
|
58
|
+
return [].concat(plugins);
|
|
59
|
+
default:
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
// replace moment
|
|
64
|
+
api.modifyConfig((memo) => {
|
|
65
|
+
memo.alias.moment = (0, path_1.dirname)(require.resolve('dayjs/package.json'));
|
|
66
|
+
return memo;
|
|
67
|
+
});
|
|
68
|
+
api.onGenerateFiles(() => {
|
|
69
|
+
const plugins = getDayjsPlugins(api);
|
|
70
|
+
const runtimeTpl = `
|
|
71
|
+
import dayjs from '{{{dayjsPath}}}';
|
|
72
|
+
import antdPlugin from '{{{dayjsAntdPluginPath}}}';
|
|
73
|
+
|
|
74
|
+
{{#plugins}}
|
|
75
|
+
import {{.}} from '{{{dayjsPath}}}/plugin/{{.}}';
|
|
76
|
+
{{/plugins}}
|
|
77
|
+
|
|
78
|
+
{{#plugins}}
|
|
79
|
+
dayjs.extend({{.}});
|
|
80
|
+
{{/plugins}}
|
|
81
|
+
|
|
82
|
+
dayjs.extend(antdPlugin);
|
|
83
|
+
`;
|
|
84
|
+
const dayjsAntdPluginPath = require.resolve('antd-dayjs-webpack-plugin/src/antd-plugin');
|
|
85
|
+
const dayjsPath = (0, path_1.dirname)(require.resolve('dayjs/package.json'));
|
|
86
|
+
api.writeTmpFile({
|
|
87
|
+
path: 'runtime.tsx',
|
|
88
|
+
content: utils_1.Mustache.render(runtimeTpl, {
|
|
89
|
+
plugins,
|
|
90
|
+
dayjsPath,
|
|
91
|
+
dayjsAntdPluginPath,
|
|
92
|
+
}),
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
api.addEntryCodeAhead(() => [`import './plugin-moment2dayjs/runtime.tsx'`]);
|
|
96
|
+
};
|
|
File without changes
|
|
File without changes
|
|
@@ -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;
|
package/libs/dva.ts
ADDED
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/plugins",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.13",
|
|
4
4
|
"description": "@umijs/plugins",
|
|
5
5
|
"homepage": "https://github.com/umijs/umi-next/tree/master/packages/plugins#readme",
|
|
6
6
|
"bugs": "https://github.com/umijs/umi-next/issues",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"main": "dist/index.js",
|
|
13
13
|
"types": "dist/index.d.ts",
|
|
14
14
|
"files": [
|
|
15
|
-
"dist"
|
|
15
|
+
"dist",
|
|
16
|
+
"libs"
|
|
16
17
|
],
|
|
17
18
|
"scripts": {
|
|
18
19
|
"build": "pnpm tsc",
|
|
@@ -20,15 +21,20 @@
|
|
|
20
21
|
"dev": "pnpm build -- --watch"
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@
|
|
24
|
-
"
|
|
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",
|
|
25
28
|
"antd-dayjs-webpack-plugin": "^1.0.6",
|
|
26
29
|
"babel-plugin-import": "^1.13.3",
|
|
27
30
|
"dayjs": "^1.10.7",
|
|
28
|
-
"dva": "^2.
|
|
31
|
+
"dva-core": "^2.0.4",
|
|
32
|
+
"fast-deep-equal": "3.1.3",
|
|
33
|
+
"react-redux": "^7.2.6",
|
|
34
|
+
"redux": "^4.1.2"
|
|
29
35
|
},
|
|
30
36
|
"devDependencies": {
|
|
31
|
-
"umi": "4.0.0-beta.
|
|
37
|
+
"umi": "4.0.0-beta.13"
|
|
32
38
|
},
|
|
33
39
|
"publishConfig": {
|
|
34
40
|
"access": "public"
|