wip:milestone 0 fixes
Some checks failed
CI/CD Pipeline / unit-tests (push) Failing after 1m16s
CI/CD Pipeline / integration-tests (push) Failing after 2m32s
CI/CD Pipeline / lint (push) Successful in 5m22s
CI/CD Pipeline / e2e-tests (push) Has been skipped
CI/CD Pipeline / build (push) Has been skipped

This commit is contained in:
2026-03-15 12:35:42 +02:00
parent 6708cf28a7
commit cffdf8af86
61266 changed files with 4511646 additions and 1938 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Anthony Fu <https://github.com/antfu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,29 @@
# strip-literal
[![NPM version](https://img.shields.io/npm/v/strip-literal?color=a1b858&label=)](https://www.npmjs.com/package/strip-literal)
Strip comments and string literals from JavaScript code. Powered by [`js-tokens`](https://github.com/lydell/js-tokens).
## Usage
<!-- eslint-disable no-template-curly-in-string -->
```ts
import { stripLiteral } from 'strip-literal'
stripLiteral('const foo = `//foo ${bar}`') // 'const foo = ` ${bar}`'
```
Comments, string literals will be replaced by spaces with the same length to keep the source map untouched.
## Sponsors
<p align="center">
<a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
<img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
</a>
</p>
## License
[MIT](./LICENSE) License © 2022 [Anthony Fu](https://github.com/antfu)

View File

@@ -0,0 +1,88 @@
'use strict';
const jsTokens = require('js-tokens');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const jsTokens__default = /*#__PURE__*/_interopDefaultCompat(jsTokens);
const FILL_COMMENT = " ";
function stripLiteralFromToken(token, fillChar, filter) {
if (token.type === "SingleLineComment") {
return FILL_COMMENT.repeat(token.value.length);
}
if (token.type === "MultiLineComment") {
return token.value.replace(/[^\n]/g, FILL_COMMENT);
}
if (token.type === "StringLiteral") {
if (!token.closed) {
return token.value;
}
const body = token.value.slice(1, -1);
if (filter(body)) {
return token.value[0] + fillChar.repeat(body.length) + token.value[token.value.length - 1];
}
}
if (token.type === "NoSubstitutionTemplate") {
const body = token.value.slice(1, -1);
if (filter(body)) {
return `\`${body.replace(/[^\n]/g, fillChar)}\``;
}
}
if (token.type === "RegularExpressionLiteral") {
const body = token.value;
if (filter(body)) {
return body.replace(/\/(.*)\/(\w?)$/g, (_, $1, $2) => `/${fillChar.repeat($1.length)}/${$2}`);
}
}
if (token.type === "TemplateHead") {
const body = token.value.slice(1, -2);
if (filter(body)) {
return `\`${body.replace(/[^\n]/g, fillChar)}\${`;
}
}
if (token.type === "TemplateTail") {
const body = token.value.slice(0, -2);
if (filter(body)) {
return `}${body.replace(/[^\n]/g, fillChar)}\``;
}
}
if (token.type === "TemplateMiddle") {
const body = token.value.slice(1, -2);
if (filter(body)) {
return `}${body.replace(/[^\n]/g, fillChar)}\${`;
}
}
return token.value;
}
function optionsWithDefaults(options) {
return {
fillChar: options?.fillChar ?? " ",
filter: options?.filter ?? (() => true)
};
}
function stripLiteral(code, options) {
let result = "";
const _options = optionsWithDefaults(options);
for (const token of jsTokens__default(code, { jsx: false })) {
result += stripLiteralFromToken(token, _options.fillChar, _options.filter);
}
return result;
}
function stripLiteralDetailed(code, options) {
let result = "";
const tokens = [];
const _options = optionsWithDefaults(options);
for (const token of jsTokens__default(code, { jsx: false })) {
tokens.push(token);
result += stripLiteralFromToken(token, _options.fillChar, _options.filter);
}
return {
result,
tokens
};
}
exports.stripLiteral = stripLiteral;
exports.stripLiteralDetailed = stripLiteralDetailed;
exports.stripLiteralJsTokens = stripLiteralDetailed;

View File

@@ -0,0 +1,30 @@
import { Token } from 'js-tokens';
interface StripLiteralOptions {
/**
* Will be called for each string literal. Return false to skip stripping.
*/
filter?: (s: string) => boolean;
/**
* Fill the stripped literal with this character.
* It must be a single character.
*
* @default ' '
*/
fillChar?: string;
}
/**
* Strip literal from code.
*/
declare function stripLiteral(code: string, options?: StripLiteralOptions): string;
/**
* Strip literal from code, return more detailed information.
*/
declare function stripLiteralDetailed(code: string, options?: StripLiteralOptions): {
result: string;
tokens: Token[];
};
export { stripLiteral, stripLiteralDetailed, stripLiteralDetailed as stripLiteralJsTokens };
export type { StripLiteralOptions };

View File

@@ -0,0 +1,30 @@
import { Token } from 'js-tokens';
interface StripLiteralOptions {
/**
* Will be called for each string literal. Return false to skip stripping.
*/
filter?: (s: string) => boolean;
/**
* Fill the stripped literal with this character.
* It must be a single character.
*
* @default ' '
*/
fillChar?: string;
}
/**
* Strip literal from code.
*/
declare function stripLiteral(code: string, options?: StripLiteralOptions): string;
/**
* Strip literal from code, return more detailed information.
*/
declare function stripLiteralDetailed(code: string, options?: StripLiteralOptions): {
result: string;
tokens: Token[];
};
export { stripLiteral, stripLiteralDetailed, stripLiteralDetailed as stripLiteralJsTokens };
export type { StripLiteralOptions };

View File

@@ -0,0 +1,30 @@
import { Token } from 'js-tokens';
interface StripLiteralOptions {
/**
* Will be called for each string literal. Return false to skip stripping.
*/
filter?: (s: string) => boolean;
/**
* Fill the stripped literal with this character.
* It must be a single character.
*
* @default ' '
*/
fillChar?: string;
}
/**
* Strip literal from code.
*/
declare function stripLiteral(code: string, options?: StripLiteralOptions): string;
/**
* Strip literal from code, return more detailed information.
*/
declare function stripLiteralDetailed(code: string, options?: StripLiteralOptions): {
result: string;
tokens: Token[];
};
export { stripLiteral, stripLiteralDetailed, stripLiteralDetailed as stripLiteralJsTokens };
export type { StripLiteralOptions };

View File

@@ -0,0 +1,80 @@
import jsTokens from 'js-tokens';
const FILL_COMMENT = " ";
function stripLiteralFromToken(token, fillChar, filter) {
if (token.type === "SingleLineComment") {
return FILL_COMMENT.repeat(token.value.length);
}
if (token.type === "MultiLineComment") {
return token.value.replace(/[^\n]/g, FILL_COMMENT);
}
if (token.type === "StringLiteral") {
if (!token.closed) {
return token.value;
}
const body = token.value.slice(1, -1);
if (filter(body)) {
return token.value[0] + fillChar.repeat(body.length) + token.value[token.value.length - 1];
}
}
if (token.type === "NoSubstitutionTemplate") {
const body = token.value.slice(1, -1);
if (filter(body)) {
return `\`${body.replace(/[^\n]/g, fillChar)}\``;
}
}
if (token.type === "RegularExpressionLiteral") {
const body = token.value;
if (filter(body)) {
return body.replace(/\/(.*)\/(\w?)$/g, (_, $1, $2) => `/${fillChar.repeat($1.length)}/${$2}`);
}
}
if (token.type === "TemplateHead") {
const body = token.value.slice(1, -2);
if (filter(body)) {
return `\`${body.replace(/[^\n]/g, fillChar)}\${`;
}
}
if (token.type === "TemplateTail") {
const body = token.value.slice(0, -2);
if (filter(body)) {
return `}${body.replace(/[^\n]/g, fillChar)}\``;
}
}
if (token.type === "TemplateMiddle") {
const body = token.value.slice(1, -2);
if (filter(body)) {
return `}${body.replace(/[^\n]/g, fillChar)}\${`;
}
}
return token.value;
}
function optionsWithDefaults(options) {
return {
fillChar: options?.fillChar ?? " ",
filter: options?.filter ?? (() => true)
};
}
function stripLiteral(code, options) {
let result = "";
const _options = optionsWithDefaults(options);
for (const token of jsTokens(code, { jsx: false })) {
result += stripLiteralFromToken(token, _options.fillChar, _options.filter);
}
return result;
}
function stripLiteralDetailed(code, options) {
let result = "";
const tokens = [];
const _options = optionsWithDefaults(options);
for (const token of jsTokens(code, { jsx: false })) {
tokens.push(token);
result += stripLiteralFromToken(token, _options.fillChar, _options.filter);
}
return {
result,
tokens
};
}
export { stripLiteral, stripLiteralDetailed, stripLiteralDetailed as stripLiteralJsTokens };

View File

@@ -0,0 +1,60 @@
{
"name": "strip-literal",
"type": "module",
"version": "3.1.0",
"description": "Strip comments and string literals from JavaScript code",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
"funding": "https://github.com/sponsors/antfu",
"homepage": "https://github.com/antfu/strip-literal#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/antfu/strip-literal.git"
},
"bugs": {
"url": "https://github.com/antfu/strip-literal/issues"
},
"keywords": [],
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.mts",
"files": [
"dist"
],
"dependencies": {
"js-tokens": "^9.0.1"
},
"devDependencies": {
"@antfu/eslint-config": "^5.4.1",
"@antfu/ni": "^26.0.1",
"@types/node": "^24.5.2",
"bumpp": "^10.2.3",
"eslint": "^9.36.0",
"pnpm": "^10.17.1",
"rimraf": "^6.0.1",
"three": "^0.180.0",
"tsx": "^4.20.6",
"typescript": "^5.9.2",
"unbuild": "^3.6.1",
"vite": "^7.1.7",
"vitest": "^3.2.4",
"vue": "^3.5.22"
},
"scripts": {
"build": "unbuild",
"dev": "unbuild --stub",
"lint": "eslint .",
"release": "bumpp",
"start": "tsx src/index.ts",
"test": "vitest",
"bench": "vitest bench",
"typecheck": "tsc --noEmit"
}
}