index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loader;
  6. var _sourceMap = require("source-map");
  7. var _options = _interopRequireDefault(require("./options.json"));
  8. var _utils = require("./utils");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /*
  11. MIT License http://www.opensource.org/licenses/mit-license.php
  12. Author Tobias Koppers @sokra
  13. */
  14. const HEADER = "/*** IMPORTS FROM imports-loader ***/\n";
  15. function loader(content, sourceMap) {
  16. const options = this.getOptions(_options.default);
  17. const type = options.type || "module";
  18. const callback = this.async();
  19. let importsCode = HEADER;
  20. let imports;
  21. if (typeof options.imports !== "undefined") {
  22. try {
  23. imports = (0, _utils.getImports)(type, options.imports);
  24. } catch (error) {
  25. callback(error);
  26. return;
  27. } // We don't need to remove 'use strict' manually, because `terser` do it
  28. const directive = (0, _utils.sourceHasUseStrict)(content);
  29. importsCode += Object.entries(imports).reduce((accumulator, item) => `${accumulator}${(0, _utils.renderImports)(this, type, item[0], item[1])}\n`, directive ? "'use strict';\n" : "");
  30. }
  31. if (typeof options.additionalCode !== "undefined") {
  32. importsCode += `\n${options.additionalCode}\n`;
  33. }
  34. let codeAfterModule = "";
  35. if (typeof options.wrapper !== "undefined") {
  36. let thisArg;
  37. let args;
  38. let params;
  39. if (typeof options.wrapper === "boolean") {
  40. thisArg = "";
  41. params = "";
  42. args = "";
  43. } else if (typeof options.wrapper === "string") {
  44. thisArg = options.wrapper;
  45. params = "";
  46. args = "";
  47. } else {
  48. ({
  49. thisArg,
  50. args
  51. } = options.wrapper);
  52. if (Array.isArray(args)) {
  53. params = args.join(", ");
  54. args = params;
  55. } else {
  56. params = Object.keys(args).join(", ");
  57. args = Object.values(args).join(", ");
  58. }
  59. }
  60. importsCode += `\n(function(${params}) {`;
  61. codeAfterModule += `\n}.call(${thisArg}${args ? `, ${args}` : ""}));\n`;
  62. }
  63. if (this.sourceMap) {
  64. if (sourceMap) {
  65. const node = _sourceMap.SourceNode.fromStringWithSourceMap(content, new _sourceMap.SourceMapConsumer(sourceMap));
  66. node.prepend(`${importsCode}\n`);
  67. node.add(codeAfterModule);
  68. const result = node.toStringWithSourceMap({
  69. file: this.resourcePath
  70. });
  71. callback(null, result.code, result.map.toJSON());
  72. return;
  73. }
  74. const generator = new _sourceMap.SourceMapGenerator();
  75. generator.setSourceContent(this.resourcePath, content);
  76. generator.addMapping({
  77. generated: {
  78. line: importsCode.split("\n").length + 1,
  79. column: 0
  80. },
  81. original: {
  82. line: 1,
  83. column: 0
  84. },
  85. source: this.resourcePath
  86. });
  87. callback(null, `${importsCode}\n${content}\n${codeAfterModule}`, generator.toString());
  88. return;
  89. }
  90. callback(null, `${importsCode}\n${content}${codeAfterModule}`, sourceMap);
  91. }