Skip to content

Commit 0e22897

Browse files
authored
feat!: migrate build to pure ESM and TypeScript (#310)
1 parent 410fd4d commit 0e22897

57 files changed

Lines changed: 8313 additions & 6448 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
release:
10-
uses: sxzz/workflows/.github/workflows/release.yml@v1
10+
uses: sxzz/workflows/.github/workflows/release.yml@main
1111
with:
1212
publish: true
1313
permissions:

.prettierignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,57 +25,57 @@ magic-string works in both node.js and browser environments. For node, install w
2525
npm i magic-string
2626
```
2727

28-
To use in browser, grab the [magic-string.umd.js](https://unpkg.com/magic-string/dist/magic-string.umd.js) file and add it to your page:
28+
magic-string is ESM-only. In browsers, import it from an ESM CDN:
2929

3030
```html
31-
<script src="magic-string.umd.js"></script>
31+
<script type="module">
32+
import MagicString from 'https://unpkg.com/magic-string/dist/index.mjs';
33+
</script>
3234
```
3335

34-
(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-
3636
## Usage
3737

3838
These examples assume you're in node.js, or something similar:
3939

4040
```js
41-
import MagicString from 'magic-string';
42-
import fs from 'fs';
41+
import fs from 'node:fs'
42+
import MagicString from 'magic-string'
4343

44-
const s = new MagicString('problems = 99');
44+
const s = new MagicString('problems = 99')
4545

46-
s.update(0, 8, 'answer');
47-
s.toString(); // 'answer = 99'
46+
s.update(0, 8, 'answer')
47+
s.toString() // 'answer = 99'
4848

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'
5151

52-
s.prepend('var ').append(';'); // most methods are chainable
53-
s.toString(); // 'var answer = 42;'
52+
s.prepend('var ').append(';') // most methods are chainable
53+
s.toString() // 'var answer = 42;'
5454

5555
const map = s.generateMap({
56-
source: 'source.js',
57-
file: 'converted.js.map',
58-
includeContent: true,
59-
}); // generates a v3 sourcemap
56+
source: 'source.js',
57+
file: 'converted.js.map',
58+
includeContent: true,
59+
}) // generates a v3 sourcemap
6060

61-
fs.writeFileSync('converted.js', s.toString());
62-
fs.writeFileSync('converted.js.map', map.toString());
61+
fs.writeFileSync('converted.js', s.toString())
62+
fs.writeFileSync('converted.js.map', map.toString())
6363
```
6464

6565
You can pass an options argument:
6666

6767
```js
6868
const s = new MagicString(someCode, {
69-
// these options will be used if you later call `bundle.addSource( s )` - see below
70-
filename: 'foo.js',
71-
indentExclusionRanges: [
72-
/*...*/
73-
],
74-
// mark source as ignore in DevTools, see below #Bundling
75-
ignoreList: false,
76-
// adjust the incoming position - see below
77-
offset: 0,
78-
});
69+
// these options will be used if you later call `bundle.addSource( s )` - see below
70+
filename: 'foo.js',
71+
indentExclusionRanges: [
72+
/* ... */
73+
],
74+
// mark source as ignore in DevTools, see below #Bundling
75+
ignoreList: false,
76+
// adjust the incoming position - see below
77+
offset: 0,
78+
})
7979
```
8080

8181
## Properties
@@ -87,9 +87,9 @@ Sets the offset property to adjust the incoming position for the following APIs:
8787
Example usage:
8888

8989
```ts
90-
const s = new MagicString('hello world', { offset: 0 });
91-
s.offset = 6;
92-
s.slice() === 'world';
90+
const s = new MagicString('hello world', { offset: 0 })
91+
s.offset = 6
92+
s.slice() === 'world'
9393
```
9494

9595
## Methods
@@ -133,7 +133,7 @@ The returned sourcemap has two (non-enumerable) methods attached for convenience
133133
- `toUrl` - returns a DataURI containing the sourcemap. Useful for doing this sort of thing:
134134

135135
```js
136-
code += '\n//# sourceMappingURL=' + map.toUrl();
136+
code += `\n//# sourceMappingURL=${map.toUrl()}`
137137
```
138138

139139
### s.hasChanged()
@@ -195,14 +195,14 @@ Same as `s.appendRight(...)`, except that the inserted content will go _before_
195195
String replacement with RegExp or string. The `substitution` parameter supports strings and functions. Returns `this`.
196196

197197
```ts
198-
import MagicString from 'magic-string';
198+
import MagicString from 'magic-string'
199199

200-
const s = new MagicString(source);
200+
const s = new MagicString(source)
201201

202-
s.replace('foo', 'bar');
203-
s.replace('foo', (str, index, s) => str + '-' + index);
204-
s.replace(/foo/g, 'bar');
205-
s.replace(/(\w)(\d+)/g, (_, $1, $2) => $1.toUpperCase() + $2);
202+
s.replace('foo', 'bar')
203+
s.replace('foo', (str, index, s) => `${str}-${index}`)
204+
s.replace(/foo/g, 'bar')
205+
s.replace(/(\w)(\d+)/g, (_, $1, $2) => $1.toUpperCase() + $2)
206206
```
207207

208208
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
262262

263263
## Bundling
264264

265-
To concatenate several sources, use `MagicString.Bundle`:
265+
To concatenate several sources, use `Bundle`:
266266

267267
```js
268-
const bundle = new MagicString.Bundle();
268+
import MagicString, { Bundle } from 'magic-string'
269+
270+
const bundle = new Bundle()
269271

270272
bundle.addSource({
271-
filename: 'foo.js',
272-
content: new MagicString('var answer = 42;'),
273-
});
273+
filename: 'foo.js',
274+
content: new MagicString('var answer = 42;'),
275+
})
274276

275277
bundle.addSource({
276-
filename: 'bar.js',
277-
content: new MagicString('console.log( answer )'),
278-
});
278+
filename: 'bar.js',
279+
content: new MagicString('console.log( answer )'),
280+
})
279281

280282
// Sources can be marked as ignore-listed, which provides a hint to debuggers
281283
// to not step into this code and also don't show the source files depending
282284
// on user preferences.
283285
bundle.addSource({
284-
filename: 'some-3rdparty-library.js',
285-
content: new MagicString('function myLib(){}'),
286-
ignoreList: false, // <--
287-
});
286+
filename: 'some-3rdparty-library.js',
287+
content: new MagicString('function myLib(){}'),
288+
ignoreList: false, // <--
289+
})
288290

289291
// Advanced: a source can include an `indentExclusionRanges` property
290292
// alongside `filename` and `content`. This will be passed to `s.indent()`
291293
// - see documentation above
292294

293295
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('}());')
297299

298-
bundle.toString();
300+
bundle.toString()
299301
// (function () {
300302
// var answer = 42;
301303
// console.log( answer );
302304
// }());
303305

304306
// options are as per `s.generateMap()` above
305307
const map = bundle.generateMap({
306-
file: 'bundle.js',
307-
includeContent: true,
308-
hires: true,
309-
});
308+
file: 'bundle.js',
309+
includeContent: true,
310+
hires: true,
311+
})
310312
```
311313

312314
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:
313315

314316
```js
315-
const bundle = new MagicString.Bundle();
317+
const bundle = new Bundle()
316318
const source = new MagicString(someCode, {
317-
filename: 'foo.js',
318-
});
319+
filename: 'foo.js',
320+
})
319321

320-
bundle.addSource(source);
322+
bundle.addSource(source)
321323
```
322324

323325
## License

benchmark/data-min.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable */
21
import { encode as k } from 'sourcemap-codec';
32
class p {
43
constructor(t) {

benchmark/data.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable */
21
import { encode } from 'sourcemap-codec';
32
class BitSet {
43
constructor(arg) {

0 commit comments

Comments
 (0)