Taro framework plugin that provides JavaScript minification functionality using uglify-js
npx @tessl/cli install tessl/npm-tarojs--plugin-uglifyjs@2.2.0@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.
npm install @tarojs/plugin-uglifyjsconst uglify = require('@tarojs/plugin-uglifyjs');For ES modules:
import uglify from '@tarojs/plugin-uglifyjs';const uglify = require('@tarojs/plugin-uglifyjs');
// Basic minification
const jsContent = 'function hello() { console.log("Hello World!"); }';
const result = uglify(jsContent, {}, {
compress: true,
mangle: true
});
console.log(result.code); // Minified JavaScriptThis plugin follows the standard Taro plugin architecture as a lightweight wrapper around uglify-js:
uglifyJS.minify()The primary and only capability of this plugin is JavaScript minification through uglify-js.
/**
* Minifies JavaScript content using uglify-js
* @param content - The JavaScript content to be minified (string)
* @param file - File information/context used by Taro build system
* @param config - Configuration options passed to uglify-js.minify() (object)
* @returns Result from uglifyJS.minify() containing minified code and optional source map
*/
function uglify(content, file, config);Parameters:
content (string): The JavaScript source code to be minifiedfile: File information or context object used by the Taro build system (typically ignored by uglify-js)config (object): Configuration options passed directly to uglifyJS.minify(). Common options include:
compress (boolean|object): Enable compression with optional detailed settingsmangle (boolean|object): Enable variable name mangling with optional settingssourceMap (boolean|object): Generate source map with optional configurationoutput (object): Output formatting optionsparse (object): Parser optionsrename (boolean): Enable variable renamingReturns:
Returns the result object from uglifyJS.minify() which typically contains:
code (string): The minified JavaScript codemap (string|object): Source map data if sourceMap option is enablederror (object): Error information if minification failsUsage Examples:
const uglify = require('@tarojs/plugin-uglifyjs');
// Basic minification with default compression
const basicResult = uglify(
'function test() { console.log("Hello"); }',
{},
{ compress: true, mangle: true }
);
// Advanced minification with custom options
const advancedResult = uglify(
'var longVariableName = function() { return "value"; };',
{},
{
compress: {
drop_console: true,
dead_code: true
},
mangle: {
toplevel: true
},
output: {
comments: false
}
}
);
// With source map generation
const withSourceMap = uglify(
'function example() { return true; }',
{},
{
compress: true,
mangle: true,
sourceMap: {
filename: "example.js",
url: "example.js.map"
}
}
);
console.log(withSourceMap.code); // Minified code
console.log(withSourceMap.map); // Source mapSince this is a JavaScript package without TypeScript definitions, here are the inferred types for clarity:
interface UglifyResult {
code: string;
map?: string | object;
error?: object;
}
interface UglifyConfig {
compress?: boolean | object;
mangle?: boolean | object;
sourceMap?: boolean | object;
output?: object;
parse?: object;
rename?: boolean;
[key: string]: any; // Additional uglify-js options
}
declare function uglify(
content: string,
file: any,
config: UglifyConfig
): UglifyResult;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.
const result = uglify('invalid javascript syntax {{{', {}, {});
if (result.error) {
console.error('Minification failed:', result.error);
} else {
console.log('Minified code:', result.code);
}This plugin is designed for use within the Taro build system but can be used standalone. When used in Taro:
file parameter