188c88e8eSopenharmony_ci/*
288c88e8eSopenharmony_ci * Licensed to the Apache Software Foundation (ASF) under one
388c88e8eSopenharmony_ci * or more contributor license agreements.  See the NOTICE file
488c88e8eSopenharmony_ci * distributed with this work for additional information
588c88e8eSopenharmony_ci * regarding copyright ownership.  The ASF licenses this file
688c88e8eSopenharmony_ci * to you under the Apache License, Version 2.0 (the
788c88e8eSopenharmony_ci * "License"); you may not use this file except in compliance
888c88e8eSopenharmony_ci * with the License.  You may obtain a copy of the License at
988c88e8eSopenharmony_ci *
1088c88e8eSopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
1188c88e8eSopenharmony_ci *
1288c88e8eSopenharmony_ci * Unless required by applicable law or agreed to in writing,
1388c88e8eSopenharmony_ci * software distributed under the License is distributed on an
1488c88e8eSopenharmony_ci * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1588c88e8eSopenharmony_ci * KIND, either express or implied.  See the License for the
1688c88e8eSopenharmony_ci * specific language governing permissions and limitations
1788c88e8eSopenharmony_ci * under the License.
1888c88e8eSopenharmony_ci */
1988c88e8eSopenharmony_ci
2088c88e8eSopenharmony_ciconst path = require('path')
2188c88e8eSopenharmony_ci
2288c88e8eSopenharmony_ciconst transCardArray = require('./templater/bind').transCardArray
2388c88e8eSopenharmony_ciconst resourceReferenceParsing = require('./resource-reference-script')
2488c88e8eSopenharmony_ci
2588c88e8eSopenharmony_ciimport { logWarn } from './util'
2688c88e8eSopenharmony_ci
2788c88e8eSopenharmony_ciconst REG_EVENT_STRING = /("\s*\$event\..+")|('\s*\$event\..+')/g
2888c88e8eSopenharmony_ciconst REG_EVENT = /\$event\.[\w]+/g
2988c88e8eSopenharmony_ciconst REG_THIS = /this\..*/g
3088c88e8eSopenharmony_ci
3188c88e8eSopenharmony_cimodule.exports = function (source) {
3288c88e8eSopenharmony_ci  this.cacheable && this.cacheable()
3388c88e8eSopenharmony_ci
3488c88e8eSopenharmony_ci  const extName = path.extname(this.resourcePath)
3588c88e8eSopenharmony_ci  if (process.env.DEVICE_LEVEL === 'card') {
3688c88e8eSopenharmony_ci    source = source.replace(/\/\*((\n|\r|.)*?)\*\//mg, "")
3788c88e8eSopenharmony_ci    source = source.replace(/(\s|\;|^|\{|\})\/\/.*$/mg, "$1")
3888c88e8eSopenharmony_ci    if (extName === '.js' || extName === '.json') {
3988c88e8eSopenharmony_ci      if(source.trim().indexOf('export default') === 0) {
4088c88e8eSopenharmony_ci        source = source.replace('export default', '')
4188c88e8eSopenharmony_ci      }
4288c88e8eSopenharmony_ci      source = resourceReferenceParsing(source)
4388c88e8eSopenharmony_ci      source = source.replace(REG_EVENT_STRING, item => {
4488c88e8eSopenharmony_ci        return item.slice(1,-1)
4588c88e8eSopenharmony_ci      })
4688c88e8eSopenharmony_ci      source = source.replace(REG_EVENT, item => {
4788c88e8eSopenharmony_ci        return '"' + item + '"'
4888c88e8eSopenharmony_ci      })
4988c88e8eSopenharmony_ci      source = source.replace(REG_THIS, item => {
5088c88e8eSopenharmony_ci        if (item.charAt(item.length-1) !== '\"' &&
5188c88e8eSopenharmony_ci          item.charAt(item.length-1) !== '\'' &&
5288c88e8eSopenharmony_ci          item.slice(-2) !== '\"\,' &&
5388c88e8eSopenharmony_ci          item.slice(-2) !== '\'\,') {
5488c88e8eSopenharmony_ci          if (item.charAt(item.length-1) === ',') {
5588c88e8eSopenharmony_ci            item = `"{{${transCardArray(item.slice(0, -1))}}}",`.replace(/this\./g, '')
5688c88e8eSopenharmony_ci          } else {
5788c88e8eSopenharmony_ci            item = `"{{${transCardArray(item)}}}"`.replace(/this\./g,'')
5888c88e8eSopenharmony_ci          }
5988c88e8eSopenharmony_ci        }
6088c88e8eSopenharmony_ci        return item
6188c88e8eSopenharmony_ci      })
6288c88e8eSopenharmony_ci      if (extName === '.js') {
6388c88e8eSopenharmony_ci        try {
6488c88e8eSopenharmony_ci          source = JSON.stringify(eval('(' + source + ')'))
6588c88e8eSopenharmony_ci        } catch (e) {
6688c88e8eSopenharmony_ci          logWarn(this, [{
6788c88e8eSopenharmony_ci            reason: 'ERROR: Failed to parse the file : ' + this.resourcePath + `\n${e}`
6888c88e8eSopenharmony_ci          }])
6988c88e8eSopenharmony_ci          return `{}`
7088c88e8eSopenharmony_ci        }
7188c88e8eSopenharmony_ci      } else {
7288c88e8eSopenharmony_ci        return source
7388c88e8eSopenharmony_ci      }
7488c88e8eSopenharmony_ci    }
7588c88e8eSopenharmony_ci  }
7688c88e8eSopenharmony_ci  return `module.exports = ${source}`
7788c88e8eSopenharmony_ci}
78