You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(It also works with various module systems, if you prefer that sort of thing - it has a dependency on [vlq](https://github.com/Rich-Harris/vlq).)
35
-
36
36
## Usage
37
37
38
38
These examples assume you're in node.js, or something similar:
39
39
40
40
```js
41
-
importMagicStringfrom'magic-string';
42
-
importfsfrom'fs';
41
+
importfsfrom'node:fs'
42
+
importMagicStringfrom'magic-string'
43
43
44
-
consts=newMagicString('problems = 99');
44
+
consts=newMagicString('problems = 99')
45
45
46
-
s.update(0, 8, 'answer');
47
-
s.toString();// 'answer = 99'
46
+
s.update(0, 8, 'answer')
47
+
s.toString() // 'answer = 99'
48
48
49
-
s.update(11, 13, '42');// character indices always refer to the original string
50
-
s.toString();// 'answer = 42'
49
+
s.update(11, 13, '42') // character indices always refer to the original string
50
+
s.toString() // 'answer = 42'
51
51
52
-
s.prepend('var ').append(';');// most methods are chainable
53
-
s.toString();// 'var answer = 42;'
52
+
s.prepend('var ').append(';') // most methods are chainable
The differences from [`String.replace`](<(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)>):
@@ -262,62 +262,64 @@ The fourth argument is optional. It can have a `storeName` property — if `true
262
262
263
263
## Bundling
264
264
265
-
To concatenate several sources, use `MagicString.Bundle`:
265
+
To concatenate several sources, use `Bundle`:
266
266
267
267
```js
268
-
constbundle=newMagicString.Bundle();
268
+
importMagicString, { Bundle } from'magic-string'
269
+
270
+
constbundle=newBundle()
269
271
270
272
bundle.addSource({
271
-
filename:'foo.js',
272
-
content:newMagicString('var answer = 42;'),
273
-
});
273
+
filename:'foo.js',
274
+
content:newMagicString('var answer = 42;'),
275
+
})
274
276
275
277
bundle.addSource({
276
-
filename:'bar.js',
277
-
content:newMagicString('console.log( answer )'),
278
-
});
278
+
filename:'bar.js',
279
+
content:newMagicString('console.log( answer )'),
280
+
})
279
281
280
282
// Sources can be marked as ignore-listed, which provides a hint to debuggers
281
283
// to not step into this code and also don't show the source files depending
282
284
// on user preferences.
283
285
bundle.addSource({
284
-
filename:'some-3rdparty-library.js',
285
-
content:newMagicString('function myLib(){}'),
286
-
ignoreList:false, // <--
287
-
});
286
+
filename:'some-3rdparty-library.js',
287
+
content:newMagicString('function myLib(){}'),
288
+
ignoreList:false, // <--
289
+
})
288
290
289
291
// Advanced: a source can include an `indentExclusionRanges` property
290
292
// alongside `filename` and `content`. This will be passed to `s.indent()`
291
293
// - see documentation above
292
294
293
295
bundle
294
-
.indent() // optionally, pass an indent string, otherwise it will be guessed
295
-
.prepend('(function () {\n')
296
-
.append('}());');
296
+
.indent() // optionally, pass an indent string, otherwise it will be guessed
297
+
.prepend('(function () {\n')
298
+
.append('}());')
297
299
298
-
bundle.toString();
300
+
bundle.toString()
299
301
// (function () {
300
302
// var answer = 42;
301
303
// console.log( answer );
302
304
// }());
303
305
304
306
// options are as per `s.generateMap()` above
305
307
constmap=bundle.generateMap({
306
-
file:'bundle.js',
307
-
includeContent:true,
308
-
hires:true,
309
-
});
308
+
file:'bundle.js',
309
+
includeContent:true,
310
+
hires:true,
311
+
})
310
312
```
311
313
312
314
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:
0 commit comments