• 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 
16 import { processSystemApi } from './validate_ui_syntax';
17 import { systemModules } from '../main';
18 
19 module.exports = function processSystemModule(source: string): string {
20   const REG_IMPORT: RegExp =
21     /(import|export)\s+(.+)\s+from\s+['"](\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"](\S+)['"]\s*\)/g;
22   source.replace(REG_IMPORT, (item, item1, item2, item3, item4, item5) => {
23     const moduleRequest: string = item3 || item5;
24     if (/^@(system|ohos)\./i.test(moduleRequest.trim())) {
25       if (!systemModules.includes(moduleRequest.trim() + '.d.ts')) {
26         const message: string =
27           `Cannot find module '${moduleRequest}' or its corresponding type declarations.`;
28         this.emitError(`BUILDERROR File: ${this.resourcePath}\n ${message}`);
29       }
30     }
31     return item;
32   });
33   source = processSystemApi(source, false, this.resourcePath, true);
34   return source;
35 };
36