intial
This commit is contained in:
7
node_modules/magic-string/LICENSE
generated
vendored
Normal file
7
node_modules/magic-string/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
Copyright 2018 Rich Harris
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
490
node_modules/magic-string/README.md
generated
vendored
Normal file
490
node_modules/magic-string/README.md
generated
vendored
Normal file
@@ -0,0 +1,490 @@
|
||||
# magic-string
|
||||
|
||||
<a href="https://npmjs.org/package/magic-string">
|
||||
<img src="https://img.shields.io/npm/v/magic-string.svg"
|
||||
alt="npm version">
|
||||
</a>
|
||||
<a href="https://github.com/Rich-Harris/magic-string/blob/master/LICENSE">
|
||||
<img src="https://img.shields.io/npm/l/magic-string.svg"
|
||||
alt="license">
|
||||
</a>
|
||||
|
||||
Suppose you have some source code. You want to make some light modifications to it - replacing a few characters here and there, wrapping it with a header and footer, etc - and ideally you'd like to generate a [source map](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/) at the end of it. You've thought about using something like [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).
|
||||
|
||||
Your requirements are, frankly, rather niche. But they're requirements that I also have, and for which I made magic-string. It's a small, fast utility for manipulating strings and generating sourcemaps.
|
||||
|
||||
## Installation
|
||||
|
||||
magic-string works in both node.js and browser environments. For node, install with npm:
|
||||
|
||||
```bash
|
||||
npm i magic-string
|
||||
```
|
||||
|
||||
magic-string is ESM-only. In browsers, import it from an ESM CDN:
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import MagicString from 'https://unpkg.com/magic-string/dist/index.mjs';
|
||||
</script>
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
These examples assume you're in node.js, or something similar:
|
||||
|
||||
```js
|
||||
import fs from 'node:fs'
|
||||
import MagicString from 'magic-string'
|
||||
|
||||
const s = new MagicString('problems = 99')
|
||||
|
||||
s.update(0, 8, 'answer')
|
||||
s.toString() // 'answer = 99'
|
||||
|
||||
s.update(11, 13, '42') // character indices always refer to the original string
|
||||
s.toString() // 'answer = 42'
|
||||
|
||||
s.prepend('var ').append(';') // most methods are chainable
|
||||
s.toString() // 'var answer = 42;'
|
||||
|
||||
const map = s.generateMap({
|
||||
source: 'source.js',
|
||||
file: 'converted.js.map',
|
||||
includeContent: true,
|
||||
}) // generates a v3 sourcemap
|
||||
|
||||
fs.writeFileSync('converted.js', s.toString())
|
||||
fs.writeFileSync('converted.js.map', map.toString())
|
||||
```
|
||||
|
||||
You can pass an options argument:
|
||||
|
||||
```js
|
||||
const s = new MagicString(someCode, {
|
||||
// these options will be used if you later call `bundle.addSource( s )` - see below
|
||||
filename: 'foo.js',
|
||||
indentExclusionRanges: [
|
||||
/* ... */
|
||||
],
|
||||
// mark source as ignore in DevTools, see below #Bundling
|
||||
ignoreList: false,
|
||||
// adjust the incoming position - see below
|
||||
offset: 0,
|
||||
})
|
||||
```
|
||||
|
||||
## Properties
|
||||
|
||||
### s.offset
|
||||
|
||||
Sets the offset property to adjust the incoming position for the following APIs: `slice`, `update`, `overwrite`, `appendLeft`, `prependLeft`, `appendRight`, `prependRight`, `move`, `reset`, and `remove`.
|
||||
|
||||
Example usage:
|
||||
|
||||
```ts
|
||||
const s = new MagicString('hello world', { offset: 0 })
|
||||
s.offset = 6
|
||||
s.slice() === 'world'
|
||||
```
|
||||
|
||||
Indices must be integers; a negative index counts from the end of the original string. `update`, `overwrite`, `remove`, `reset`, `move` and the `append*`/`prepend*` methods throw a `MagicStringError` for an index outside the original string, while `slice` clamps like `String.prototype.slice`. `addSourcemapLocation` and `indent`'s `exclude` ranges are not adjusted by `offset`.
|
||||
|
||||
## Hi-res mappings
|
||||
|
||||
The `hires` option (passed to `generateMap`/`generateDecodedMap`) controls how granular sourcemap mappings are:
|
||||
|
||||
- `false` (default) - lo-res mappings. Only one mapping per line, plus any locations added with `s.addSourcemapLocation()`. Quicker to generate and less bulky, but devtools may only be able to identify the correct line, not the exact column.
|
||||
- `true` - hi-res mappings. Every single character gets a mapping, so devtools can always pinpoint the exact location of function calls and so on.
|
||||
- `"boundary"` - semi-hi-res mappings, segmented per word boundary instead of per character. Suitable for string semantics that are separated by words.
|
||||
- `"experimental-range"` - hi-res mappings that use [range mappings](https://github.com/tc39/ecma426/blob/main/proposals/range-mappings.md), a source map extension that can map all positions in a range with fewer mappings than mapping every character individually. This requires support for range mappings in the source map consumer, and the feature is experimental.
|
||||
|
||||
## Methods
|
||||
|
||||
### s.addSourcemapLocation( index )
|
||||
|
||||
Adds the specified character index (with respect to the original string) to sourcemap mappings, if `hires` is `false` (see [Hi-res mappings](#hi-res-mappings)).
|
||||
|
||||
### s.append( content )
|
||||
|
||||
Appends the specified content to the end of the string. Returns `this`.
|
||||
|
||||
### s.appendLeft( index, content )
|
||||
|
||||
Appends the specified `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 )
|
||||
|
||||
Appends the specified `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()
|
||||
|
||||
Returns an independent copy, including edits, intro/outro, sourcemap locations, stored names, indent exclusion ranges, `offset`, `filename` and `ignoreList`; later edits to either string do not affect the other.
|
||||
|
||||
### s.generateDecodedMap( options )
|
||||
|
||||
Generates a sourcemap object with raw mappings in array form, rather than encoded as a string. 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 )
|
||||
|
||||
Generates a [version 3 sourcemap](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit). All options are, well, optional:
|
||||
|
||||
- `file` - the filename where you plan to write the sourcemap
|
||||
- `source` - the filename of the file containing the original source
|
||||
- `includeContent` - whether to include the original content in the map's `sourcesContent` array
|
||||
- `hires` - whether the mapping should be high-resolution. See [Hi-res mappings](#hi-res-mappings) for the available modes. If sourcemap locations have been specified with `s.addSourcemapLocation()`, they will be used here.
|
||||
|
||||
The returned sourcemap has two (non-enumerable) methods attached for convenience:
|
||||
|
||||
- `toString` - returns the equivalent of `JSON.stringify(map)`
|
||||
- `toUrl` - returns a DataURI containing the sourcemap. Useful for doing this sort of thing:
|
||||
|
||||
```js
|
||||
code += `\n//# sourceMappingURL=${map.toUrl()}`
|
||||
```
|
||||
|
||||
The returned object is an instance of the exported `SourceMap` class.
|
||||
|
||||
### 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()
|
||||
|
||||
Indicates if the string has been changed.
|
||||
|
||||
### s.indent( prefix[, options] )
|
||||
|
||||
Prefixes each line of the string with `prefix`. If `prefix` is not supplied, the indentation will be guessed from the original content, falling back to a single tab character. Returns `this`.
|
||||
|
||||
Lines are counted in the generated string, so content added with `append*`/`prepend*` is indented too, and a line break inside that content starts a new line just like a line break in the original:
|
||||
|
||||
```js
|
||||
const s = new MagicString('var a = 1;\nvar b = 2;')
|
||||
|
||||
s.prependRight(11, 'debugger;\n')
|
||||
s.toString() // 'var a = 1;\ndebugger;\nvar b = 2;'
|
||||
|
||||
s.indent(' ')
|
||||
s.toString() // ' var a = 1;\n debugger;\n var b = 2;'
|
||||
```
|
||||
|
||||
The `options` argument can have an `exclude` property, which is an array of `[start, end]` character ranges. These ranges will be excluded from the indentation - useful for (e.g.) multiline strings. It can also have an `indentStart` property (default `true`); when `false`, the first line is left unindented, which is what `Bundle` uses to join sources that do not start on a fresh line.
|
||||
|
||||
### s.isEmpty()
|
||||
|
||||
Returns true if the resulting source is empty (disregarding white space).
|
||||
|
||||
### 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.move( start, end, index[, affinity] )
|
||||
|
||||
Moves the characters from `start` and `end` to `index`. Returns `this`.
|
||||
|
||||
`affinity` (`'left'` or `'right'`, defaulting to `'right'`) controls where the range is anchored at `index`: `'right'` inserts it before the content that starts at `index`, `'left'` inserts it after the content that ends at `index`. The two differ only when other content has already been moved to that boundary, mirroring the `appendLeft`/`appendRight` distinction.
|
||||
|
||||
### s.overwrite( start, end, content[, options] )
|
||||
|
||||
Replaces the characters from `start` to `end` with `content`, along with the appended/prepended content in that range. The same restrictions as `s.remove()` apply. Returns `this`.
|
||||
|
||||
The fourth argument is optional. It can have a `storeName` property — if `true`, the original name will be stored for later inclusion in a sourcemap's `names` array — and a `contentOnly` property which determines whether only the content is overwritten, or anything that was appended/prepended to the range as well.
|
||||
|
||||
It may be preferred to use `s.update(...)` instead if you wish to avoid overwriting the appended/prepended content.
|
||||
|
||||
> [!NOTE]
|
||||
> `overwrite`/`update`/`remove` operate on ranges of the **original** string, so they'll throw (e.g. `cannot overwrite across a split point`, `cannot split a chunk that has already been edited`) when an edit would split a chunk that was previously replaced with non-empty content, or when a replacement crosses moved chunks. If you run into this, or want to chain multiple rounds of edits on a single instance with an auto-combined sourcemap, see [magic-string-stack](https://github.com/antfu/magic-string-stack).
|
||||
|
||||
### s.prepend( content )
|
||||
|
||||
Prepends the string with the specified content. Returns `this`.
|
||||
|
||||
### s.prependLeft ( index, content )
|
||||
|
||||
Same as `s.appendLeft(...)`, except that the inserted content will go _before_ any previous appends or prepends at `index`
|
||||
|
||||
### s.prependRight ( index, content )
|
||||
|
||||
Same as `s.appendRight(...)`, except that the inserted content will go _before_ any previous appends or prepends at `index`
|
||||
|
||||
### s.replace( regexpOrString, substitution )
|
||||
|
||||
String replacement with RegExp or string. The `substitution` parameter supports strings and functions. Returns `this`.
|
||||
|
||||
```ts
|
||||
import MagicString from 'magic-string'
|
||||
|
||||
const s = new MagicString(source)
|
||||
|
||||
s.replace('foo', 'bar')
|
||||
s.replace('foo', (str, index, s) => `${str}-${index}`)
|
||||
s.replace(/foo/g, 'bar')
|
||||
s.replace(/(\w)(\d+)/g, (_, $1, $2) => $1.toUpperCase() + $2)
|
||||
```
|
||||
|
||||
The differences from [`String.replace`](<(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)>):
|
||||
|
||||
- It will always match against the **original string**
|
||||
- It mutates the magic string state (use `.clone()` to be immutable)
|
||||
- A zero-length match spans no characters, so there is nothing to overwrite - the
|
||||
substitution is inserted at the matched position, as if by `s.appendRight(index, substitution)`
|
||||
|
||||
### s.replaceAll( regexpOrString, substitution )
|
||||
|
||||
Same as `s.replace`, but replace all matched strings instead of just one.
|
||||
If `regexpOrString` is a regex, then it must have the global (`g`) flag set, or a `MagicStringError` (`[MagicString] replaceAll() requires a global RegExp`) is thrown where the builtin would throw a `TypeError`. Otherwise matches the behavior of the builtin [`String.property.replaceAll`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll). Returns `this`.
|
||||
|
||||
### s.remove( start, end )
|
||||
|
||||
Removes the characters from `start` to `end` (of the original string, **not** the generated string). Content appended or prepended at positions strictly inside the range is removed along with it, while content attached at `start` or `end` is preserved — use `s.overwrite( start, end, '' )` to remove the range including its edge inserts. Repeated removals and removals with partially overlapping ranges are allowed. Returns `this`.
|
||||
|
||||
### s.reset( start, end )
|
||||
|
||||
Resets the characters from `start` to `end` (of the original string, **not** the generated string).
|
||||
It can be used to restore previously removed characters and discard unwanted changes.
|
||||
|
||||
### s.slice( start, end )
|
||||
|
||||
Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string. Throws error if the indices are for characters that were already removed. After `s.move(...)`, the slice follows the generated order: if the content at `end` now sits before the content at `start`, the result is `''`.
|
||||
|
||||
### s.snip( start, end )
|
||||
|
||||
Returns a clone of `s`, with all content before the `start` and `end` characters of the original string removed.
|
||||
|
||||
### s.toString()
|
||||
|
||||
Returns the generated string.
|
||||
|
||||
### s.trim([ charType ])
|
||||
|
||||
Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end. Returns `this`.
|
||||
|
||||
### s.trimStart([ charType ])
|
||||
|
||||
Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start. Returns `this`.
|
||||
|
||||
### s.trimEnd([ charType ])
|
||||
|
||||
Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end. Returns `this`.
|
||||
|
||||
### s.trimLines()
|
||||
|
||||
Removes empty lines from the start and end. Returns `this`.
|
||||
|
||||
### s.update( start, end, content[, options] )
|
||||
|
||||
Replaces the characters from `start` to `end` with `content`. The same restrictions as `s.remove()` apply. Returns `this`.
|
||||
|
||||
The fourth argument is optional. It can have a `storeName` property — if `true`, the original name will be stored for later inclusion in a sourcemap's `names` array — and an `overwrite` property which defaults to `false` and determines whether anything that was appended/prepended to the range will be overwritten along with the original content.
|
||||
|
||||
Without `overwrite`, all of that content is kept, no matter how earlier edits have split the range: content attached at `start` stays in front of the new content, and the rest follows it in its original order.
|
||||
|
||||
`s.update(start, end, content)` is equivalent to `s.overwrite(start, end, content, { contentOnly: true })`.
|
||||
|
||||
## Bundling
|
||||
|
||||
To concatenate several sources, use `Bundle`:
|
||||
|
||||
```js
|
||||
import MagicString, { Bundle } from 'magic-string'
|
||||
|
||||
const bundle = new Bundle()
|
||||
|
||||
bundle.addSource({
|
||||
filename: 'foo.js',
|
||||
content: new MagicString('var answer = 42;'),
|
||||
})
|
||||
|
||||
bundle.addSource({
|
||||
filename: 'bar.js',
|
||||
content: new MagicString('console.log( answer )'),
|
||||
})
|
||||
|
||||
// Sources can be marked as ignore-listed, which provides a hint to debuggers
|
||||
// to not step into this code and also don't show the source files depending
|
||||
// on user preferences.
|
||||
bundle.addSource({
|
||||
filename: 'some-3rdparty-library.js',
|
||||
content: new MagicString('function myLib(){}'),
|
||||
ignoreList: false, // <--
|
||||
})
|
||||
|
||||
// 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
|
||||
const map = bundle.generateMap({
|
||||
file: 'bundle.js',
|
||||
includeContent: true,
|
||||
hires: true,
|
||||
})
|
||||
```
|
||||
|
||||
`includeContent` can also be a function that receives `{ filename, content }` for each source and returns whether to include it, allowing per-source control:
|
||||
|
||||
```js
|
||||
bundle.generateMap({
|
||||
includeContent: source => !source.filename.startsWith('http'),
|
||||
})
|
||||
```
|
||||
|
||||
As an alternative syntax, if you a) don't have `filename` or `indentExclusionRanges` options, or b) passed those in when you used `new MagicString(...)`, you can simply pass the `MagicString` instance itself:
|
||||
|
||||
```js
|
||||
const bundle = new Bundle()
|
||||
const source = new MagicString(someCode, {
|
||||
filename: 'foo.js',
|
||||
})
|
||||
|
||||
bundle.addSource(source)
|
||||
```
|
||||
|
||||
### 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'
|
||||
```
|
||||
|
||||
### bundle.toMagicString()
|
||||
|
||||
Flattens the bundle into a single `MagicString`, so the concatenated result can be processed further with the full `MagicString` API:
|
||||
|
||||
```js
|
||||
const bundle = new Bundle()
|
||||
bundle.addSource(new MagicString('var answer = 42;'))
|
||||
bundle.addSource(new MagicString('console.log(answer);'))
|
||||
|
||||
const s = bundle.toMagicString()
|
||||
s.toString() // 'var answer = 42;\nconsole.log(answer);'
|
||||
|
||||
// now use any MagicString method on the combined result
|
||||
s.prepend('(function () {\n').append('\n}());')
|
||||
```
|
||||
|
||||
The returned string's `original` is the concatenation of every source's `original`, and all existing edits, inserts, intros, outros and separators are preserved, so `s.toString()` equals `bundle.toString()` and `s.generateMap()` maps back to that combined original. Because a `MagicString` maps to a single source, per-source `filename`s are not carried over - call `bundle.generateMap()` before flattening if you need the multi-source mapping.
|
||||
|
||||
### bundle.clone()
|
||||
|
||||
Returns an independent copy of the bundle: every source's `MagicString` is cloned, so later edits to either bundle do not affect the other.
|
||||
|
||||
### bundle.generateDecodedMap( options )
|
||||
|
||||
Generates a sourcemap object with raw mappings in array form, rather than encoded as a string. Takes the same options as `bundle.generateMap()`.
|
||||
|
||||
### bundle.getIndentString()
|
||||
|
||||
Returns the most common indent string among the sources, falling back to a tab.
|
||||
|
||||
### bundle.isEmpty()
|
||||
|
||||
Returns true if the bundle's output is empty (disregarding white space).
|
||||
|
||||
### bundle.length()
|
||||
|
||||
Returns the length of the bundle's output; it equals `bundle.toString().length`, unlike `s.length()`.
|
||||
|
||||
### bundle.trim([ charType ])
|
||||
|
||||
Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end. Returns `this`.
|
||||
|
||||
### bundle.trimStart([ charType ])
|
||||
|
||||
Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start. Returns `this`.
|
||||
|
||||
### bundle.trimEnd([ charType ])
|
||||
|
||||
Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end. Returns `this`.
|
||||
|
||||
### bundle.trimLines()
|
||||
|
||||
Removes empty lines from the start and end. Returns `this`.
|
||||
|
||||
## Errors
|
||||
|
||||
Every error thrown by magic-string is a `MagicStringError` (exported), a subclass of `Error` whose message starts with `[MagicString]`.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
399
node_modules/magic-string/dist/index.d.mts
generated
vendored
Normal file
399
node_modules/magic-string/dist/index.d.mts
generated
vendored
Normal file
@@ -0,0 +1,399 @@
|
||||
//#region src/SourceMap.d.ts
|
||||
interface SourceMapOptions {
|
||||
/**
|
||||
* Whether the mapping should be high-resolution:
|
||||
* - `false` (default) - lo-res mappings. Only one mapping per line, plus any locations added
|
||||
* with `s.addSourcemapLocation()`. Quicker to generate and less bulky, but devtools may only
|
||||
* be able to identify the correct line, not the exact column.
|
||||
* - `true` - hi-res mappings. Every single character gets a mapping, so devtools can always
|
||||
* pinpoint the exact location of function calls and so on.
|
||||
* - `"boundary"` - semi-hi-res mappings, segmented per word boundary instead of per character.
|
||||
* Suitable for string semantics that are separated by words.
|
||||
* - `"experimental-range"` - hi-res mappings that use range mappings
|
||||
* (https://github.com/tc39/ecma426/blob/main/proposals/range-mappings.md), a source map
|
||||
* extension that can map all positions in a range with fewer mappings than mapping every
|
||||
* character individually. This requires support for range mappings in the source map
|
||||
* consumer, and the feature is experimental.
|
||||
*
|
||||
* If sourcemap locations have been specified with s.addSourceMapLocation(), they will be used here.
|
||||
*/
|
||||
hires?: boolean | "boundary" | "experimental-range";
|
||||
/**
|
||||
* The filename where you plan to write the sourcemap.
|
||||
*/
|
||||
file?: string;
|
||||
/**
|
||||
* The filename of the file containing the original source.
|
||||
*/
|
||||
source?: string;
|
||||
/**
|
||||
* Whether to include the original content in the map's sourcesContent array.
|
||||
*/
|
||||
includeContent?: boolean;
|
||||
}
|
||||
type SourceMapSegment = [number] | [number, number, number, number] | [number, number, number, number, number];
|
||||
type SourceMapRangeMappings = number[][];
|
||||
interface DecodedSourceMap {
|
||||
file?: string;
|
||||
sources: string[];
|
||||
sourcesContent?: Array<string | null>;
|
||||
names: string[];
|
||||
mappings: SourceMapSegment[][];
|
||||
x_google_ignoreList?: number[];
|
||||
debugId?: string;
|
||||
rangeMappings?: SourceMapRangeMappings;
|
||||
}
|
||||
export declare class SourceMap {
|
||||
version: number;
|
||||
file: string | undefined;
|
||||
sources: string[];
|
||||
sourcesContent: Array<string | null> | undefined;
|
||||
names: string[];
|
||||
mappings: string;
|
||||
x_google_ignoreList: number[] | undefined;
|
||||
debugId: string | undefined;
|
||||
rangeMappings: string | undefined;
|
||||
constructor(properties: DecodedSourceMap | (Omit<DecodedSourceMap, "mappings"> & {
|
||||
mappings: string;
|
||||
}));
|
||||
/**
|
||||
* Returns the equivalent of `JSON.stringify(map)`
|
||||
*/
|
||||
toString(): string;
|
||||
/**
|
||||
* Returns a DataURI containing the sourcemap. Useful for doing this sort of thing:
|
||||
* `generateMap(options?: SourceMapOptions): SourceMap;`
|
||||
*/
|
||||
toUrl(): string;
|
||||
}
|
||||
//#endregion
|
||||
//#region src/MagicString.d.ts
|
||||
type ExclusionRange = [number, number];
|
||||
interface MagicStringOptions {
|
||||
filename?: string;
|
||||
ignoreList?: boolean;
|
||||
indentExclusionRanges?: ExclusionRange | ExclusionRange[];
|
||||
offset?: number;
|
||||
}
|
||||
interface IndentOptions {
|
||||
exclude?: ExclusionRange | ExclusionRange[];
|
||||
indentStart?: boolean;
|
||||
}
|
||||
interface OverwriteOptions {
|
||||
storeName?: boolean;
|
||||
contentOnly?: boolean;
|
||||
}
|
||||
interface UpdateOptions {
|
||||
storeName?: boolean;
|
||||
overwrite?: boolean;
|
||||
}
|
||||
type ReplacementFunction = (substring: string, ...args: any[]) => string;
|
||||
export declare class MagicString {
|
||||
readonly original: string;
|
||||
offset: number;
|
||||
/**
|
||||
* Wraps `string` for editing; every index passed to the editing methods refers to this original string.
|
||||
*/
|
||||
constructor(string: string, options?: MagicStringOptions);
|
||||
/**
|
||||
* Adds the specified character index (with respect to the original string) to sourcemap mappings, if `hires` is false.
|
||||
*/
|
||||
addSourcemapLocation(char: number): void;
|
||||
/**
|
||||
* Appends the specified content to the end of the string.
|
||||
*/
|
||||
append(content: string): this;
|
||||
/**
|
||||
* Appends the specified content at the index in the original string.
|
||||
* If a range *ending* with index is subsequently moved, the insert will be moved with it.
|
||||
* See also `s.prependLeft(...)`.
|
||||
*/
|
||||
appendLeft(index: number, content: string): this;
|
||||
/**
|
||||
* Appends the specified content at the index in the original string.
|
||||
* If a range *starting* with index is subsequently moved, the insert will be moved with it.
|
||||
* See also `s.prependRight(...)`.
|
||||
*/
|
||||
appendRight(index: number, content: string): this;
|
||||
/**
|
||||
* Returns an independent copy: the chunk list, string-level intro/outro,
|
||||
* sourcemap locations, stored names, indent exclusion ranges, `offset`,
|
||||
* `filename` and `ignoreList` are all copied; later edits to either string
|
||||
* do not affect the other.
|
||||
*/
|
||||
clone(): this;
|
||||
/**
|
||||
* Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
|
||||
* Useful if you need to manipulate the sourcemap further, but most of the time you will use `generateMap` instead.
|
||||
*/
|
||||
generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap;
|
||||
/**
|
||||
* Generates a version 3 sourcemap.
|
||||
*/
|
||||
generateMap(options?: SourceMapOptions): SourceMap;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
getIndentString(): string;
|
||||
/**
|
||||
* Prefixes each line of the string with prefix.
|
||||
* If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
|
||||
*/
|
||||
indent(options?: IndentOptions): this;
|
||||
/**
|
||||
* Prefixes each line of the string with prefix.
|
||||
* If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
|
||||
*
|
||||
* The options argument can have an exclude property, which is an array of [start, end] character ranges.
|
||||
* These ranges will be excluded from the indentation - useful for (e.g.) multiline strings.
|
||||
*/
|
||||
indent(indentStr?: string, options?: IndentOptions): this;
|
||||
/**
|
||||
* Moves the characters from `start` and `end` to `index`.
|
||||
*
|
||||
* `affinity` controls where the range is anchored at `index`. With the
|
||||
* default `'right'`, it is inserted before the content that starts at `index`;
|
||||
* with `'left'`, it is inserted after the content that ends at `index`. The
|
||||
* two differ only when other content has already been moved to that boundary,
|
||||
* mirroring the `appendLeft`/`appendRight` distinction.
|
||||
*/
|
||||
move(start: number, end: number, index: number, affinity?: "left" | "right"): this;
|
||||
/**
|
||||
* Replaces the characters from `start` to `end` with `content`, along with the appended/prepended content in
|
||||
* that range. The same restrictions as `s.remove()` apply.
|
||||
*
|
||||
* The fourth argument is optional. It can have a storeName property - if true, the original name will be stored
|
||||
* for later inclusion in a sourcemap's names array - and a contentOnly property which determines whether only
|
||||
* the content is overwritten, or anything that was appended/prepended to the range as well.
|
||||
*
|
||||
* It may be preferred to use `s.update(...)` instead if you wish to avoid overwriting the appended/prepended content.
|
||||
*/
|
||||
overwrite(start: number, end: number, content: string, options?: boolean | OverwriteOptions): this;
|
||||
/**
|
||||
* Replaces the characters from `start` to `end` with `content`. The same restrictions as `s.remove()` apply.
|
||||
*
|
||||
* The fourth argument is optional. It can have a storeName property - if true, the original name will be stored
|
||||
* for later inclusion in a sourcemap's names array - and an overwrite property which determines whether only
|
||||
* the content is overwritten, or anything that was appended/prepended to the range as well.
|
||||
*/
|
||||
update(start: number, end: number, content: string, options?: boolean | UpdateOptions): this;
|
||||
/**
|
||||
* Prepends the string with the specified content.
|
||||
*/
|
||||
prepend(content: string): this;
|
||||
/**
|
||||
* Same as `s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at index
|
||||
*/
|
||||
prependLeft(index: number, content: string): this;
|
||||
/**
|
||||
* Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
|
||||
*/
|
||||
prependRight(index: number, content: string): this;
|
||||
/**
|
||||
* Removes the characters from `start` to `end` (of the original string, **not** the generated string).
|
||||
* Content appended or prepended at positions strictly inside the range is removed with it, while
|
||||
* content attached at `start` or `end` is preserved — use `s.overwrite(start, end, '')` to remove
|
||||
* the range including its edge inserts.
|
||||
* Repeated removals and removals with partially overlapping ranges are allowed.
|
||||
*/
|
||||
remove(start: number, end: number): this;
|
||||
/**
|
||||
* Reset the modified characters from `start` to `end` (of the original string, **not** the generated string).
|
||||
*/
|
||||
reset(start: number, end: number): this;
|
||||
/**
|
||||
* Returns the last character of the generated string, or `''` if it is empty.
|
||||
*/
|
||||
lastChar(): string;
|
||||
/**
|
||||
* Returns everything after the last newline in the generated string, or the whole string if there is no newline.
|
||||
*/
|
||||
lastLine(): string;
|
||||
/**
|
||||
* Returns the content of the generated string that corresponds to the slice
|
||||
* between `start` and `end` of the original string, walking the generated
|
||||
* string from the chunk holding `start` to the chunk holding `end`. Throws
|
||||
* if either index falls inside replaced content. After `move()`, a range
|
||||
* whose end now comes before its start in the output yields `''`.
|
||||
*/
|
||||
slice(start?: number, end?: number): string;
|
||||
/**
|
||||
* Returns a clone of `s`, with all content before the `start` and `end` characters of the original string removed.
|
||||
*/
|
||||
snip(start: number, end: number): this;
|
||||
/**
|
||||
* Returns the generated string.
|
||||
*/
|
||||
toString(): string;
|
||||
/**
|
||||
* Returns true if the resulting source is empty (disregarding white space).
|
||||
*/
|
||||
isEmpty(): boolean;
|
||||
/**
|
||||
* Returns the length of the generated string, counting only content attached to the original characters.
|
||||
* Content outside the string body (`s.prepend()`/`s.append()`, and inserts before the first or after the last
|
||||
* character) is not counted, so this can differ from `s.toString().length`.
|
||||
*/
|
||||
length(): number;
|
||||
/**
|
||||
* Removes empty lines from the start and end.
|
||||
*/
|
||||
trimLines(): this;
|
||||
/**
|
||||
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
|
||||
*/
|
||||
trim(charType?: string): this;
|
||||
/**
|
||||
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
|
||||
*/
|
||||
trimEnd(charType?: string): this;
|
||||
/**
|
||||
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
|
||||
*/
|
||||
trimStart(charType?: string): this;
|
||||
/**
|
||||
* Indicates if the string has been changed.
|
||||
*/
|
||||
hasChanged(): boolean;
|
||||
/**
|
||||
* String replacement with RegExp or string.
|
||||
*/
|
||||
replace(searchValue: string | RegExp, replacement: string | ReplacementFunction): this;
|
||||
/**
|
||||
* Same as `s.replace`, but replace all matched strings instead of just one.
|
||||
*/
|
||||
replaceAll(searchValue: string | RegExp, replacement: string | ReplacementFunction): this;
|
||||
}
|
||||
//#endregion
|
||||
//#region src/Bundle.d.ts
|
||||
interface BundleOptions {
|
||||
intro?: string;
|
||||
separator?: string;
|
||||
}
|
||||
interface BundleSourceDescription {
|
||||
filename?: string;
|
||||
content: MagicString;
|
||||
ignoreList?: boolean;
|
||||
indentExclusionRanges?: ExclusionRange | ExclusionRange[];
|
||||
separator?: string;
|
||||
}
|
||||
interface BundledSourceFileRecord {
|
||||
filename: string;
|
||||
content: string;
|
||||
}
|
||||
interface BundleSourceMapOptions extends Omit<SourceMapOptions, "includeContent"> {
|
||||
/**
|
||||
* Whether to include the original content of each source in the map's `sourcesContent` array.
|
||||
* Can also be a function that receives the source's `filename` and `content` and returns
|
||||
* whether to include it, allowing per-source control (for example, omitting content for
|
||||
* sources that are otherwise loadable at runtime, such as public http(s) urls).
|
||||
*/
|
||||
includeContent?: boolean | ((source: BundledSourceFileRecord) => boolean);
|
||||
}
|
||||
interface DecodedSourceMapOrMissingContent extends Omit<DecodedSourceMap, "sourcesContent"> {
|
||||
sourcesContent: Array<string | null>;
|
||||
}
|
||||
export declare class Bundle {
|
||||
indentExclusionRanges: ExclusionRange | ExclusionRange[] | undefined;
|
||||
constructor(options?: BundleOptions);
|
||||
/**
|
||||
* Adds the specified source to the bundle, which can either be a `MagicString` object directly,
|
||||
* or an options object that holds a magic string `content` property and optionally provides
|
||||
* a `filename` for the source within the bundle, as well as an optional `ignoreList` hint
|
||||
* (which defaults to `false`). The `filename` is used when constructing the source map for the
|
||||
* bundle, to identify this `source` in the source map's `sources` field. The `ignoreList` hint
|
||||
* is used to populate the `x_google_ignoreList` extension field in the source map, which is a
|
||||
* mechanism for tools to signal to debuggers that certain sources should be ignored by default
|
||||
* (depending on user preferences).
|
||||
*/
|
||||
addSource(source: MagicString | BundleSourceDescription): this;
|
||||
/**
|
||||
* Appends `str` as a new source without a filename. Only `options.separator` is read, and it defaults to `''`
|
||||
* rather than to the bundle separator.
|
||||
*/
|
||||
append(str: string, options?: BundleOptions): this;
|
||||
/**
|
||||
* Returns an independent copy of the bundle: every source's `MagicString` is cloned, so later edits to either
|
||||
* bundle do not affect the other.
|
||||
*/
|
||||
clone(): this;
|
||||
/**
|
||||
* Flattens the bundle into a single `MagicString`, so the concatenated result can be
|
||||
* processed further with the full `MagicString` API. The returned string's `original`
|
||||
* is the concatenation of every source's `original`, and all existing edits, inserts,
|
||||
* intros, outros and separators are preserved as inserted content, so its `toString()`
|
||||
* equals the bundle's `toString()` and its sourcemap maps back to that combined original.
|
||||
*
|
||||
* Because a `MagicString` maps to a single source, per-source `filename`s are not carried
|
||||
* over; generate the bundle's map before flattening if you need the multi-source mapping.
|
||||
*/
|
||||
toMagicString(): MagicString;
|
||||
/**
|
||||
* Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
|
||||
* Takes the same options as `generateMap`.
|
||||
*/
|
||||
generateDecodedMap(options?: BundleSourceMapOptions): DecodedSourceMapOrMissingContent;
|
||||
/**
|
||||
* Generates a version 3 sourcemap covering every source that has a `filename`.
|
||||
* Options are as per `MagicString#generateMap`, except that `includeContent` can also be a per-source function.
|
||||
*/
|
||||
generateMap(options?: BundleSourceMapOptions): Omit<SourceMap, "sourcesContent"> & {
|
||||
sourcesContent: Array<string | null>;
|
||||
};
|
||||
/**
|
||||
* Returns the most common indent string among the sources, falling back to a tab.
|
||||
*/
|
||||
getIndentString(): string;
|
||||
/**
|
||||
* Prefixes each line of every source and of the intro with `indentStr`, guessed with `getIndentString()`
|
||||
* when not supplied. Each source's `indentExclusionRanges` are left unindented.
|
||||
*/
|
||||
indent(indentStr?: string): this;
|
||||
/**
|
||||
* Prepends `str` to the bundle's intro.
|
||||
*/
|
||||
prepend(str: string): this;
|
||||
/**
|
||||
* Returns the intro followed by every source's generated string, joined by their separators.
|
||||
*/
|
||||
toString(): string;
|
||||
/**
|
||||
* Returns true if the bundle's output is empty (disregarding white space).
|
||||
*/
|
||||
isEmpty(): boolean;
|
||||
/**
|
||||
* Returns the length of the bundle's output; unlike `MagicString#length`, this equals `bundle.toString().length`.
|
||||
*/
|
||||
length(): number;
|
||||
/**
|
||||
* Removes empty lines from the start and end.
|
||||
*/
|
||||
trimLines(): this;
|
||||
/**
|
||||
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
|
||||
*/
|
||||
trim(charType?: string): this;
|
||||
/**
|
||||
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
|
||||
*/
|
||||
trimStart(charType?: string): this;
|
||||
/**
|
||||
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
|
||||
*/
|
||||
trimEnd(charType?: string): this;
|
||||
}
|
||||
//#endregion
|
||||
//#region src/MagicStringError.d.ts
|
||||
/**
|
||||
* The single error type thrown by MagicString.
|
||||
*
|
||||
* Every message is prefixed with `[MagicString]` so its source is obvious at a
|
||||
* glance, and is kept short and consistent in tone.
|
||||
*/
|
||||
export declare class MagicStringError extends Error {
|
||||
override name: string;
|
||||
constructor(message: string, options?: ErrorOptions);
|
||||
}
|
||||
//#endregion
|
||||
export { type BundleOptions, type BundleSourceDescription, type BundleSourceMapOptions, type BundledSourceFileRecord, type DecodedSourceMap, type DecodedSourceMapOrMissingContent, type ExclusionRange, type IndentOptions, MagicString as default, type MagicStringOptions, type OverwriteOptions, type ReplacementFunction, type SourceMapOptions, type SourceMapRangeMappings, type SourceMapSegment, type UpdateOptions };
|
||||
1867
node_modules/magic-string/dist/index.mjs
generated
vendored
Normal file
1867
node_modules/magic-string/dist/index.mjs
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
60
node_modules/magic-string/package.json
generated
vendored
Normal file
60
node_modules/magic-string/package.json
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"name": "magic-string",
|
||||
"type": "module",
|
||||
"version": "1.4.5",
|
||||
"description": "Modify strings, generate sourcemaps",
|
||||
"author": "Rich Harris",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/Rich-Harris/magic-string.git"
|
||||
},
|
||||
"keywords": [
|
||||
"string",
|
||||
"string manipulation",
|
||||
"sourcemap",
|
||||
"templating",
|
||||
"transpilation"
|
||||
],
|
||||
"sideEffects": false,
|
||||
"exports": {
|
||||
".": "./dist/index.mjs",
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"types": "./dist/index.d.mts",
|
||||
"files": [
|
||||
"README.md",
|
||||
"dist/*"
|
||||
],
|
||||
"dependencies": {
|
||||
"@jridgewell/sourcemap-codec": "^1.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^9.5.1",
|
||||
"@types/node": "^26.5.1",
|
||||
"@vitest/coverage-v8": "^5.0.0",
|
||||
"bumpp": "^12.3.0",
|
||||
"conventional-changelog": "^8.1.3",
|
||||
"conventional-changelog-angular": "^9.4.0",
|
||||
"eslint": "^10.10.0",
|
||||
"publint": "^0.3.24",
|
||||
"source-map-js": "^1.2.1",
|
||||
"source-map-support": "^0.5.21",
|
||||
"tsdown": "^0.23.0",
|
||||
"typescript": "~6.0.3",
|
||||
"vitest": "^5.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsdown",
|
||||
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
|
||||
"lint": "eslint",
|
||||
"lint:fix": "eslint --fix",
|
||||
"release": "bumpp -x \"pnpm run changelog\" --all",
|
||||
"pretest": "pnpm run build",
|
||||
"test": "vitest run",
|
||||
"test:dev": "vitest",
|
||||
"typecheck": "tsc && tsc -p tsconfig.test.json",
|
||||
"bench": "pnpm run build && vitest bench --run --reporter=verbose",
|
||||
"watch": "tsdown -w"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user