fix: 🐛 解决 Rax 出码到小程序的时候 require(xxx) 语句不能被编译的问题

This commit is contained in:
牧毅 2020-08-18 23:03:43 +08:00
parent 42e41bbd12
commit 332a473cd2
2 changed files with 31 additions and 19 deletions

View File

@ -11,6 +11,7 @@ import {
DataSourceConfig,
isJSExpression,
isJSFunction,
JSExpression,
} from '../../../types';
import { generateUnknownType } from '../../../utils/compositeType';
@ -37,23 +38,30 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
const dataSourceItems: DataSourceConfig[] = (dataSourceConfig && dataSourceConfig.list) || [];
const dataSourceEngineOptions = { runtimeConfig: true };
if (dataSourceItems.length > 0) {
Object.assign(dataSourceEngineOptions, {
requestHandlersMap: dataSourceItems.reduce(
(handlers, ds) =>
ds.type in handlers && ds.type !== 'custom'
? handlers
: {
...handlers,
[ds.type]: {
type: 'JSExpression',
value:
`require('@ali/lowcode-datasource-engine/handlers/${changeCase.kebabCase(ds.type)}')` +
(ds.type === 'urlParams' ? '({ search: this.props.location.search })' : ''),
},
},
{} as Record<string, CompositeValue>,
),
const requestHandlersMap = {} as Record<string, JSExpression>;
dataSourceItems.forEach((ds) => {
if (!(ds.type in requestHandlersMap) && ds.type !== 'custom') {
const handlerName = '__$$' + changeCase.camelCase(ds.type) + 'RequestHandler';
requestHandlersMap[ds.type] = {
type: 'JSExpression',
value: handlerName + (ds.type === 'urlParams' ? '({ search: this.props.location.search })' : ''),
};
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.ExternalDepsImport,
content: `
import ${handlerName} from '@ali/lowcode-datasource-engine/handlers/${changeCase.kebabCase(ds.type)}';
`,
linkAfter: [],
});
}
});
Object.assign(dataSourceEngineOptions, { requestHandlersMap });
}
next.chunks.push({

View File

@ -9,6 +9,10 @@ import Text from 'rax-text';
import Image from 'rax-image';
import __$$urlParamsRequestHandler from '@ali/lowcode-datasource-engine/handlers/url-params';
import __$$fetchRequestHandler from '@ali/lowcode-datasource-engine/handlers/fetch';
import { create as __$$createDataSourceEngine } from '@ali/lowcode-datasource-engine';
import { isMiniApp as __$$isMiniApp } from 'universal-env';
@ -44,9 +48,9 @@ class Home$$Page extends Component {
_dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, {
runtimeConfig: true,
requestHandlers: {
urlParams: require('@ali/lowcode-datasource-engine/handlers/url-params')({ search: this.props.location.search }),
fetch: require('@ali/lowcode-datasource-engine/handlers/fetch'),
requestHandlersMap: {
urlParams: __$$urlParamsRequestHandler({ search: this.props.location.search }),
fetch: __$$fetchRequestHandler,
},
});