• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20import path from 'path'
21
22import {
23  logWarn,
24  parseRequireModule
25} from './util'
26import {
27  parseScript
28} from './parser'
29
30const { DEVICE_LEVEL } = require('./lite/lite-enum')
31
32module.exports = function (source, map) {
33  this.cacheable && this.cacheable()
34  const callback = this.async()
35  parseScript(source, this.resourcePath)
36    .then(({
37      parsed, log
38    }) => {
39      if (log && log.length) {
40        logWarn(this, log)
41      }
42      parsed = parseRequireModule(parsed, this.resourcePath);
43      if (process.env.DEVICE_LEVEL === DEVICE_LEVEL.RICH || process.env.DEVICE_LEVEL === 'card') {
44        const appName = process.env.abilityType === 'page' ? 'app.js' : `${process.env.abilityType}.js`
45        if (path.basename(this.resourcePath) !== appName) {
46          parsed += `\nvar moduleOwn = exports.default || module.exports;\n` +
47            `var accessors = ['public', 'protected', 'private'];
48if (moduleOwn.data && accessors.some(function (acc) {
49    return moduleOwn[acc];
50  })) {\n  throw new Error('For VM objects, attribute data must not coexist with public,` +
51  ` protected, or private. Please replace data with public.');
52} else if (!moduleOwn.data) {
53  moduleOwn.data = {};\n  moduleOwn._descriptor = {};\n  accessors.forEach(function(acc) {
54    var accType = typeof moduleOwn[acc];
55    if (accType === 'object') {
56      moduleOwn.data = Object.assign(moduleOwn.data, moduleOwn[acc]);
57      for (var name in moduleOwn[acc]) {
58        moduleOwn._descriptor[name] = {access : acc};
59      }
60    } else if (accType === 'function') {
61      console.warn('For VM objects, attribute ' + acc +` +
62      ` ' value must not be a function. Change the value to an object.');
63    }\n  });\n}`
64        }
65        let result = `module.exports = function(module, exports, $app_require$){${parsed}}`
66        result += '\n/* generated by ace-loader */\n'
67        callback(null, result, map)
68      }
69      if (process.env.DEVICE_LEVEL === DEVICE_LEVEL.LITE) {
70        callback(null, parsed, map)
71      }
72    }).catch(e => {
73      logWarn(this, [{
74        reason: 'ERROR: Failed to parse the JS file. ' + e
75      }])
76      callback('')
77    })
78}