Taro framework plugin that provides JavaScript minification functionality using uglify-js
npx @tessl/cli install tessl/npm-tarojs--plugin-uglifyjs@2.2.00
# @tarojs/plugin-uglifyjs
1
2
@tarojs/plugin-uglifyjs is a Taro framework plugin that provides JavaScript minification functionality using the uglify-js library. It serves as a simple wrapper around uglify-js minification capabilities, designed to integrate seamlessly with the Taro build system to compress JavaScript files during the build process.
3
4
## Package Information
5
6
- **Package Name**: @tarojs/plugin-uglifyjs
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install @tarojs/plugin-uglifyjs`
10
- **Dependencies**: uglify-js ^3.3.24
11
12
## Core Imports
13
14
```javascript
15
const uglify = require('@tarojs/plugin-uglifyjs');
16
```
17
18
For ES modules:
19
20
```javascript
21
import uglify from '@tarojs/plugin-uglifyjs';
22
```
23
24
## Basic Usage
25
26
```javascript
27
const uglify = require('@tarojs/plugin-uglifyjs');
28
29
// Basic minification
30
const jsContent = 'function hello() { console.log("Hello World!"); }';
31
const result = uglify(jsContent, {}, {
32
compress: true,
33
mangle: true
34
});
35
36
console.log(result.code); // Minified JavaScript
37
```
38
39
## Architecture
40
41
This plugin follows the standard Taro plugin architecture as a lightweight wrapper around uglify-js:
42
43
- **Single Function Export**: The module exports one function that directly wraps `uglifyJS.minify()`
44
- **Build Integration**: Designed for integration into Taro's multi-platform build pipeline
45
- **Pass-through API**: Configuration options are passed directly to uglify-js without modification
46
- **Zero Additional Logic**: No custom error handling or validation beyond uglify-js behavior
47
48
## Capabilities
49
50
### JavaScript Minification
51
52
The primary and only capability of this plugin is JavaScript minification through uglify-js.
53
54
```javascript { .api }
55
/**
56
* Minifies JavaScript content using uglify-js
57
* @param content - The JavaScript content to be minified (string)
58
* @param file - File information/context used by Taro build system
59
* @param config - Configuration options passed to uglify-js.minify() (object)
60
* @returns Result from uglifyJS.minify() containing minified code and optional source map
61
*/
62
function uglify(content, file, config);
63
```
64
65
**Parameters:**
66
67
- `content` (string): The JavaScript source code to be minified
68
- `file`: File information or context object used by the Taro build system (typically ignored by uglify-js)
69
- `config` (object): Configuration options passed directly to `uglifyJS.minify()`. Common options include:
70
- `compress` (boolean|object): Enable compression with optional detailed settings
71
- `mangle` (boolean|object): Enable variable name mangling with optional settings
72
- `sourceMap` (boolean|object): Generate source map with optional configuration
73
- `output` (object): Output formatting options
74
- `parse` (object): Parser options
75
- `rename` (boolean): Enable variable renaming
76
77
**Returns:**
78
79
Returns the result object from `uglifyJS.minify()` which typically contains:
80
81
- `code` (string): The minified JavaScript code
82
- `map` (string|object): Source map data if `sourceMap` option is enabled
83
- `error` (object): Error information if minification fails
84
85
**Usage Examples:**
86
87
```javascript
88
const uglify = require('@tarojs/plugin-uglifyjs');
89
90
// Basic minification with default compression
91
const basicResult = uglify(
92
'function test() { console.log("Hello"); }',
93
{},
94
{ compress: true, mangle: true }
95
);
96
97
// Advanced minification with custom options
98
const advancedResult = uglify(
99
'var longVariableName = function() { return "value"; };',
100
{},
101
{
102
compress: {
103
drop_console: true,
104
dead_code: true
105
},
106
mangle: {
107
toplevel: true
108
},
109
output: {
110
comments: false
111
}
112
}
113
);
114
115
// With source map generation
116
const withSourceMap = uglify(
117
'function example() { return true; }',
118
{},
119
{
120
compress: true,
121
mangle: true,
122
sourceMap: {
123
filename: "example.js",
124
url: "example.js.map"
125
}
126
}
127
);
128
129
console.log(withSourceMap.code); // Minified code
130
console.log(withSourceMap.map); // Source map
131
```
132
133
## Types
134
135
Since this is a JavaScript package without TypeScript definitions, here are the inferred types for clarity:
136
137
```typescript { .api }
138
interface UglifyResult {
139
code: string;
140
map?: string | object;
141
error?: object;
142
}
143
144
interface UglifyConfig {
145
compress?: boolean | object;
146
mangle?: boolean | object;
147
sourceMap?: boolean | object;
148
output?: object;
149
parse?: object;
150
rename?: boolean;
151
[key: string]: any; // Additional uglify-js options
152
}
153
154
declare function uglify(
155
content: string,
156
file: any,
157
config: UglifyConfig
158
): UglifyResult;
159
```
160
161
## Error Handling
162
163
Error handling is delegated entirely to uglify-js. If minification fails, the returned result object will contain an `error` property with details about the failure. The plugin does not add any additional error handling or validation.
164
165
```javascript
166
const result = uglify('invalid javascript syntax {{{', {}, {});
167
if (result.error) {
168
console.error('Minification failed:', result.error);
169
} else {
170
console.log('Minified code:', result.code);
171
}
172
```
173
174
## Integration Notes
175
176
This plugin is designed for use within the Taro build system but can be used standalone. When used in Taro:
177
178
- The plugin integrates into the minification phase of the build process
179
- File context information is passed through the `file` parameter
180
- Configuration is typically provided by Taro's build configuration
181
- The plugin helps reduce bundle sizes for multi-platform Taro applications targeting web, native apps, and mini-programs