You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var loaderUtils = require("loader-utils");
  6. var processCss = require("./processCss");
  7. var getImportPrefix = require("./getImportPrefix");
  8. var compileExports = require("./compile-exports");
  9. module.exports = function(content) {
  10. var callback = this.async();
  11. var query = loaderUtils.getOptions(this) || {};
  12. var moduleMode = query.modules;
  13. var camelCaseKeys = query.camelCase;
  14. processCss(content, null, {
  15. mode: moduleMode ? "local" : "global",
  16. query: query,
  17. loaderContext: this,
  18. }, function(err, result) {
  19. if(err) return callback(err);
  20. // for importing CSS
  21. var importUrlPrefix = getImportPrefix(this, query);
  22. function importItemMatcher(item) {
  23. var match = result.importItemRegExp.exec(item);
  24. var idx = +match[1];
  25. var importItem = result.importItems[idx];
  26. var importUrl = importUrlPrefix + importItem.url;
  27. return "\" + require(" + loaderUtils.stringifyRequest(this, importUrl) + ")" +
  28. "[" + JSON.stringify(importItem.export) + "] + \"";
  29. }
  30. var exportJs = compileExports(result, importItemMatcher.bind(this), camelCaseKeys);
  31. if (exportJs) {
  32. exportJs = "module.exports = " + exportJs + ";";
  33. }
  34. callback(null, exportJs);
  35. }.bind(this));
  36. };