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 2aa4f52
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,52 @@ 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 { resolve } 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('example/02-import/2.css'); // Convert to absolute path
const result = await locator.load(filePath);

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 2aa4f52

Please sign in to comment.