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_ciimport {
2188c88e8eSopenharmony_ci  logWarn
2288c88e8eSopenharmony_ci} from './util'
2388c88e8eSopenharmony_ci
2488c88e8eSopenharmony_ciimport {
2588c88e8eSopenharmony_ci  parseTemplate
2688c88e8eSopenharmony_ci} from './parser'
2788c88e8eSopenharmony_ciconst compiler = require('./lite/lite-transform-template')
2888c88e8eSopenharmony_ciconst { DEVICE_LEVEL } = require('./lite/lite-enum')
2988c88e8eSopenharmony_cimodule.exports = function (source) {
3088c88e8eSopenharmony_ci  if (process.env.DEVICE_LEVEL !== DEVICE_LEVEL.RICH && source.lastIndexOf('</element>') > 0) {
3188c88e8eSopenharmony_ci    const count = source.lastIndexOf('</element>')
3288c88e8eSopenharmony_ci    source = source.substr(count + 10, source.length)
3388c88e8eSopenharmony_ci  }
3488c88e8eSopenharmony_ci  this.cacheable && this.cacheable()
3588c88e8eSopenharmony_ci
3688c88e8eSopenharmony_ci  const callback = this.async()
3788c88e8eSopenharmony_ci
3888c88e8eSopenharmony_ci  let hasError = false
3988c88e8eSopenharmony_ci  parseTemplate(source, this.resourcePath)
4088c88e8eSopenharmony_ci    .then(({ parsed, log }) => {
4188c88e8eSopenharmony_ci      if (log && log.length) {
4288c88e8eSopenharmony_ci        hasError = logWarn(this, log)
4388c88e8eSopenharmony_ci      }
4488c88e8eSopenharmony_ci      // If there is an error during compilation, an empty object is returned
4588c88e8eSopenharmony_ci      if (process.env.DEVICE_LEVEL === DEVICE_LEVEL.LITE) {
4688c88e8eSopenharmony_ci        if (hasError) {
4788c88e8eSopenharmony_ci          parsed = `function () { return {} }`
4888c88e8eSopenharmony_ci        }
4988c88e8eSopenharmony_ci        else {
5088c88e8eSopenharmony_ci          parsed = compiler.transformTemplate(parsed)
5188c88e8eSopenharmony_ci        }
5288c88e8eSopenharmony_ci      }
5388c88e8eSopenharmony_ci      callback(null, parsed)
5488c88e8eSopenharmony_ci    }).catch(e => {
5588c88e8eSopenharmony_ci      logWarn(this, [{
5688c88e8eSopenharmony_ci        reason: 'ERROR: Failed to parse the HML file. ' + e
5788c88e8eSopenharmony_ci      }])
5888c88e8eSopenharmony_ci      callback('')
5988c88e8eSopenharmony_ci    })
6088c88e8eSopenharmony_ci}
61