mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-13 01:21:58 +00:00
75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
import { CLASS_DEFINE_CHUNK_NAME, COMMON_CHUNK_NAME } from '../../../const/generator';
|
|
|
|
import {
|
|
BuilderComponentPlugin,
|
|
BuilderComponentPluginFactory,
|
|
ChunkType,
|
|
FileType,
|
|
ICodeStruct,
|
|
} from '../../../types';
|
|
import { RAX_CHUNK_NAME } from './const';
|
|
|
|
type PluginConfig = {
|
|
fileType: string;
|
|
};
|
|
|
|
const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => {
|
|
const cfg: PluginConfig = {
|
|
fileType: FileType.JSX,
|
|
...config,
|
|
};
|
|
|
|
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
|
|
const next: ICodeStruct = {
|
|
...pre,
|
|
};
|
|
|
|
next.chunks.push({
|
|
type: ChunkType.STRING,
|
|
fileType: cfg.fileType,
|
|
name: COMMON_CHUNK_NAME.InternalDepsImport,
|
|
// TODO: 下面这个路径有没有更好的方式来获取?而非写死
|
|
content: `
|
|
import __$$projectUtils from '../../utils';
|
|
`,
|
|
linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport],
|
|
});
|
|
|
|
next.chunks.push({
|
|
type: ChunkType.STRING,
|
|
fileType: cfg.fileType,
|
|
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
|
|
content: '_utils = this._defineUtils();',
|
|
linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start],
|
|
});
|
|
|
|
next.chunks.push({
|
|
type: ChunkType.STRING,
|
|
fileType: cfg.fileType,
|
|
name: CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod,
|
|
|
|
// 绑定下上下文,这样在所有的 utils 里面都能通过 this.xxx 来访问上下文了
|
|
content: `
|
|
_defineUtils() {
|
|
const utils = {
|
|
...__$$projectUtils,
|
|
};
|
|
|
|
Object.entries(utils).forEach(([name, util]) => {
|
|
if (typeof util === 'function') {
|
|
utils[name] = util.bind(this._context);
|
|
}
|
|
});
|
|
|
|
return utils;
|
|
}`,
|
|
linkAfter: [RAX_CHUNK_NAME.ClassRenderEnd],
|
|
});
|
|
|
|
return next;
|
|
};
|
|
return plugin;
|
|
};
|
|
|
|
export default pluginFactory;
|