JsonParser.js 758 B

12345678910111213141516171819202122232425262728
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const parseJson = require("json-parse-better-errors");
  7. const JsonExportsDependency = require("./dependencies/JsonExportsDependency");
  8. class JsonParser {
  9. constructor(options) {
  10. this.options = options;
  11. }
  12. parse(source, state) {
  13. const data = parseJson(source[0] === "\ufeff" ? source.slice(1) : source);
  14. state.module.buildInfo.jsonData = data;
  15. state.module.buildMeta.exportsType = "named";
  16. if (typeof data === "object" && data) {
  17. state.module.addDependency(new JsonExportsDependency(Object.keys(data)));
  18. }
  19. state.module.addDependency(new JsonExportsDependency(["default"]));
  20. return state;
  21. }
  22. }
  23. module.exports = JsonParser;