skeleton-loader.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var loaderUtils = require('loader-utils');
  3. module.exports = function(content, sourceMap, meta) {
  4. var context = this,
  5. options = loaderUtils.getOptions(context) || {};
  6. function getResult(content) {
  7. return context.loaderIndex === 0 && options.toCode
  8. ? 'module.exports = ' + JSON.stringify(content) + ';' : content;
  9. }
  10. options.cacheable = typeof options.cacheable === 'boolean' ? options.cacheable : true;
  11. options.cacheable && context.cacheable && context.cacheable();
  12. if (typeof context.resourceQuery === 'string' && context.resourceQuery) {
  13. options.resourceOptions = loaderUtils.parseQuery(context.resourceQuery);
  14. }
  15. options.sourceMap = sourceMap;
  16. options.meta = meta;
  17. if (typeof options.procedure === 'function') {
  18. content = options.procedure.call(context, content, options,
  19. function(error, content, sourceMap, meta) {
  20. if (error) {
  21. context.callback(error);
  22. } else {
  23. context.callback(null, getResult(content), sourceMap, meta);
  24. }
  25. });
  26. if (options.procedure.length >= 3) { // async mode
  27. if (!context.async()) { throw new Error('Asynchronous mode is not allowed'); }
  28. return;
  29. }
  30. }
  31. return getResult(content);
  32. };