Skip to content

Commit a95cdba

Browse files
authored
Merge pull request #23 from parcel-bundler/improved-readme
Improve README
2 parents 2031c32 + cf52560 commit a95cdba

File tree

2 files changed

+112
-6
lines changed

2 files changed

+112
-6
lines changed

README.md

+111-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,125 @@
11
# Parcel's source-map library
22

3-
A purpose build source-maps library for combining and manipulating source-maps.
3+
A source map library purpose-build for the Parcel bundler with a focus on fast combining and manipulating of source-maps.
44

5-
For just reading source-maps, this library does not outperform the probably more stable and well-known package `source-map` by Mozilla.
5+
## How to use this library?
66

7-
## Why did we write this library
7+
If you want to use this library in your project or are looking to write a Parcel plugin with sourcemap support this should explain how you could get started.
8+
9+
For more information we have added doctypes to each function of the SourceMap class so you can have an in depth look at what everything does.
10+
11+
### Creating a SourceMap instance
12+
13+
You can create a sourcemap from another sourcemap or by creating it one mapping at a time.
14+
15+
#### Creating from existing sourcemap
16+
17+
To create a sourcemap from an existing sourcemap you have to ensure it is a JS Object first by asking for the object version from whichever transpiler you're running or by parsing the serialised map using `JSON.parse`.
18+
19+
After this you can call the function `addRawMappings(mappings, sources, names, lineOffset, columnOffset)` this function takes in the parameters `mappings`, `sources`, `names`, `lineOffset` and `columnOffset`. These correspond to the mappings, sources and names fields in the sourcemap object. The line and column offset are optional parameters used for offsetting the generated line and column. (this can be used when post-processing or wrapping the code linked to the sourcemap, in Parcel this is used when combining maps).
20+
21+
Example:
22+
23+
```JS
24+
import SourceMap from '@parcel/source-map';
25+
26+
const RAW_SOURCEMAP = {
27+
version: 3,
28+
file: "helloworld.js",
29+
sources: ["helloworld.coffee"],
30+
names: [],
31+
mappings: "AAAA;AAAA,EAAA,OAAO,CAAC,GAAR,CAAY,aAAZ,CAAA,CAAA;AAAA",
32+
};
33+
34+
let sourcemap = new SourceMap();
35+
sourcemap.addRawMappings(RAW_SOURCEMAP.mappings, RAW_SOURCEMAP.sources, RAW_SOURCEMAP.names);
36+
37+
// This function removes the underlying references in the native code
38+
sourcemap.delete();
39+
```
40+
41+
#### Creating a sourcemap one mapping at a time
42+
43+
If you want to use this library to create a sourcemap from scratch you can, for this you can call the `addIndexedMapping(mapping, lineOffset, columnOffset)` function.
44+
45+
Example:
46+
47+
```JS
48+
import SourceMap from '@parcel/source-map';
49+
50+
let sourcemap = new SourceMap();
51+
52+
// Add a single mapping
53+
sourcemap.addIndexedMapping({
54+
generated: {
55+
// line index starts at 1
56+
line: 1,
57+
// column index starts at 0
58+
column: 4
59+
},
60+
original: {
61+
// line index starts at 1
62+
line: 1,
63+
// column index starts at 0
64+
column: 4
65+
},
66+
source: 'index.js',
67+
// Name is optional
68+
name: 'A'
69+
});
70+
71+
// This function removes the underlying references in the native code
72+
sourcemap.delete();
73+
```
74+
75+
### Caching
76+
77+
For caching sourcemaps we have a `toBuffer()` function which returns a buffer that can be saved on disk for later use and combining sourcemaps very quickly.
78+
79+
You can add a cached map to a SourceMap instance using the `addBufferMappings(buffer, lineOffset, columnOffset)` function, where you can also offset the generated line and column.
80+
81+
## Inspiration and purpose
82+
83+
### Why did we write this library
884

985
Parcel is a performance concious bundler, and therefore we like to optimise Parcel's performance as much as possible.
1086

1187
Our original source-map implementation used mozilla's source-map and a bunch of javascript and had issues with memory usage and serialisation times (we were keeping all mappings in memory using JS objects and write/read it using JSON for caching).
1288

1389
This implementation has been written from scratch in C++ minimizing the memory usage, by utilising indexes for sources and names and optimising serialisation times by using flatbuffers instead of JSON for caching.
1490

15-
## Compile flatbuffer schema
91+
### Previous works and inspiration
92+
93+
Without these libraries this library wouldn't be as good as it is today. We've inspired and optimised our code using ideas and patterns used inside these libraries as well as used it to figure out how sourcemaps should be handled properly.
94+
95+
- [source-map by Mozilla](https://github.com/mozilla/source-map)
96+
- [source-map-mappings by Nick Fitzgerald](https://github.com/fitzgen/source-map-mappings)
97+
- [sourcemap-codec by Rich Harris](https://github.com/Rich-Harris/sourcemap-codec)
98+
99+
## Contributing to this library
100+
101+
All contributions to this library are welcome as is with any part of Parcel's vast collection of libraries and tools.
102+
103+
### Prerequisites
104+
105+
To be able to build and work on this project you need to have the following tools installed:
106+
107+
- [`emscripten`](https://emscripten.org/docs/getting_started/downloads.html)
108+
- [`node.js`](https://nodejs.org/en/)
109+
- `make`
110+
111+
### Building the project
112+
113+
For development purposes you might want to build or rebuild the project, for this you need to build the N-API module, JS Code and WASM bindings.
114+
115+
To do this run the following commmand: (for more information about this you can have a look in `./package.json` and `./Makefile`)
116+
117+
```shell
118+
yarn transpile && yarn build:dev && make clean && make all
119+
```
120+
121+
### Compile flatbuffer schema
16122

17123
```bash
18-
cd ./src && ../flatc --cpp ./sourcemap-schema.fbs && cd ..
124+
./flatc -o ./src --cpp ./src/sourcemap-schema.fbs
19125
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@parcel/source-map",
3-
"version": "2.0.0-alpha.4.10",
3+
"version": "2.0.0-alpha.4.11",
44
"main": "./dist/node.js",
55
"browser": "./dist/wasm-browser.js",
66
"license": "MIT",

0 commit comments

Comments
 (0)