1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { 17 validateUISyntax, 18 processSystemApi, 19 ReplaceResult, 20 sourceReplace, 21 componentCollection 22} from './validate_ui_syntax'; 23import { 24 LogInfo, 25 emitLogInfo, 26} from './utils'; 27import { BUILD_ON } from './pre_define'; 28import { parseVisual } from './process_visual'; 29import { 30 CUSTOM_BUILDER_METHOD, 31 GLOBAL_CUSTOM_BUILDER_METHOD, 32 INNER_CUSTOM_BUILDER_METHOD 33} from './component_map'; 34 35function preProcess(source: string): string { 36 process.env.compiler = BUILD_ON; 37 if (/\.ets$/.test(this.resourcePath)) { 38 clearCollection(); 39 const result: ReplaceResult = sourceReplace(source, this.resourcePath); 40 let newContent: string = result.content; 41 const log: LogInfo[] = result.log.concat(validateUISyntax(source, newContent, 42 this.resourcePath, this.resourceQuery)); 43 newContent = parseVisual(this.resourcePath, this.resourceQuery, newContent, log, source); 44 if (log.length) { 45 emitLogInfo(this, log); 46 } 47 return newContent; 48 } else { 49 return processSystemApi(source, false, this.resourcePath); 50 } 51} 52 53function clearCollection(): void { 54 componentCollection.customComponents.clear(); 55 CUSTOM_BUILDER_METHOD.clear(); 56 GLOBAL_CUSTOM_BUILDER_METHOD.clear(); 57 INNER_CUSTOM_BUILDER_METHOD.clear(); 58} 59 60module.exports = preProcess; 61