• 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
16const OHOS_THEME_PROP_GROUPS = require('./theme/ohosStyles')
17module.exports = function (source) {
18  let result
19  // target format: this.$r("ohos.id_color_background") or this.$r('ohos.id_color_background')  or
20  // "this.$r('ohos.id_color_background')"
21  let ResourceRefReg = /"?this\s*\.\$r\s*\(\s*((['"]ohos\.(?<resName>\w+)['"]))\s*\)"?/g
22  while (result = ResourceRefReg.exec(source)) {
23    const resourceName = result.groups['resName']
24    if (resourceName && OHOS_THEME_PROP_GROUPS[resourceName]) {
25      const resourceId = "\"@ohos_id_" + OHOS_THEME_PROP_GROUPS[resourceName] + "\""
26      source = source.replace(result[0], resourceId)
27      // update the start position of the next match
28      ResourceRefReg.lastIndex -= result[0].length - resourceId.length
29    }
30  }
31
32  // target format: this.$r("sys.type.id_color_background") or this.$r('sys.type.id_color_background') or
33  // "this.$r('sys.type.id_color_background')"
34  // The "type" field can be "float", "color", "string" and so on.
35  let SysResourceTypeRefReg = /"?this\s*\.\$r\s*\(\s*((['"]sys\.(?<resType>\w+)\.(?<resName>\w+)['"]))\s*\)"?/g
36  while (result = SysResourceTypeRefReg.exec(source)) {
37    const resourceName = result.groups['resName']
38    const resourceType = result.groups['resType']
39    if (resourceName && resourceType && OHOS_THEME_PROP_GROUPS[resourceName]) {
40      const resourceId = "\"@sys." + resourceType + "." + OHOS_THEME_PROP_GROUPS[resourceName] + "\""
41      source = source.replace(result[0], resourceId)
42      // update the start position of the next match
43      SysResourceTypeRefReg.lastIndex -= result[0].length - resourceId.length
44    }
45  }
46
47  // target format: this.$r("app.type.developer_defined_color") or this.$r('app.type.developer_defined_color') or
48  // "this.$r('app.type.developer_defined_color')"
49  // The "type" field can be "float", "color", "string" and so on.
50  let AppResourceTypeRefReg = /"?this\s*\.\$r\s*\(\s*((['"]app\.(?<resType>\w+)\.(?<resName>\w+)['"]))\s*\)"?/g
51  while (result = AppResourceTypeRefReg.exec(source)) {
52    const resourceName = result.groups['resName']
53    const resourceType = result.groups['resType']
54    if (resourceName && resourceType) {
55      const resourceId = "\"@app." + resourceType + "." + resourceName + "\""
56      source = source.replace(result[0], resourceId)
57      // update the start position of the next match
58      AppResourceTypeRefReg.lastIndex -= result[0].length - resourceId.length
59    }
60  }
61
62  return source
63}