UNPKG

1.62 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7const Source = require("./Source");
8const { SourceNode } = require("source-map");
9const { SourceListMap } = require("source-list-map");
10
11class RawSource extends Source {
12 constructor(value, convertToString = false) {
13 super();
14 const isBuffer = Buffer.isBuffer(value);
15 if (!isBuffer && typeof value !== "string") {
16 throw new TypeError("argument 'value' must be either string of Buffer");
17 }
18 this._valueIsBuffer = !convertToString && isBuffer;
19 this._value = convertToString && isBuffer ? undefined : value;
20 this._valueAsBuffer = isBuffer ? value : undefined;
21 }
22
23 isBuffer() {
24 return this._valueIsBuffer;
25 }
26
27 source() {
28 if (this._value === undefined) {
29 this._value = this._valueAsBuffer.toString("utf-8");
30 }
31 return this._value;
32 }
33
34 buffer() {
35 if (this._valueAsBuffer === undefined) {
36 this._valueAsBuffer = Buffer.from(this._value, "utf-8");
37 }
38 return this._valueAsBuffer;
39 }
40
41 map(options) {
42 return null;
43 }
44
45 node(options) {
46 if (this._value === undefined) {
47 this._value = this._valueAsBuffer.toString("utf-8");
48 }
49 return new SourceNode(null, null, null, this._value);
50 }
51
52 listMap(options) {
53 if (this._value === undefined) {
54 this._value = this._valueAsBuffer.toString("utf-8");
55 }
56 return new SourceListMap(this._value);
57 }
58
59 updateHash(hash) {
60 if (this._valueAsBuffer === undefined) {
61 this._valueAsBuffer = Buffer.from(this._value, "utf-8");
62 }
63 hash.update("RawSource");
64 hash.update(this._valueAsBuffer);
65 }
66}
67
68module.exports = RawSource;