All files OpenUI5ResourceModule.js

76.14% Statements 67/88
62.5% Branches 20/32
85.19% Functions 23/27
77.11% Lines 64/83
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 1981x 1x 1x 1x 1x 1x       4x 4x 4x 4x 4x       16x               4x 4x         4x 5x 5x 5x 5x 5x 5x 5x     4x 4x   4x         4x 4x 4x 4x     4x   4x               4x 9x   5x     5x   9x 9x         4x 4x                                                                       4x 4x           4x     4x       4x                         4x 6x 6x       11x 11x   11x 11x                   11x 2x 9x 4x       4x 4x 4x   5x       6x         6x         11x       4x       1x  
const path = require('path');
const async = require('async');
const Module = require('webpack/lib/Module');
const ContextElementDependency = require('webpack/lib/dependencies/ContextElementDependency');
const OriginalSource = require('webpack-sources').OriginalSource;
const RawSource = require('webpack-sources').RawSource;
 
class OpenUI5ResourceModule extends Module {
  constructor(context, modulePath, extensions, resources) {
    super();
    this.context = context;
    this.modulePath = modulePath;
    this.extensions = extensions;
    this.resources = resources;
  }
 
  identifier() {
    return this.context;
  }
 
  readableIdentifier(requestShortener) {
    return `${requestShortener.shorten(this.context)} openui5 resources`;
  }
 
  build(options, compilation, resolver, fs, callback) {
    this.built = true;
    this.buildTime = Date.now();
 
    // this.dependencies = [];
    // this.blocks = [];
 
    this.dependencies = this.resources.map((resource) => {
      const userRequest = resource.indexOf('cldr') > -1 ? resource : `./${resource}`;
      const dep = new ContextElementDependency(resource, userRequest);
      dep.optional = true;
      dep.modulePath = this.modulePath;
      dep.loc = dep.userRequest;
      dep.weak = true;
      return dep;
    });
 
    this.collectDependencies(fs, this.context, this.libraries, this.extensions, (err, dependencies) => {
      Iif (err) return callback(err);
 
      Iif (!dependencies) {
        return callback();
      }
 
      // enhance dependencies with meta info
      dependencies.forEach((dep) => {
        dep.modulePath = this.modulePath;
        dep.loc = dep.userRequest;
        dep.weak = true;
      });
 
      this.dependencies = this.dependencies.concat(dependencies);
 
      return callback();
    });
  }
 
  getUserRequestMap(dependencies) {
    // if we filter first we get a new array
    // therefor we dont need to create a clone of dependencies explicitly
    // therefore the order of this is !important!
    return dependencies
      .filter(dependency => dependency.module)
      .sort((a, b) => {
        Iif (a.userRequest === b.userRequest) {
          return 0;
        }
        return a.userRequest < b.userRequest ? -1 : 1;
      }).reduce((map, dep) => {
        map[dep.userRequest] = dep.module.id;
        return map;
      }, Object.create(null));
  }
 
  getWeakSyncSource(dependencies, id) {
    const map = this.getUserRequestMap(dependencies);
    return `var map = ${JSON.stringify(map, null, '\t')};
function webpackContext(req) {
  var id = webpackContextResolve(req);
  if(!__webpack_require__.m[id])
    throw new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
  return __webpack_require__(id);
};
function webpackContextResolve(req) {
  var id = map[req];
  if(!(id + 1)) // check for number or string
    throw new Error("Cannot find module '" + req + "'.");
  return id;
};
webpackContext.keys = function webpackContextKeys() {
  return Object.keys(map);
};
webpackContext.resolve = webpackContextResolve;
webpackContext.id = ${JSON.stringify(id)};
module.exports = webpackContext;`;
  }
 
  getSourceForEmptyContext(id) {
    return `function webpackEmptyContext(req) {
  throw new Error("Cannot find module '" + req + "'.");
}
webpackEmptyContext.keys = function() { return []; };
webpackEmptyContext.resolve = webpackEmptyContext;
module.exports = webpackEmptyContext;
webpackEmptyContext.id = ${JSON.stringify(id)};`;
  }
 
  getSourceString(asyncMode, outputOptions, requestShortener) {
    // if(this.blocks && this.blocks.length > 0) {
    //   return this.getLazySource(this.blocks, this.id);
    // }
    // return this.getSourceForEmptyAsyncContext(this.id);
    Eif (this.dependencies && this.dependencies.length > 0) {
      return this.getWeakSyncSource(this.dependencies, this.id);
    }
    return this.getSourceForEmptyContext(this.id);
  }
 
  getSource(sourceString) {
    Iif (this.useSourceMap) {
      return new OriginalSource(sourceString, this.identifier());
    }
    return new RawSource(sourceString);
  }
 
  source(dependencyTemplates, outputOptions, requestShortener) {
    return this.getSource(this.getSourceString(outputOptions, requestShortener));
  }
 
  size() {
    // TODO update
    // base penalty
    const initialSize = 160;
 
    // if we dont have dependencies we stop here.
    return this.dependencies.reduce((size, dependency) => size + 5 + dependency.userRequest.length, initialSize);
  }
 
  collectDependencies(fs, context, libraries, extensions, callback) {
    const addDirectory = (directory, callback) => {
      fs.readdir(directory, (err, files) => {
        Iif (err) {
          callback(err);
          return;
        }
        async.map(files.filter(p => p.indexOf('.') !== 0), (segment, callback) => {
          const subResource = path.join(directory, segment);
 
          fs.stat(subResource, (err, stat) => {
            Iif (err) {
              if (err.code === 'ENOENT') {
                // ENOENT is ok here because the file may have been deleted between
                // the readdir and stat calls.
                callback();
                return;
              }
              callback(err);
              return;
            }
            if (stat.isDirectory()) {
              addDirectory(subResource, callback);
            } else if (stat.isFile() && extensions.includes(path.extname(subResource).substr(1))) {
              const obj = {
                context,
                request: `.${subResource.substr(context.length).replace(/\\/g, '/')}`,
              };
              const dep = new ContextElementDependency(obj.request);
              dep.optional = true;
              callback(null, dep);
            } else {
              callback();
            }
          });
        }, (err, result) => {
          Iif (err) {
            callback(err);
            return;
          }
 
          Iif (!result) {
            callback(null, []);
            return;
          }
 
          callback(null, result.filter(i => !!i).reduce((a, i) => a.concat(i), []));
        });
      });
    };
    addDirectory(context, callback);
  }
}
 
module.exports = OpenUI5ResourceModule;