92 lines
2.6 KiB
TypeScript

import { CLASS_DEFINE_CHUNK_NAME } from '../../../const/generator';
import { Scope } from '../../../utils/Scope';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
IContainerInfo,
} from '../../../types';
import { DEFAULT_LINK_AFTER } from '../../../const';
export interface 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,
};
const ir = next.ir as IContainerInfo;
const scope = Scope.createRootScope();
const { inStrictMode } = next.contextData;
if (!inStrictMode) {
// 非严格模式下,上下文就是自己
next.chunks.push({
type: ChunkType.STRING,
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
content: `
_context = this;
`,
linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start],
});
} else {
// 严格模式下的上下文只保留协议中规定的那些
next.chunks.push({
type: ChunkType.STRING,
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
content: `
_context = this._createContext();
`,
linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod,
content: `
_createContext() {
const self = this;
const context = {
get state() { return self.state; },
setState(newState, callback) { self.setState(newState, callback); },
get dataSourceMap() { return self._dataSourceEngine.dataSourceMap || {}; },
async reloadDataSource() { await self._dataSourceEngine.reloadDataSource(); },
get utils() { return self.utils; },
get page() { return context; },
get component() { return context; },
get props() { return self.props; },
get constants() { return self.constants; },
get $() { return self.$ },
get $$() { return self.$$ },
...this._methods,
};
return context;
}
`,
linkAfter: DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod],
});
}
return next;
};
return plugin;
};
export default pluginFactory;