🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

minify-html-css

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minify-html-css - npm Package Compare versions

Comparing version
1.0.2
to
1.1.0
+37
-7
dist/index.cjs

@@ -23,2 +23,3 @@ "use strict";

__export(index_exports, {
minifyCSS: () => minifyCSS,
minifyHTML: () => minifyHTML

@@ -28,10 +29,10 @@ });

// src/minify-html.ts
var import_html = require("@swc/html");
// src/minify-css.ts
var import_lightningcss = require("lightningcss");
// src/utils/merge-options.ts
function mergeOptions(targetOptions, sourceOptions, disabledOptions2, callback) {
function mergeOptions(targetOptions, sourceOptions, disabledOptions3, callback) {
const mergedOptions = structuredClone(targetOptions);
for (const key in sourceOptions) {
if (disabledOptions2?.includes(key)) {
if (disabledOptions3?.includes(key)) {
continue;

@@ -67,6 +68,34 @@ }

// src/minify-html.ts
// src/minify-css.ts
var disabledOptions = [
"context-element",
"filename",
"code",
"sourceMap",
"inputSourceMap",
"projectRoot",
"include",
"exclude",
"visitor",
"customAtRules"
];
function minifyCSS(input, options) {
const baseOptions = {
filename: "",
code: Buffer.from(input),
minify: true,
sourceMap: false
};
let mergedOptions = baseOptions;
if (options) {
mergedOptions = mergeOptions(baseOptions, options, disabledOptions);
}
const transformResult = (0, import_lightningcss.transform)(mergedOptions);
return { ...transformResult, code: transformResult.code.toString() };
}
// src/minify-html.ts
var import_html = require("@swc/html");
var disabledOptions2 = [
"context_element",
"filename",
"forceSetHtml5Doctype",

@@ -90,3 +119,3 @@ "form_element",

options,
disabledOptions,
disabledOptions2,
(key, targetValue, sourceValue) => {

@@ -108,3 +137,4 @@ if (key === "minifyCss") {

0 && (module.exports = {
minifyCSS,
minifyHTML
});

@@ -1,4 +0,105 @@

import { TransformOutput } from '@swc/html/binding';
import * as lightningcss from 'lightningcss';
import * as _swc_html_binding from '@swc/html/binding';
type MinifyCSSOptions = {
/**
* Whether to enable minification.
* @default true
*/
minify?: boolean;
/** The browser targets for the generated code. */
targets?: {
android?: number;
chrome?: number;
edge?: number;
firefox?: number;
ie?: number;
ios_saf?: number;
opera?: number;
safari?: number;
samsung?: number;
};
/** Whether to enable parsing various draft syntax. */
drafts?: {
/** Whether to enable @custom-media rules. */
customMedia?: boolean;
};
/** Whether to enable various non-standard syntax. */
nonStandard?: {
/** Whether to enable the non-standard >>> and /deep/ selector combinators used by Angular and Vue. */
deepSelectorCombinator?: boolean;
};
/** Whether to compile this file as a CSS module. */
cssModules?: boolean | {
/** The pattern to use when renaming class names and other identifiers. Default is `[hash]_[local]`. */
pattern?: string;
/** Whether to rename dashed identifiers, e.g. custom properties. */
dashedIdents?: boolean;
/** Whether to enable hashing for `@keyframes`. */
animation?: boolean;
/** Whether to enable hashing for CSS grid identifiers. */
grid?: boolean;
/** Whether to enable hashing for `@container` names. */
container?: boolean;
/** Whether to enable hashing for custom identifiers. */
customIdents?: boolean;
/** Whether to require at least one class or id selector in each rule. */
pure?: boolean;
};
/**
* Whether to analyze dependencies (e.g. `@import` and `url()`).
* When enabled, `@import` rules are removed, and `url()` dependencies
* are replaced with hashed placeholders that can be replaced with the final
* urls later (after bundling). Dependencies are returned as part of the result.
*/
analyzeDependencies?: boolean | {
/** Whether to preserve `@import` rules rather than removing them. */
preserveImports?: boolean;
};
/**
* Replaces user action pseudo classes with class names that can be applied from JavaScript.
* This is useful for polyfills, for example.
*/
pseudoClasses?: {
hover?: string;
active?: string;
focus?: string;
focusVisible?: string;
focusWithin?: string;
};
/**
* A list of class names, ids, and custom identifiers (e.g. @keyframes) that are known
* to be unused. These will be removed during minification. Note that these are not
* selectors but individual names (without any . or # prefixes).
*/
unusedSymbols?: string[];
/**
* Whether to ignore invalid rules and declarations rather than erroring.
* When enabled, warnings are returned, and the invalid rule or declaration is
* omitted from the output code.
*/
errorRecovery?: boolean;
};
/**
* Minifies CSS content using Lightning CSS.
*
* @param input - The CSS string to be minified.
* @param {MinifyCSSOptions} options - Optional configuration for the minification process.
* @returns An object containing the minified CSS code and optional exports for CSS modules.
*
* @example
* const result = minifyCSS('body { color : red ; }');
* console.log(result.code); // Outputs: 'body{color:red}'
*/
declare function minifyCSS(input: string, options?: MinifyCSSOptions): {
code: string;
map: Uint8Array | void;
exports: lightningcss.CSSModuleExports | void;
references: lightningcss.CSSModuleReferences;
dependencies: lightningcss.Dependency[] | void;
warnings: lightningcss.Warning[];
};
/**
* This file is derived from the SWC Project (Apache 2.0).

@@ -213,4 +314,4 @@ * Original types: https://github.com/swc-project/swc/blob/main/packages/html/index.ts

*/
declare function minifyHTML(input: string, options?: MinifyHTMLOptions): TransformOutput;
declare function minifyHTML(input: string, options?: MinifyHTMLOptions): _swc_html_binding.TransformOutput;
export { type MinifierType, type MinifyHTMLOptions, minifyHTML };
export { type MinifierType, type MinifyCSSOptions, type MinifyHTMLOptions, minifyCSS, minifyHTML };

@@ -1,4 +0,105 @@

import { TransformOutput } from '@swc/html/binding';
import * as lightningcss from 'lightningcss';
import * as _swc_html_binding from '@swc/html/binding';
type MinifyCSSOptions = {
/**
* Whether to enable minification.
* @default true
*/
minify?: boolean;
/** The browser targets for the generated code. */
targets?: {
android?: number;
chrome?: number;
edge?: number;
firefox?: number;
ie?: number;
ios_saf?: number;
opera?: number;
safari?: number;
samsung?: number;
};
/** Whether to enable parsing various draft syntax. */
drafts?: {
/** Whether to enable @custom-media rules. */
customMedia?: boolean;
};
/** Whether to enable various non-standard syntax. */
nonStandard?: {
/** Whether to enable the non-standard >>> and /deep/ selector combinators used by Angular and Vue. */
deepSelectorCombinator?: boolean;
};
/** Whether to compile this file as a CSS module. */
cssModules?: boolean | {
/** The pattern to use when renaming class names and other identifiers. Default is `[hash]_[local]`. */
pattern?: string;
/** Whether to rename dashed identifiers, e.g. custom properties. */
dashedIdents?: boolean;
/** Whether to enable hashing for `@keyframes`. */
animation?: boolean;
/** Whether to enable hashing for CSS grid identifiers. */
grid?: boolean;
/** Whether to enable hashing for `@container` names. */
container?: boolean;
/** Whether to enable hashing for custom identifiers. */
customIdents?: boolean;
/** Whether to require at least one class or id selector in each rule. */
pure?: boolean;
};
/**
* Whether to analyze dependencies (e.g. `@import` and `url()`).
* When enabled, `@import` rules are removed, and `url()` dependencies
* are replaced with hashed placeholders that can be replaced with the final
* urls later (after bundling). Dependencies are returned as part of the result.
*/
analyzeDependencies?: boolean | {
/** Whether to preserve `@import` rules rather than removing them. */
preserveImports?: boolean;
};
/**
* Replaces user action pseudo classes with class names that can be applied from JavaScript.
* This is useful for polyfills, for example.
*/
pseudoClasses?: {
hover?: string;
active?: string;
focus?: string;
focusVisible?: string;
focusWithin?: string;
};
/**
* A list of class names, ids, and custom identifiers (e.g. @keyframes) that are known
* to be unused. These will be removed during minification. Note that these are not
* selectors but individual names (without any . or # prefixes).
*/
unusedSymbols?: string[];
/**
* Whether to ignore invalid rules and declarations rather than erroring.
* When enabled, warnings are returned, and the invalid rule or declaration is
* omitted from the output code.
*/
errorRecovery?: boolean;
};
/**
* Minifies CSS content using Lightning CSS.
*
* @param input - The CSS string to be minified.
* @param {MinifyCSSOptions} options - Optional configuration for the minification process.
* @returns An object containing the minified CSS code and optional exports for CSS modules.
*
* @example
* const result = minifyCSS('body { color : red ; }');
* console.log(result.code); // Outputs: 'body{color:red}'
*/
declare function minifyCSS(input: string, options?: MinifyCSSOptions): {
code: string;
map: Uint8Array | void;
exports: lightningcss.CSSModuleExports | void;
references: lightningcss.CSSModuleReferences;
dependencies: lightningcss.Dependency[] | void;
warnings: lightningcss.Warning[];
};
/**
* This file is derived from the SWC Project (Apache 2.0).

@@ -213,4 +314,4 @@ * Original types: https://github.com/swc-project/swc/blob/main/packages/html/index.ts

*/
declare function minifyHTML(input: string, options?: MinifyHTMLOptions): TransformOutput;
declare function minifyHTML(input: string, options?: MinifyHTMLOptions): _swc_html_binding.TransformOutput;
export { type MinifierType, type MinifyHTMLOptions, minifyHTML };
export { type MinifierType, type MinifyCSSOptions, type MinifyHTMLOptions, minifyCSS, minifyHTML };

@@ -1,9 +0,11 @@

// src/minify-html.ts
import { minifyFragmentSync } from "@swc/html";
// src/minify-css.ts
import {
transform
} from "lightningcss";
// src/utils/merge-options.ts
function mergeOptions(targetOptions, sourceOptions, disabledOptions2, callback) {
function mergeOptions(targetOptions, sourceOptions, disabledOptions3, callback) {
const mergedOptions = structuredClone(targetOptions);
for (const key in sourceOptions) {
if (disabledOptions2?.includes(key)) {
if (disabledOptions3?.includes(key)) {
continue;

@@ -39,6 +41,34 @@ }

// src/minify-html.ts
// src/minify-css.ts
var disabledOptions = [
"context-element",
"filename",
"code",
"sourceMap",
"inputSourceMap",
"projectRoot",
"include",
"exclude",
"visitor",
"customAtRules"
];
function minifyCSS(input, options) {
const baseOptions = {
filename: "",
code: Buffer.from(input),
minify: true,
sourceMap: false
};
let mergedOptions = baseOptions;
if (options) {
mergedOptions = mergeOptions(baseOptions, options, disabledOptions);
}
const transformResult = transform(mergedOptions);
return { ...transformResult, code: transformResult.code.toString() };
}
// src/minify-html.ts
import { minifyFragmentSync } from "@swc/html";
var disabledOptions2 = [
"context_element",
"filename",
"forceSetHtml5Doctype",

@@ -62,3 +92,3 @@ "form_element",

options,
disabledOptions,
disabledOptions2,
(key, targetValue, sourceValue) => {

@@ -79,3 +109,4 @@ if (key === "minifyCss") {

export {
minifyCSS,
minifyHTML
};
+13
-2
# Notice on Third-Party Code
This project includes code and documentation adapted from the SWC Project (https://github.com/swc-project/swc), which is licensed under the Apache License, Version 2.0.
This project includes code and documentation adapted from the SWC Project (https://github.com/swc-project/swc), licensed under the Apache License, Version 2.0.
The full text of the Apache License is included as LICENSE-APACHE in this repository.
Modifications were made to the original code and documentation, including the removal and adaptation of certain options to fit this project’s requirements.
The file `src/minify-html-types.ts` contains type definitions copied and modified from the SWC Project.
Modifications include documentation changes and removal or adaptation of certain options to fit this project’s requirements.
---
This project includes code adapted from Lightning CSS (https://github.com/parcel-bundler/lightningcss), licensed under the Mozilla Public License, Version 2.0 (MPL-2.0).
The full text of the MPL-2.0 license is included as LICENSE-MPL-2.0 in this repository.
The file `src/minify-css-types.ts` contains type definitions copied and modified from Lightning CSS.
Modifications include documentation changes and removal or adaptation of certain options to fit this project’s requirements.
See the source files for details on which options are omitted or disabled from upstream projects.
+6
-2
{
"name": "minify-html-css",
"version": "1.0.2",
"version": "1.1.0",
"description": "🔽 A library to minify HTML and CSS.",

@@ -52,3 +52,4 @@ "keywords": [

"dependencies": {
"@swc/html": "^1.13.5"
"@swc/html": "^1.13.5",
"lightningcss": "^1.30.1"
},

@@ -69,3 +70,6 @@ "devDependencies": {

"node": ">=18"
},
"publishConfig": {
"provenance": true
}
}

@@ -6,3 +6,3 @@ # minify-html-css

[![CI](https://github.com/femincan/minify-html-css/actions/workflows/ci.yml/badge.svg)](https://github.com/femincan/minify-html-css/actions/workflows/ci.yml)
![NPM Version](https://img.shields.io/npm/v/minify-html-css)
[![NPM Version](https://img.shields.io/npm/v/minify-html-css)](https://www.npmjs.com/package/minify-html-css)
[![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)

@@ -112,2 +112,17 @@

### `minifyCSS(input: string, options?: MinifyCSSOptions): TransformResult`
**Description:**
Minifies CSS code by removing unnecessary whitespace, optimizing values, and applying various transformations.
> **Implementation note:**
> This function is a wrapper around the [`lightningcss`](https://github.com/parcel-bundler/lightningcss) package and uses its minification logic under the hood.
**Parameters:**
- `input` (`string`): The CSS code to minify.
- `options?` (`MinifyCSSOptions`): Configuration object for fine-grained control.
For detailed type definitions and documentation for options, see [`src/minify-css-types.ts`](https://github.com/femincan/minify-html-css/blob/main/src/minify-css-types.ts).
---

@@ -117,3 +132,3 @@

- [ ] Implement CSS minification function (`minifyCSS`) built on top of Lightning CSS
- [x] Implement CSS minification function (`minifyCSS`) built on top of Lightning CSS
- [ ] Implement CLI usage (command-line tool)

@@ -120,0 +135,0 @@