54eac1f09b5196a1a955d51d928190b3fb00f5357f6ba847442fe54e8134adc17bbcc03956fab6079facae827b97957abbce4f4388f79594fdec6a427bd78c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # strip-literal
  2. [![NPM version](https://img.shields.io/npm/v/strip-literal?color=a1b858&label=)](https://www.npmjs.com/package/strip-literal)
  3. Strip comments and string literals from JavaScript code. Powered by [acorn](https://github.com/acornjs/acorn)'s tokenizer.
  4. ## Usage
  5. <!-- eslint-disable no-template-curly-in-string -->
  6. ```ts
  7. import { stripLiteral } from 'strip-literal'
  8. stripLiteral('const foo = `//foo ${bar}`') // 'const foo = ` ${bar}`'
  9. ```
  10. Comments, string literals will be replaced by spaces with the same length to keep the source map untouched.
  11. ## Functions
  12. ### `stripLiteralAcorn`
  13. Strip literal using [Acorn](https://github.com/acornjs/acorn)'s tokenizer.
  14. Will throw error if the input is not valid JavaScript.
  15. [Source](./src/acorn.ts)
  16. ### `stripLiteralRegex`
  17. Strip literal using RegExp.
  18. This will be faster and can work on non-JavaScript input. But will have some caveats on distinguish strings and comments.
  19. [Source](./src/regex.ts)
  20. ### `stripLiteral`
  21. Strip literal from code.
  22. Try to use `stripLiteralAcorn` first, and fallback to `stripLiteralRegex` if Acorn fails.
  23. [Source](./src/index.ts)
  24. ### `createIsLiteralPositionAcorn`
  25. Returns a function that returns whether the position is in a literal using [Acorn](https://github.com/acornjs/acorn)'s tokenizer.
  26. Will throw error if the input is not valid JavaScript.
  27. [Source](./src/acorn.ts)
  28. ## Sponsors
  29. <p align="center">
  30. <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
  31. <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
  32. </a>
  33. </p>
  34. ## License
  35. [MIT](./LICENSE) License © 2022 [Anthony Fu](https://github.com/antfu)