https://github.com/coffee-cup/count.macro
Babel macro for counting number of lines or words in files at compile time
https://github.com/coffee-cup/count.macro
babel count javascript macro typescript
Last synced: 3 months ago
JSON representation
Babel macro for counting number of lines or words in files at compile time
- Host: GitHub
- URL: https://github.com/coffee-cup/count.macro
- Owner: coffee-cup
- License: mit
- Created: 2019-11-21T16:46:23.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-18T19:03:17.000Z (over 2 years ago)
- Last Synced: 2025-03-29T09:41:52.144Z (4 months ago)
- Topics: babel, count, javascript, macro, typescript
- Language: TypeScript
- Homepage: https://codesandbox.io/s/countmacro-jgo3c
- Size: 202 KB
- Stars: 22
- Watchers: 2
- Forks: 0
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# count.macro
[](https://github.com/coffee-cup/count.macro/actions?query=workflow%3ACI)
[](https://www.npmjs.com/package/count.macro)
[](https://github.com/coffee-cup/count.macro/blob/master/LICENSE)
[](https://github.com/kentcdodds/babel-plugin-macros)Count lines or words in files at build time
## Installation
`count.macro` is a [Babel
macro](https://github.com/kentcdodds/babel-plugin-macros). This will work out of
the box with CRA, Gatsby, and Next.```shell
npm install --save-dev count.macro
```## Usage
Line and word information is based on the **source** file, not the output file.
For example, this file
```js
import { lines, linesIn, words, wordsIn } from "count.macro";console.log(`This file has ${lines} lines`);
console.log(`lines.txt has ${linesIn("./lines.txt")}`);console.log(`This file has ${words} words`);
console.log(`words.txt has ${wordsIn("./words.txt")}`);
```will be transpiled to
```js
console.log(`This file has ${7} lines`);
console.log(`lines.txt has ${100}`);
console.log(`This file has ${25} words`);
console.log(`words.txt has ${1000}`);
```### Named Exports
- `lines` is a number that will be transpiled to number of lines in current file
- `linesIn` is a function that takes a filename as an argument. The call will be replaced with the number of lines in the file (relative to the current file).
- `words` is a number that will be transpiled to number of words in current file.
- `wordsIn` is a function that takes a filename as an argument. The call will be replaced with the number of words in the file (relative to the current file).## CodeSandbox Example
[](https://codesandbox.io/s/countmacro-jgo3c?fontsize=14&hidenavigation=1&theme=dark)