Supposeyouhavesomesourcecode.Youwanttomakesomelightmodificationstoit-replacingafewcharactershereandthere,wrappingitwithaheaderandfooter,etc-andideallyyou'dliketogenerateasourcemapattheendofit.You'vethoughtaboutusingsomethinglike[recast](https://github.com/benjamn/recast) (which allows you to generate an AST from some JavaScript, manipulate it, and reprint it with a sourcemap without losing your comments and formatting), but it seems like overkill for your needs (or maybe the source code isn't JavaScript).
Appendsthespecified`content` at the `index` in the original string. If a range *ending* with `index` is subsequently moved, the insert will be moved with it. Returns `this`. See also `s.prependLeft(...)`.
###s.appendRight(index,content)
Appendsthespecified`content` at the `index` in the original string. If a range *starting* with `index` is subsequently moved, the insert will be moved with it. Returns `this`. See also `s.prependRight(...)`.
###s.clone()
Doeswhatyou'dexpect.
###s.generateDecodedMap(options)
Generatesasourcemapobjectwithrawmappingsinarrayform,ratherthanencodedasastring.See`generateMap` documentation below for options details. Useful if you need to manipulate the sourcemap further, but most of the time you will use `generateMap`instead.
###s.generateMap(options)
Generatesa[version3sourcemap](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit). All options are, well, optional:
*`includeContent` - whether to include the original content in the map's `sourcesContent`array
*`hires` - whether the mapping should be high-resolution. Hi-res mappings map every single character, meaning (for example) your devtools will always be able to pinpoint the exact location of function calls and so on. With lo-res mappings, devtools may only be able to identify the correct line - but they're quicker to generate and less bulky. If sourcemap locations have been specified with `s.addSourceMapLocation()`,theywillbeusedhere.
Prefixeseachlineofthestringwith`prefix`. If `prefix` is not supplied, the indentation will be guessed from the original content, falling back to a single tab character. Returns `this`.
The`options` argument can have an `exclude` property, which is an array of `[start, end]`characterranges.Theserangeswillbeexcludedfromtheindentation-usefulfor(e.g.)multilinestrings.
Movesthecharactersfrom`start` and `end` to `index`. Returns `this`.
###s.overwrite(start,end,content[,options])
Replacesthecharactersfrom`start` to `end` with `content`. The same restrictions as `s.remove()` apply. Returns `this`.
Thefourthargumentisoptional.Itcanhavea`storeName` property — if `true`, the original name will be stored for later inclusion in a sourcemap's `names` array — and a `contentOnly`propertywhichdetermineswhetheronlythecontentisoverwritten,oranythingthatwasappended/prependedtotherangeaswell.
Sameas`s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
###s.prependRight(index,content)
Sameas`s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
###s.remove(start,end)
Removesthecharactersfrom`start` to `end` (of the original string, **not** the generated string). Removing the same content twice, or making removals that partially overlap, will cause an error. Returns `this`.
###s.slice(start,end)
Returnsthecontentofthegeneratedstringthatcorrespondstotheslicebetween`start` and `end`oftheoriginalstring.Throwserroriftheindicesareforcharactersthatwerealreadyremoved.
###s.snip(start,end)
Returnsacloneof`s`, with all content before the `start` and `end`charactersoftheoriginalstringremoved.
###s.toString()
Returnsthegeneratedstring.
###s.trim([charType])
Trimscontentmatching`charType` (defaults to `\s`, i.e. whitespace) from the start and end. Returns `this`.
###s.trimStart([charType])
Trimscontentmatching`charType` (defaults to `\s`, i.e. whitespace) from the start. Returns `this`.
###s.trimEnd([charType])
Trimscontentmatching`charType` (defaults to `\s`, i.e. whitespace) from the end. Returns `this`.
// Advanced: a source can include an `indentExclusionRanges` property
// alongside `filename` and `content`. This will be passed to `s.indent()`
// - see documentation above
bundle.indent()// optionally, pass an indent string, otherwise it will be guessed
.prepend('(function(){\n')
.append('}());');
bundle.toString();
// (function () {
// var answer = 42;
// console.log( answer );
// }());
// options are as per `s.generateMap()` above
constmap=bundle.generateMap({
file:'bundle.js',
includeContent:true,
hires:true
});
```
Asanalternativesyntax,ifyoua)don'thave`filename` or `indentExclusionRanges` options, or b) passed those in when you used `new MagicString(...)`, you can simply pass the `MagicString`instanceitself: