or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md

index.mddocs/

0

# Uglifyify

1

2

Uglifyify is a Browserify transform that minifies JavaScript code using Terser (a maintained fork of UglifyJS2). It processes files individually before bundling, enabling more aggressive dead code elimination and conditional require optimization compared to post-bundle minification.

3

4

## Package Information

5

6

- **Package Name**: uglifyify

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install uglifyify`

10

11

## Core Imports

12

13

```javascript

14

const uglifyify = require('uglifyify');

15

```

16

17

## Basic Usage

18

19

```javascript

20

const browserify = require('browserify');

21

const fs = require('fs');

22

23

// Basic usage as a transform

24

const bundler = browserify('./index.js');

25

bundler.transform(uglifyify);

26

bundler.bundle().pipe(fs.createWriteStream('./bundle.js'));

27

28

// Global transform to minify all modules

29

bundler.transform(uglifyify, { global: true });

30

31

// Command line usage

32

// browserify -t uglifyify ./index.js > bundle.js

33

// browserify -g uglifyify ./index.js > bundle.js

34

```

35

36

## Architecture

37

38

Uglifyify operates as a Browserify transform that:

39

40

- **File-level Processing**: Minifies each file individually before bundling

41

- **Stream Interface**: Uses Node.js streams via the 'through' package

42

- **Selective Processing**: Filters files by extension and ignore patterns

43

- **Source Map Integration**: Preserves and generates source maps when debug mode is enabled

44

- **Dead Code Elimination**: Removes unreachable code paths for conditional requires

45

46

## Capabilities

47

48

### Transform Function

49

50

Creates a transform stream that minifies JavaScript files using Terser.

51

52

```javascript { .api }

53

/**

54

* Creates a transform stream for minifying JavaScript files

55

* @param {string} file - Path to the file being transformed

56

* @param {Object} opts - Configuration options

57

* @returns {Stream} Transform stream that processes the file

58

*/

59

function uglifyify(file, opts);

60

```

61

62

**Configuration Options:**

63

64

```javascript { .api }

65

interface UglifyifyOptions {

66

/** Glob patterns to ignore specific files */

67

ignore?: string | string[];

68

/** File extensions to process (e.g., ['.js', '.coffee']) */

69

exts?: string[];

70

/** Alternative way to specify file extensions */

71

x?: string[];

72

/** Whether to apply transform globally to all modules */

73

global?: boolean;

74

/** Enable/disable source map generation (auto-detected from debug flag) */

75

sourceMap?: boolean;

76

/** Terser compression options */

77

compress?: boolean | object;

78

/** Terser mangling options */

79

mangle?: boolean | object;

80

/** Terser parsing options */

81

parse?: object;

82

/** Terser beautify options */

83

beautify?: boolean | object;

84

/** Terser output options */

85

output?: object;

86

/** Global variable definitions for dead code elimination */

87

define?: object;

88

/** Internal flags object */

89

_flags?: { debug?: boolean };

90

}

91

```

92

93

**Command Line Option Mappings:**

94

95

The following command line flags are automatically mapped to Terser options:

96

97

```javascript { .api }

98

interface CommandLineMappings {

99

/** -c flag maps to compress option */

100

c?: boolean | object;

101

/** -m flag maps to mangle option */

102

m?: boolean | object;

103

/** -p flag maps to parse option */

104

p?: object;

105

/** -b flag maps to beautify option */

106

b?: boolean | object;

107

/** -o flag maps to output option */

108

o?: object;

109

/** -d flag maps to define option */

110

d?: object;

111

}

112

```

113

114

## Usage Examples

115

116

### File Extension Filtering

117

118

Process only specific file types to avoid errors with non-JavaScript files:

119

120

```javascript

121

// Command line

122

// browserify -t [ uglifyify -x .js -x .coffee ]

123

124

// Programmatic

125

bundler.transform(uglifyify, {

126

exts: ['.js'],

127

x: ['.coffee']

128

});

129

```

130

131

### Ignoring Files

132

133

Skip minification for specific files or patterns:

134

135

```javascript

136

// Command line

137

// browserify -g [ uglifyify --ignore '**/node_modules/weakmap/*' ]

138

139

// Programmatic

140

bundler.transform(uglifyify, {

141

global: true,

142

ignore: [

143

'**/node_modules/weakmap/*',

144

'**/node_modules/async/*'

145

]

146

});

147

```

148

149

### Source Maps

150

151

Enable source map generation with debug mode:

152

153

```javascript

154

// Command line

155

// browserify -t uglifyify --debug index.js

156

157

// Programmatic

158

const bundler = browserify({ debug: true });

159

bundler.transform(uglifyify);

160

161

// Disable source maps explicitly

162

bundler.transform(uglifyify, { sourceMap: false });

163

```

164

165

### Advanced Terser Options

166

167

Pass custom options to the Terser minifier:

168

169

```javascript

170

bundler.transform(uglifyify, {

171

compress: {

172

conditionals: false,

173

drop_console: true

174

},

175

mangle: {

176

reserved: ['$', 'jQuery']

177

},

178

output: {

179

comments: false

180

}

181

});

182

```

183

184

### Dead Code Elimination

185

186

Combine with envify for environment-based code elimination:

187

188

```javascript

189

// This code in your module:

190

if (process.env.NODE_ENV === 'development') {

191

module.exports = require('./development');

192

} else {

193

module.exports = require('./production');

194

}

195

196

// Command line compilation:

197

// NODE_ENV=production browserify -t envify -t uglifyify index.js -o prod.js

198

```

199

200

## Error Handling

201

202

The transform emits 'error' events for:

203

204

- **Minification Errors**: When Terser fails to parse or minify code

205

- **File Processing Errors**: When file reading or stream processing fails

206

- **Source Map Errors**: When source map parsing or generation fails

207

208

```javascript

209

const stream = fs.createReadStream('input.js')

210

.pipe(uglifyify('input.js'));

211

212

stream.on('error', (err) => {

213

console.error('Minification failed:', err.message);

214

});

215

```

216

217

## File Processing Logic

218

219

1. **Ignore Check**: Files matching ignore patterns return a passthrough stream

220

2. **Extension Filter**: JSON files and non-matching extensions are skipped

221

3. **Content Accumulation**: File content is buffered completely before processing

222

4. **Minification**: Terser processes the buffered content with provided options

223

5. **Source Map Handling**: Existing source maps are preserved and new ones generated

224

6. **Output**: Minified code is emitted with optional source map comments

225

226

## Integration Patterns

227

228

### With Other Transforms

229

230

```javascript

231

bundler

232

.transform('babelify') // First: transpile ES6+ to ES5

233

.transform('envify') // Second: replace environment variables

234

.transform(uglifyify); // Last: minify the result

235

```

236

237

### Production Builds

238

239

```javascript

240

const bundler = browserify('./src/index.js');

241

242

if (process.env.NODE_ENV === 'production') {

243

bundler.transform(uglifyify, {

244

global: true,

245

compress: {

246

drop_console: true,

247

drop_debugger: true

248

}

249

});

250

}

251

```