|
| 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 | + |
| 20 | +const path = require('path'); |
| 21 | + |
| 22 | +const { |
| 23 | + readContents, |
| 24 | + stripLicenseHeader, |
| 25 | + prependFileComment, |
| 26 | + values, |
| 27 | + pkgRoot, |
| 28 | + collectModules |
| 29 | +} = require('./common'); |
| 30 | + |
| 31 | +module.exports = function modules (config) { |
| 32 | + for (const m of values(config.extraModules)) { |
| 33 | + if (m.path.startsWith('../')) { |
| 34 | + m.path = path.resolve(m.path); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + const commonModules = collectCommonModules(); |
| 39 | + const modules = values(Object.assign(commonModules, config.extraModules)); |
| 40 | + modules.sort((a, b) => a.moduleId.localeCompare(b.moduleId)); |
| 41 | + return Promise.all(modules.map(modulePipeline(config))); |
| 42 | +}; |
| 43 | + |
| 44 | +function collectCommonModules () { |
| 45 | + const modules = collectModules(path.join(pkgRoot, 'src/common')); |
| 46 | + modules[''] = { |
| 47 | + moduleId: '', |
| 48 | + path: path.relative(pkgRoot, path.join(pkgRoot, 'src/cordova.js')) |
| 49 | + }; |
| 50 | + return modules; |
| 51 | +} |
| 52 | + |
| 53 | +function modulePipeline (config) { |
| 54 | + return f => Promise.resolve(f) |
| 55 | + .then(readContents) |
| 56 | + .then(config.preprocess) |
| 57 | + .then(stripLicenseHeader) |
| 58 | + .then(addModuleNamespace('cordova')) |
| 59 | + .then(wrapInModuleContext) |
| 60 | + .then(prependFileComment); |
| 61 | +} |
| 62 | + |
| 63 | +function addModuleNamespace (ns) { |
| 64 | + return m => { |
| 65 | + const moduleId = path.posix.join(ns, m.moduleId); |
| 66 | + return Object.assign({}, m, { moduleId }); |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +function wrapInModuleContext (f) { |
| 71 | + const contents = moduleTemplate({ id: f.moduleId, body: f.contents }); |
| 72 | + return Object.assign({}, f, { contents }); |
| 73 | +} |
| 74 | + |
| 75 | +const moduleTemplate = ({ id, body }) => ` |
| 76 | +define("${id}", function(require, exports, module) { |
| 77 | +
|
| 78 | +${body} |
| 79 | +}); |
| 80 | +`.trimLeft(); |
0 commit comments