Skip to content

Commit

Permalink
Merge pull request #158 from mizdra/export-locator
Browse files Browse the repository at this point in the history
Export `Locator`
  • Loading branch information
mizdra committed Oct 16, 2022
2 parents d8fb521 + 2aa4f52 commit ca6d021
Show file tree
Hide file tree
Showing 2 changed files with 47 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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export {
createDefaultTransformer,
} from './transformer/index.js';
export { type Resolver, type ResolverOptions, createDefaultResolver } from './resolver/index.js';
export { Locator, type LocatorOptions, type LoadResult, type Token, type Location } from './locator/index.js';

0 comments on commit ca6d021

Please sign in to comment.