mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-06-30 02:35:08 +08:00
refactor(plugin-model): 去掉provide
This commit is contained in:
parent
3ddc355738
commit
b73c1f9027
@ -1,9 +1,6 @@
|
||||
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { lodash, winPath } from '@fesjs/utils';
|
||||
import { getModels } from './utils/getModels';
|
||||
import { getTmpFile } from './utils/getTmpFile';
|
||||
|
||||
const namespace = 'plugin-model';
|
||||
|
||||
@ -13,6 +10,10 @@ export default (api) => {
|
||||
utils: { Mustache }
|
||||
} = api;
|
||||
|
||||
const { lodash, winPath } = require('@fesjs/utils');
|
||||
const { getModels } = require('./utils/getModels');
|
||||
const { getTmpFile } = require('./utils/getTmpFile');
|
||||
|
||||
function getModelDir() {
|
||||
return api.config.singular ? 'model' : 'models';
|
||||
}
|
||||
@ -25,22 +26,16 @@ export default (api) => {
|
||||
const srcModelsPath = getModelsPath();
|
||||
return lodash.uniq([
|
||||
...getModels(srcModelsPath)
|
||||
// ...getModels(
|
||||
// paths.absPagesPath,
|
||||
// `**/${getModelDir()}/**/*.{js,jsx}`
|
||||
// ),
|
||||
// ...getModels(paths.absPagesPath, '**/*.model.{js,jsx}')
|
||||
]);
|
||||
}
|
||||
|
||||
const absCoreFilePath = join(namespace, 'core.js');
|
||||
const absRuntimeFilePath = join(namespace, 'runtime.js');
|
||||
const absInitlaStateFilePath = join(namespace, 'models/initialState.js');
|
||||
const absInitialStateFilePath = join(namespace, 'models/initialState.js');
|
||||
|
||||
api.register({
|
||||
key: 'addExtraModels',
|
||||
fn: () => [{
|
||||
absPath: winPath(join(paths.absTmpPath, absInitlaStateFilePath)),
|
||||
absPath: winPath(join(paths.absTmpPath, absInitialStateFilePath)),
|
||||
namespace: '@@initialState'
|
||||
}]
|
||||
});
|
||||
@ -63,16 +58,10 @@ export default (api) => {
|
||||
})
|
||||
});
|
||||
|
||||
api.writeTmpFile({
|
||||
path: absRuntimeFilePath,
|
||||
content: Mustache.render(readFileSync(join(__dirname, 'runtime/runtime.tpl'), 'utf-8'), {
|
||||
})
|
||||
});
|
||||
|
||||
api.writeTmpFile({
|
||||
path: absInitlaStateFilePath,
|
||||
content: Mustache.render(readFileSync(join(__dirname, 'runtime/models/initialState.tpl'), 'utf-8'), {
|
||||
})
|
||||
api.copyTmpFiles({
|
||||
namespace,
|
||||
path: join(__dirname, 'runtime'),
|
||||
ignore: ['.tpl']
|
||||
});
|
||||
});
|
||||
|
||||
@ -82,6 +71,4 @@ export default (api) => {
|
||||
source: absCoreFilePath
|
||||
}
|
||||
]);
|
||||
|
||||
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
|
||||
};
|
||||
|
@ -1,39 +1,29 @@
|
||||
import { inject } from "vue";
|
||||
|
||||
const modelKey = Symbol("plugin-model");
|
||||
|
||||
{{{userImports}}}
|
||||
{{{extraImports}}}
|
||||
|
||||
export const models = {
|
||||
export const models = {
|
||||
{{#extraModels}}
|
||||
{{{extraModels}}},
|
||||
{{/extraModels}}
|
||||
{{#userModels}}
|
||||
{{{userModels}}},
|
||||
{{/userModels}}
|
||||
}
|
||||
const cache = new Map();
|
||||
|
||||
export const install = (app)=>{
|
||||
const useModel = (name) => {
|
||||
const model = models[name];
|
||||
if(model === undefined){
|
||||
throw new Error("[plugin-model]: useModel, name is undefined.");
|
||||
}
|
||||
if (typeof model !== "function") {
|
||||
throw new Error("[plugin-model]: useModel is not a function.");
|
||||
}
|
||||
if(!cache.has(name)){
|
||||
cache.set(name, model())
|
||||
}
|
||||
return cache.get(name)
|
||||
};
|
||||
app.provide(modelKey, useModel);
|
||||
}
|
||||
|
||||
export const useModel = (name) => {
|
||||
return inject(modelKey)(name);
|
||||
};
|
||||
|
||||
|
||||
const cache = new Map();
|
||||
|
||||
export const useModel = (name) => {
|
||||
const modelFunc = models[name];
|
||||
if (modelFunc === undefined) {
|
||||
throw new Error('[plugin-model]: useModel, name is undefined.');
|
||||
}
|
||||
if (typeof modelFunc !== 'function') {
|
||||
throw new Error('[plugin-model]: useModel is not a function.');
|
||||
}
|
||||
if (!cache.has(name)) {
|
||||
cache.set(name, modelFunc());
|
||||
}
|
||||
return cache.get(name);
|
||||
};
|
||||
|
@ -0,0 +1,5 @@
|
||||
import { inject } from 'vue';
|
||||
|
||||
export default function initialStateModel() {
|
||||
return inject('initialState');
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
import { inject, reactive } from "vue";
|
||||
|
||||
export default function initalModel() {
|
||||
return reactive(inject("initialState"));
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
import { install } from "./core";
|
||||
|
||||
export function onAppCreated({ app }) {
|
||||
install(app)
|
||||
}
|
@ -3,13 +3,13 @@ import { getValidFiles } from '.';
|
||||
|
||||
export function getModels(cwd, pattern) {
|
||||
const files = glob
|
||||
.sync(pattern || '**/*.{js,jsx}', {
|
||||
.sync(pattern || '**/*.{js,jsx,ts,tsx}', {
|
||||
cwd
|
||||
})
|
||||
.filter(
|
||||
file => !file.endsWith('.d.ts')
|
||||
&& !file.endsWith('.test.js')
|
||||
&& !file.endsWith('.test.jsx')
|
||||
&& !file.endsWith('.test.js')
|
||||
&& !file.endsWith('.test.jsx')
|
||||
);
|
||||
|
||||
return getValidFiles(files, cwd);
|
||||
|
Loading…
x
Reference in New Issue
Block a user