• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  storedFileInfo
27} from './utils';
28import { BUILD_ON } from './pre_define';
29import { parseVisual } from './process_visual';
30import {
31  CUSTOM_BUILDER_METHOD,
32  GLOBAL_CUSTOM_BUILDER_METHOD,
33  INNER_CUSTOM_BUILDER_METHOD,
34  INNER_CUSTOM_LOCALBUILDER_METHOD
35} from './component_map';
36
37function preProcess(source: string): string {
38  process.env.compiler = BUILD_ON;
39  if (/\.ets$/.test(this.resourcePath)) {
40    storedFileInfo.setCurrentArkTsFile();
41    clearCollection();
42    const result: ReplaceResult = sourceReplace(source, this.resourcePath);
43    let newContent: string = result.content;
44    const log: LogInfo[] = result.log.concat(validateUISyntax(source, newContent,
45      this.resourcePath, this.resourceQuery));
46    newContent = parseVisual(this.resourcePath, this.resourceQuery, newContent, log, source);
47    if (log.length) {
48      emitLogInfo(this, log);
49    }
50    return newContent;
51  } else {
52    return processSystemApi(source, false, this.resourcePath);
53  }
54}
55
56function clearCollection(): void {
57  componentCollection.customComponents.clear();
58  CUSTOM_BUILDER_METHOD.clear();
59  INNER_CUSTOM_LOCALBUILDER_METHOD.clear();
60  GLOBAL_CUSTOM_BUILDER_METHOD.clear();
61  INNER_CUSTOM_BUILDER_METHOD.clear();
62}
63
64module.exports = preProcess;
65