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 path from 'path'
2188c88e8eSopenharmony_ci
2288c88e8eSopenharmony_ciimport {
2388c88e8eSopenharmony_ci  logWarn,
2488c88e8eSopenharmony_ci  parseRequireModule
2588c88e8eSopenharmony_ci} from './util'
2688c88e8eSopenharmony_ciimport {
2788c88e8eSopenharmony_ci  parseScript
2888c88e8eSopenharmony_ci} from './parser'
2988c88e8eSopenharmony_ci
3088c88e8eSopenharmony_ciconst { DEVICE_LEVEL } = require('./lite/lite-enum')
3188c88e8eSopenharmony_ci
3288c88e8eSopenharmony_cimodule.exports = function (source, map) {
3388c88e8eSopenharmony_ci  this.cacheable && this.cacheable()
3488c88e8eSopenharmony_ci  const callback = this.async()
3588c88e8eSopenharmony_ci  parseScript(source, this.resourcePath)
3688c88e8eSopenharmony_ci    .then(({
3788c88e8eSopenharmony_ci      parsed, log
3888c88e8eSopenharmony_ci    }) => {
3988c88e8eSopenharmony_ci      if (log && log.length) {
4088c88e8eSopenharmony_ci        logWarn(this, log)
4188c88e8eSopenharmony_ci      }
4288c88e8eSopenharmony_ci      parsed = parseRequireModule(parsed, this.resourcePath);
4388c88e8eSopenharmony_ci      if (process.env.DEVICE_LEVEL === DEVICE_LEVEL.RICH || process.env.DEVICE_LEVEL === 'card') {
4488c88e8eSopenharmony_ci        const appName = process.env.abilityType === 'page' ? 'app.js' : `${process.env.abilityType}.js`
4588c88e8eSopenharmony_ci        if (path.basename(this.resourcePath) !== appName) {
4688c88e8eSopenharmony_ci          parsed += `\nvar moduleOwn = exports.default || module.exports;\n` +
4788c88e8eSopenharmony_ci            `var accessors = ['public', 'protected', 'private'];
4888c88e8eSopenharmony_ciif (moduleOwn.data && accessors.some(function (acc) {
4988c88e8eSopenharmony_ci    return moduleOwn[acc];
5088c88e8eSopenharmony_ci  })) {\n  throw new Error('For VM objects, attribute data must not coexist with public,` +
5188c88e8eSopenharmony_ci  ` protected, or private. Please replace data with public.');
5288c88e8eSopenharmony_ci} else if (!moduleOwn.data) {
5388c88e8eSopenharmony_ci  moduleOwn.data = {};\n  moduleOwn._descriptor = {};\n  accessors.forEach(function(acc) {
5488c88e8eSopenharmony_ci    var accType = typeof moduleOwn[acc];
5588c88e8eSopenharmony_ci    if (accType === 'object') {
5688c88e8eSopenharmony_ci      moduleOwn.data = Object.assign(moduleOwn.data, moduleOwn[acc]);
5788c88e8eSopenharmony_ci      for (var name in moduleOwn[acc]) {
5888c88e8eSopenharmony_ci        moduleOwn._descriptor[name] = {access : acc};
5988c88e8eSopenharmony_ci      }
6088c88e8eSopenharmony_ci    } else if (accType === 'function') {
6188c88e8eSopenharmony_ci      console.warn('For VM objects, attribute ' + acc +` +
6288c88e8eSopenharmony_ci      ` ' value must not be a function. Change the value to an object.');
6388c88e8eSopenharmony_ci    }\n  });\n}`
6488c88e8eSopenharmony_ci        }
6588c88e8eSopenharmony_ci        let result = `module.exports = function(module, exports, $app_require$){${parsed}}`
6688c88e8eSopenharmony_ci        result += '\n/* generated by ace-loader */\n'
6788c88e8eSopenharmony_ci        callback(null, result, map)
6888c88e8eSopenharmony_ci      }
6988c88e8eSopenharmony_ci      if (process.env.DEVICE_LEVEL === DEVICE_LEVEL.LITE) {
7088c88e8eSopenharmony_ci        callback(null, parsed, map)
7188c88e8eSopenharmony_ci      }
7288c88e8eSopenharmony_ci    }).catch(e => {
7388c88e8eSopenharmony_ci      logWarn(this, [{
7488c88e8eSopenharmony_ci        reason: 'ERROR: Failed to parse the JS file. ' + e
7588c88e8eSopenharmony_ci      }])
7688c88e8eSopenharmony_ci      callback('')
7788c88e8eSopenharmony_ci    })
7888c88e8eSopenharmony_ci}