Registry / serialization / strip-literal

strip-literal

JSON →
library3.1.0jsnpmunverified

strip-literal is a JavaScript utility library designed to remove comments and string literals from code while preserving character positions. This is achieved by replacing the stripped content with an equivalent number of spaces, which is crucial for maintaining source map integrity. The library is currently on version 3.1.0 and is actively maintained, with a recent focus on performance improvements. Key differentiators include its reliance on `js-tokens` for robust parsing and its commitment to preserving code structure for tools like linters or code transformers. The project has undergone significant breaking changes, notably transitioning to an ESM-only module in v3.0.0 and switching its internal parsing engine from `acorn` to `js-tokens` in v2.0.0. Releases are fairly regular, addressing bugs and introducing optimizations.

npm install strip-literal
INSTALL
IMPORT
SIG · STRIP-LITERAL
S
strip-literal
serializationjavascriptv3.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

stripLiteral
import { stripLiteral } from 'strip-literal'
const { stripLiteral } = require('strip-literal')
The default package type became 'module' in v3.0.0, making CommonJS 'require' syntax incompatible in Node.js ESM environments.
stripLiteral
import type { StripLiteralOptions } from 'strip-literal'
TypeScript types are included, allowing for type-safe usage and options definition.
stripLiteral
import * as stripLiteralExports from 'strip-literal'
import stripLiteral from 'strip-literal'
The primary export is a named export, `stripLiteral`, not a default export.

Demonstrates how to import and use `stripLiteral` to process JavaScript code, showcasing its behavior with various string and comment types while preserving whitespace for source map compatibility.

import { stripLiteral } from 'strip-literal'; // Example 1: Basic string literal stripping const code1 = `const foo = 'hello world';\nconsole.log(foo);`; const stripped1 = stripLiteral(code1); console.log('Stripped 1:\n', stripped1); // Expected: const foo = ' ';\nconsole.log(foo); // Example 2: Template literal with interpolation and comments const code2 = ` const name = `Alice`; // This is a comment const greeting = `Hello, ${name}!`; /* multi-line comment */ console.log(greeting); `; const stripped2 = stripLiteral(code2); console.log('\nStripped 2:\n', stripped2); /* Expected Output: const name = ` `; const greeting = `Hello, ${name}!`; console.log(greeting); */ // Example 3: Stripping only specific types of literals (e.g., just comments) // Note: The library strips all comments and string literals by default. // For more granular control, you'd typically need a more advanced parser. const code3 = `// Only comments please\nconst x = 1; /* another comment */`; const stripped3 = stripLiteral(code3); console.log('\nStripped 3:\n', stripped3); // Expected: \nconst x = 1;
Debug
Known issues
breakingStarting with v3.0.0, `strip-literal` is an ES module (ESM) only. CommonJS `require()` calls will no longer work in Node.js environments configured for ESM.
fix
Update your import statements from `const { stripLiteral } = require('strip-literal')` to `import { stripLiteral } from 'strip-literal'` and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
affects: >=3.0.0
breakingVersion 2.0.0 changed the underlying parsing engine from `acorn` to `js-tokens`. While the primary API (`stripLiteral`) remained the same, this internal change could lead to subtle differences in how edge cases or malformed code are handled, potentially causing unexpected outputs for certain inputs.
fix
Thoroughly re-test your code transformation pipelines if upgrading from v1.x to v2.x to ensure the new parser behaves as expected for all your input scenarios.
affects: >=2.0.0 <3.0.0
gotchaThe `stripLiteral` function replaces stripped comments and string literals with spaces of the same length, rather than completely removing them. This behavior is intentional to preserve original character positions, which is critical for source map generation and tools relying on accurate file offsets.
fix
Be aware that the output code will contain whitespace where comments/literals once were. If you require truly 'removed' content (e.g., for minification), `strip-literal` might need to be part of a larger processing pipeline or a different tool might be more suitable.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined in ES module scope
Attempting to use CommonJS `require()` syntax for `strip-literal` in a JavaScript module (ESM) environment after upgrading to v3.0.0.
fix
Change your import statement from `const { stripLiteral } = require('strip-literal')` to `import { stripLiteral } from 'strip-literal'`.
TypeError: Cannot read properties of undefined (reading 'stripLiteral')
Trying to import `stripLiteral` as a default import (e.g., `import stripLiteral from 'strip-literal'`) when it is exported as a named export.
fix
Use named import syntax: `import { stripLiteral } from 'strip-literal'`.
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
strip-literal — npm install strip-literal · libregistry