magic-string
Advanced tools
+11
-8
@@ -378,7 +378,7 @@ import { encode } from "@jridgewell/sourcemap-codec"; | ||
| writable: true, | ||
| value: {} | ||
| value: /* @__PURE__ */ new Map([[0, chunk]]) | ||
| }, | ||
| byEnd: { | ||
| writable: true, | ||
| value: {} | ||
| value: /* @__PURE__ */ new Map([[string.length, chunk]]) | ||
| }, | ||
@@ -414,4 +414,2 @@ filename: { | ||
| }); | ||
| this.byStart = /* @__PURE__ */ new Map([[0, chunk]]); | ||
| this.byEnd = /* @__PURE__ */ new Map([[string.length, chunk]]); | ||
| } | ||
@@ -699,4 +697,6 @@ /** | ||
| } | ||
| if (start < 0) throw new Error("Character is out of bounds"); | ||
| if (end > this.original.length) throw new Error("end is out of bounds"); | ||
| if (start === end) throw new Error("Cannot overwrite a zero-length range – use appendLeft or prependRight instead"); | ||
| if (start > end) throw new Error(`end must be greater than start (start: ${start}, end: ${end})`); | ||
| this._split(start); | ||
@@ -784,3 +784,3 @@ this._split(end); | ||
| if (start < 0 || end > this.original.length) throw new Error("Character is out of bounds"); | ||
| if (start > end) throw new Error("end must be greater than start"); | ||
| if (start > end) throw new Error(`end must be greater than start (start: ${start}, end: ${end})`); | ||
| this._split(start); | ||
@@ -809,3 +809,3 @@ this._split(end); | ||
| if (start < 0 || end > this.original.length) throw new Error("Character is out of bounds"); | ||
| if (start > end) throw new Error("end must be greater than start"); | ||
| if (start > end) throw new Error(`end must be greater than start (start: ${start}, end: ${end})`); | ||
| this._split(start); | ||
@@ -1185,3 +1185,3 @@ this._split(end); | ||
| this.sources.forEach((source, i) => { | ||
| if (i > 0) mappings.advance(this.separator); | ||
| if (i > 0) mappings.advance(source.separator !== void 0 ? source.separator : this.separator); | ||
| const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1; | ||
@@ -1268,3 +1268,6 @@ const magicString = source.content; | ||
| length() { | ||
| return this.sources.reduce((length, source) => length + source.content.length(), this.intro.length); | ||
| return this.sources.reduce((length, source, i) => { | ||
| const separator = source.separator !== void 0 ? source.separator : this.separator; | ||
| return length + (i > 0 ? separator.length : 0) + source.content.length(); | ||
| }, this.intro.length); | ||
| } | ||
@@ -1271,0 +1274,0 @@ trimLines() { |
+1
-1
| { | ||
| "name": "magic-string", | ||
| "type": "module", | ||
| "version": "1.1.0", | ||
| "version": "1.1.1", | ||
| "description": "Modify strings, generate sourcemaps", | ||
@@ -6,0 +6,0 @@ "author": "Rich Harris", |
+85
-2
@@ -139,2 +139,6 @@ # magic-string | ||
| ### s.getIndentString() | ||
| Returns the indentation string guessed from the original content, falling back to a single tab character. This is the value `s.indent()` uses when no prefix is supplied. | ||
| ### s.hasChanged() | ||
@@ -162,9 +166,35 @@ | ||
| ### s.lastChar() | ||
| Returns the last character of the generated string, or `''` if it is empty. | ||
| ### s.lastLine() | ||
| Returns everything after the last newline in the generated string, or the whole string if there is no newline. | ||
| ### s.length() | ||
| Returns the length of the generated string, counting only content attached to the original characters. Content that sits outside the string body is not counted, so `s.length()` and `s.toString().length` can disagree: | ||
| ```js | ||
| const s = new MagicString('abcde') | ||
| s.appendLeft(2, '__') | ||
| s.toString() // 'ab__cde' | ||
| s.length() // 7 | ||
| s.prepend('>>') | ||
| s.toString() // '>>ab__cde' | ||
| s.length() // still 7 | ||
| ``` | ||
| `s.prepend()` and `s.append()` always land outside the body, as do inserts made before the first character or after the last one. | ||
| ### s.locate( index ) | ||
| **DEPRECATED** since 0.10 – see [#30](https://github.com/Rich-Harris/magic-string/pull/30) | ||
| **REMOVED** in 0.13, deprecated since 0.10 – see [#30](https://github.com/Rich-Harris/magic-string/pull/30) | ||
| ### s.locateOrigin( index ) | ||
| **DEPRECATED** since 0.10 – see [#30](https://github.com/Rich-Harris/magic-string/pull/30) | ||
| **REMOVED** in 0.13, deprecated since 0.10 – see [#30](https://github.com/Rich-Harris/magic-string/pull/30) | ||
@@ -327,4 +357,57 @@ ### s.move( start, end, index ) | ||
| ### new Bundle([ options ]) | ||
| The constructor takes an optional options object: | ||
| - `intro` (default `''`) is placed at the start of the bundle, before the first source. | ||
| - `separator` (default `'\n'`) is placed between sources. | ||
| ```js | ||
| const bundle = new Bundle({ | ||
| intro: '/* my-bundle.js */\n', | ||
| separator: '\n\n', | ||
| }) | ||
| bundle.addSource(new MagicString('var answer = 42;')) | ||
| bundle.addSource(new MagicString('console.log(answer);')) | ||
| bundle.toString() | ||
| // /* my-bundle.js */ | ||
| // var answer = 42; | ||
| // | ||
| // console.log(answer); | ||
| ``` | ||
| A source can override the bundle separator with its own `separator` property: | ||
| ```js | ||
| const bundle = new Bundle({ separator: '\n\n' }) | ||
| bundle.addSource(new MagicString('var answer = 42;')) | ||
| bundle.addSource({ | ||
| content: new MagicString('console.log(answer);'), | ||
| separator: '\n', | ||
| }) | ||
| bundle.toString() | ||
| // var answer = 42; | ||
| // console.log(answer); | ||
| ``` | ||
| `bundle.append( str[, options ])` takes the same options object but reads only `separator`, which defaults to `''` rather than to the bundle separator: | ||
| ```js | ||
| const bundle = new Bundle({ separator: '\n\n' }) | ||
| bundle.addSource(new MagicString('a')) | ||
| bundle.append('b') | ||
| bundle.toString() // 'ab' | ||
| bundle.append('c', { separator: '\n' }) | ||
| bundle.toString() // 'ab\nc' | ||
| ``` | ||
| ## License | ||
| MIT |
70604
3.85%1301
0.23%411
25.3%