blob: 325c5865de28ee74681e65226eb04fdfd027b5f5 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import {
parseScript
} from './parser'
module.exports = function (source, map) {
this.cacheable && this.cacheable()
const callback = this.async()
parseScript(source)
.then(({
parsed
}) => {
let requireList = parsed.match(/require\([^()]+?\)/g)
if (requireList && requireList.length > 0) {
requireList = requireList.filter(str => {
if (str.indexOf('@weex-module') > 0) {
parsed = parsed.replace(str, str.replace('require', '__weex_require__'))
return true
}
return false
}).join('\n')
}
let result = `module.exports = function(module, exports, __weex_require__){${parsed}}`
result += '\n/* generated by weex-loader */\n'
callback(null, result, map)
}).catch(e => {
callback(e, '')
})
}