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