or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.mdminifiers.mdplugin-configuration.mdutilities.md

utilities.mddocs/

0

# Utility Functions

1

2

Helper functions for task management, memoization, and internal plugin operations that support the core minification functionality.

3

4

## Capabilities

5

6

### Throttle All

7

8

Run multiple async tasks with limited concurrency to control resource usage and improve performance.

9

10

```javascript { .api }

11

/**

12

* Run tasks with limited concurrency

13

* @param limit - Maximum number of tasks to run simultaneously

14

* @param tasks - Array of task functions returning promises

15

* @returns Promise resolving to array of results

16

*/

17

function throttleAll<T>(limit: number, tasks: Task<T>[]): Promise<T[]>;

18

19

type Task<T> = () => Promise<T>;

20

```

21

22

**Usage Examples:**

23

24

```javascript

25

const { throttleAll } = require('terser-webpack-plugin/dist/utils');

26

27

// Limit concurrent file processing

28

const fileTasks = files.map(file =>

29

() => processFile(file)

30

);

31

32

// Process max 4 files concurrently

33

const results = await throttleAll(4, fileTasks);

34

35

// Process tasks one at a time

36

const sequentialResults = await throttleAll(1, tasks);

37

```

38

39

### Memoize

40

41

Memoization utility that caches the result of expensive function calls.

42

43

```javascript { .api }

44

/**

45

* Memoize function results for performance optimization

46

* @param fn - Function to memoize (called once, result cached)

47

* @returns Memoized function that returns cached result

48

*/

49

function memoize<T>(fn: (() => any) | undefined): () => T;

50

```

51

52

**Usage Examples:**

53

54

```javascript

55

const { memoize } = require('terser-webpack-plugin/dist/utils');

56

57

// Memoize expensive computation

58

const getExpensiveValue = memoize(() => {

59

console.log('Computing expensive value...');

60

return performExpensiveComputation();

61

});

62

63

// First call executes the function

64

const result1 = getExpensiveValue(); // Logs "Computing expensive value..."

65

66

// Subsequent calls return cached result

67

const result2 = getExpensiveValue(); // No log, returns cached value

68

console.log(result1 === result2); // true

69

```

70

71

### Worker Functions

72

73

Internal functions used by the plugin's worker system for parallel processing.

74

75

```javascript { .api }

76

/**

77

* Internal minification function used by worker processes

78

* @param options - Internal minification options

79

* @returns Promise resolving to minification result

80

*/

81

function minify<T>(options: InternalOptions<T>): Promise<MinimizedResult>;

82

83

/**

84

* Transform function for worker threads with serialized options

85

* @param options - Serialized options string

86

* @returns Promise resolving to minification result

87

*/

88

function transform(options: string): Promise<MinimizedResult>;

89

90

interface InternalOptions<T> {

91

name: string;

92

input: string;

93

inputSourceMap: SourceMapInput | undefined;

94

extractComments: ExtractCommentsOptions | undefined;

95

minimizer: {

96

implementation: MinimizerImplementation<T>;

97

options: MinimizerOptions<T>;

98

};

99

}

100

```

101

102

## Built-in Minifier Functions

103

104

All built-in minifier functions are available as exports from the utils module.

105

106

### Terser Minify Function

107

108

```javascript { .api }

109

/**

110

* Terser minification implementation with comprehensive options

111

* @param input - Object mapping filenames to source code

112

* @param sourceMap - Optional source map input

113

* @param minimizerOptions - Terser-specific configuration

114

* @param extractComments - Comment extraction settings

115

* @returns Promise resolving to minification result

116

*/

117

async function terserMinify(

118

input: Input,

119

sourceMap: SourceMapInput | undefined,

120

minimizerOptions: any,

121

extractComments?: ExtractCommentsOptions

122

): Promise<MinimizedResult>;

123

124

// Function properties

125

terserMinify.getMinimizerVersion(): string | undefined;

126

terserMinify.supportsWorkerThreads(): boolean; // returns true

127

```

128

129

### UglifyJS Minify Function

130

131

```javascript { .api }

132

/**

133

* UglifyJS minification implementation

134

* @param input - Object mapping filenames to source code

135

* @param sourceMap - Optional source map input

136

* @param minimizerOptions - UglifyJS-specific configuration

137

* @param extractComments - Comment extraction settings

138

* @returns Promise resolving to minification result

139

*/

140

async function uglifyJsMinify(

141

input: Input,

142

sourceMap: SourceMapInput | undefined,

143

minimizerOptions: any,

144

extractComments?: ExtractCommentsOptions

145

): Promise<MinimizedResult>;

146

147

// Function properties

148

uglifyJsMinify.getMinimizerVersion(): string | undefined;

149

uglifyJsMinify.supportsWorkerThreads(): boolean; // returns true

150

```

151

152

### SWC Minify Function

153

154

```javascript { .api }

155

/**

156

* SWC minification implementation

157

* @param input - Object mapping filenames to source code

158

* @param sourceMap - Optional source map input

159

* @param minimizerOptions - SWC-specific configuration

160

* @returns Promise resolving to minification result

161

*/

162

async function swcMinify(

163

input: Input,

164

sourceMap: SourceMapInput | undefined,

165

minimizerOptions: any

166

): Promise<MinimizedResult>;

167

168

// Function properties

169

swcMinify.getMinimizerVersion(): string | undefined;

170

swcMinify.supportsWorkerThreads(): boolean; // returns false

171

```

172

173

### ESBuild Minify Function

174

175

```javascript { .api }

176

/**

177

* ESBuild minification implementation

178

* @param input - Object mapping filenames to source code

179

* @param sourceMap - Optional source map input

180

* @param minimizerOptions - ESBuild-specific configuration

181

* @returns Promise resolving to minification result

182

*/

183

async function esbuildMinify(

184

input: Input,

185

sourceMap: SourceMapInput | undefined,

186

minimizerOptions: any

187

): Promise<MinimizedResult>;

188

189

// Function properties

190

esbuildMinify.getMinimizerVersion(): string | undefined;

191

esbuildMinify.supportsWorkerThreads(): boolean; // returns false

192

```

193

194

## Usage Examples

195

196

### Custom Task Processing

197

198

```javascript

199

const { throttleAll, memoize } = require('terser-webpack-plugin/dist/utils');

200

201

// Combine utilities for optimized processing

202

const getProcessingConfig = memoize(() => ({

203

workers: require('os').cpus().length - 1,

204

chunkSize: 1000,

205

}));

206

207

async function processFiles(files) {

208

const config = getProcessingConfig();

209

210

const tasks = files.map(file => async () => {

211

// Process individual file

212

return await minifyFile(file);

213

});

214

215

// Process with limited concurrency

216

return await throttleAll(config.workers, tasks);

217

}

218

```

219

220

### Accessing Utility Functions

221

222

```javascript

223

// Import utilities from the utils module

224

const {

225

throttleAll,

226

memoize,

227

terserMinify,

228

uglifyJsMinify,

229

swcMinify,

230

esbuildMinify

231

} = require('terser-webpack-plugin/dist/utils');

232

233

// Use minifier functions directly

234

const result = await terserMinify(

235

{ 'main.js': sourceCode },

236

undefined,

237

{ compress: true }

238

);

239

```

240

241

## Types

242

243

```javascript { .api }

244

type Task<T> = () => Promise<T>;

245

246

interface Input {

247

[file: string]: string;

248

}

249

250

interface MinimizedResult {

251

code: string;

252

map?: SourceMapInput;

253

errors?: (string | Error)[];

254

warnings?: (string | Error)[];

255

extractedComments?: string[];

256

}

257

258

interface MinimizerImplementation<T> {

259

(

260

input: Input,

261

sourceMap: SourceMapInput | undefined,

262

minifyOptions: MinimizerOptions<T>,

263

extractComments?: ExtractCommentsOptions

264

): Promise<MinimizedResult>;

265

getMinimizerVersion?(): string | undefined;

266

supportsWorkerThreads?(): boolean | undefined;

267

}

268

269

type MinimizerOptions<T> = PredefinedOptions<T> & T;

270

271

interface PredefinedOptions<T> {

272

module?: boolean | string;

273

ecma?: number | string;

274

}

275

276

type ExtractCommentsOptions =

277

| boolean

278

| string

279

| RegExp

280

| ExtractCommentsFunction

281

| ExtractCommentsObject;

282

283

type SourceMapInput = any; // From @jridgewell/trace-mapping

284

```