Skip to content

Commit

Permalink
docs: add usage of locator
Browse files Browse the repository at this point in the history
  • Loading branch information
mizdra committed Oct 16, 2022
1 parent fa540fe commit bdf11ed
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,54 @@ run({ ...runnerOptions, resolver, cwd }).catch((e) => {
});
```

### Example: Get locations for selectors exported by CSS Modules

`Locator` can be used to get location for selectors exported by CSS Modules.

```typescript
import { Locator } from 'happy-css-modules';
import { exists } from 'fs/promises';
import { resolve, join } from 'path';
import assert from 'assert';

const locator = new Locator({
// You can customize the transformer and resolver used by the locator.
// transformer: createDefaultTransformer(),
// resolver: createDefaultResolver(),
});

// Process https://github.com/mizdra/happy-css-modules/blob/main/example/02-import/2.css
const filePath = resolve(process.cwd(), 'example/02-import/2.css');
const result = await locator.load(filePath);
console.log(result);

assert.deepEqual(result, {
dependencies: ['/Users/mizdra/src/github.com/mizdra/happy-css-modules/example/02-import/3.css'],
tokens: [
{
name: 'b',
originalLocations: [
{
filePath: '/Users/mizdra/src/github.com/mizdra/happy-css-modules/example/02-import/3.css',
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
],
},
{
name: 'a',
originalLocations: [
{
filePath: '/Users/mizdra/src/github.com/mizdra/happy-css-modules/example/02-import/2.css',
start: { line: 3, column: 1 },
end: { line: 3, column: 2 },
},
],
},
],
});
```

## TODO

- [x] Add more tests
Expand Down

0 comments on commit bdf11ed

Please sign in to comment.