pax_global_header00006660000000000000000000000064141240117330014506gustar00rootroot0000000000000052 comment=28ec69b51429e88c661d300122b993ba270f9b53 duan602728596-gulp-terser-28ec69b/000077500000000000000000000000001412401173300162655ustar00rootroot00000000000000duan602728596-gulp-terser-28ec69b/.editorconfig000066400000000000000000000003671412401173300207500ustar00rootroot00000000000000root = true [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = false [*.md] trim_trailing_whitespace = falseduan602728596-gulp-terser-28ec69b/.eslintignore000066400000000000000000000000461412401173300207700ustar00rootroot00000000000000node_modules lib dist test/build/es.jsduan602728596-gulp-terser-28ec69b/.eslintrc.js000066400000000000000000000231571412401173300205340ustar00rootroot00000000000000module.exports = { env: { browser: true, node: true, commonjs: true, 'shared-node-browser': true, es6: true, es2017: true, es2020: true, worker: true, mocha: true, jest: true, jquery: true, serviceworker: true }, parserOptions: { ecmaFeatures: { globalReturn: true, jsx: true }, sourceType: 'module' }, overrides: [ { files: ['*.ts', '*.tsx'], parser: '@typescript-eslint/parser', parserOptions: { project: 'tsconfig.json', createDefaultProgram: true }, plugins: ['@typescript-eslint'], rules: { // Supported Rules '@typescript-eslint/explicit-function-return-type': 'error', // 函数必须返回值 '@typescript-eslint/member-delimiter-style': [ // 在接口和类型文字中强制使用一致的成员定界符样式 'error', { multiline: { delimiter: 'semi', requireLast: true }, singleline: { delimiter: 'semi', requireLast: false } } ], '@typescript-eslint/no-empty-interface': 'error', // 禁止空接口 '@typescript-eslint/no-for-in-array': 'error', // 禁止使用for-in循环遍历数组 '@typescript-eslint/type-annotation-spacing': [ // 在类型注释周围需要一致的间距 'error', { before: true, after: true, overrides: { colon: { before: false, after: true } } } ], '@typescript-eslint/typedef': [ // 需要存在类型定义 'error', { arrayDestructuring: true, arrowParameter: true, memberVariableDeclaration: true, objectDestructuring: true, parameter: true, propertyDeclaration: true, variableDeclaration: true } ], // Extension Rules '@typescript-eslint/no-array-constructor': 'error', // 禁止使用new Array(),但是可以使用new Array() 'no-use-before-define': 'off', '@typescript-eslint/no-use-before-define': 'error' // 禁止定义前使用 } }, { files: ['*.js', '*.jsx'], parser: '@babel/eslint-parser', parserOptions: { requireConfigFile: false } } ], rules: { // Possible Errors 'no-cond-assign': ['error', 'always'], // 禁止条件表达式中出现赋值操作符 'no-constant-condition': [ // 禁止在条件中使用常量表达式 'error', { checkLoops: true } ], 'no-dupe-args': 'error', // 禁止 function 定义中出现重名参数 'no-dupe-keys': 'error', // 禁止对象字面量中出现重复的 key 'no-duplicate-case': 'error', // 禁止出现重复的 case 标签 'no-ex-assign': 'error', // 禁止对 catch 子句的参数重新赋值 'no-extra-boolean-cast': 'error', // 禁止不必要的布尔转换 'no-extra-semi': 'error', // 禁止不必要的分号 'no-func-assign': 'error', // 禁止对 function 声明重新赋值 'no-inner-declarations': 'error', // 禁止在嵌套的块中出现变量声明或 function 声明 'no-invalid-regexp': 'error', // 禁止 RegExp 构造函数中存在无效的正则表达式字符串 'no-obj-calls': 'error', // 禁止把全局对象作为函数调用 'no-prototype-builtins': 'error', // 禁止直接调用 Object.prototypes 的内置属性 'no-sparse-arrays': 'error', // 禁用稀疏数组 'no-unexpected-multiline': 'error', // 禁止出现令人困惑的多行表达式 'use-isnan': 'error', // 要求使用 isNaN() 检查 NaN 'valid-typeof': 'error', // 强制 typeof 表达式与有效的字符串进行比较 // Best Practices 'block-scoped-var': 'error', // 强制把变量的使用限制在其定义的作用域范围内 curly: ['error', 'multi-line'], // 要求遵循大括号约定 'dot-location': ['error', 'property'], // 强制在点号之前和之后一致的换行 eqeqeq: ['error', 'always'], // 要求使用 === 和 !== 'no-empty-function': 'error', // 禁止出现空函数 'no-eval': 'error', // 禁用 eval() 'no-implied-eval': 'error', // 禁用隐式的eval() 'no-multi-spaces': [ // 禁止出现多个空格 'error', { ignoreEOLComments: true } ], 'no-new-func': 'error', // 禁止对 Function 对象使用 new 操作符 'no-new-wrappers': 'error', // 禁止对 String,Number 和 Boolean 使用 new 操作符 'no-param-reassign': 'error', // 禁止对 function 的参数进行重新赋值 'no-redeclare': 'error', // 禁止多次声明同一变量 'no-script-url': 'error', // 禁止使用 javascript: url 'no-self-assign': 'error', // 禁止自我赋值 'no-self-compare': 'error', // 禁止自身比较 'no-sequences': 'error', // 禁用逗号操作符 'require-await': 'error', // 禁止使用不带 await 表达式的 async 函数 // Variables 'no-delete-var': 'error', // 禁止删除变量 'no-use-before-define': 'error', // 禁止定义前使用 // Node.js and CommonJS 'no-new-require': 'error', // 禁止调用 require 时使用 new 操作符 // Stylistic Issues 'array-bracket-spacing': 'error', // 强制数组方括号中使用一致的空格 'block-spacing': 'error', // 禁止或强制在代码块中开括号前和闭括号后有空格 'brace-style': 'error', // 强制在代码块中使用一致的大括号风格 'comma-dangle': ['error', 'never'], // 要求或禁止末尾逗号 'comma-spacing': 'error', // 强制在逗号前后使用一致的空格 indent: ['error', 2, { SwitchCase: 1 }], // 强制使用一致的缩进 'jsx-quotes': 'error', // 强制在 JSX 属性中一致地使用双引号或单引号 'key-spacing': [ // 强制在对象字面量的属性中键和值之间使用一致的间距 'error', { beforeColon: false, afterColon: true, mode: 'strict' } ], 'keyword-spacing': 'error', // 强制在关键字前后使用一致的空格 'no-mixed-operators': 'error', // 禁止混合使用不同的操作符 'no-mixed-spaces-and-tabs': 'error', // 禁止空格和 tab 的混合缩进 'no-multiple-empty-lines': [ 'error', { max: 2, maxEOF: 0, maxBOF: 0 } ], 'no-new-object': 'error', // 禁用 Object 的构造函数 'no-tabs': 'error', // 禁用 tab 'no-trailing-spaces': [ // 禁用行尾空格 'error', { skipBlankLines: true, ignoreComments: true } ], 'no-whitespace-before-property': 'error', // 禁止属性前有空白 'object-curly-spacing': ['error', 'always'], // 强制在大括号中使用一致的空格 'operator-linebreak': ['error', 'before'], // 强制操作符使用一致的换行符 'padding-line-between-statements': [ // 要求或禁止在语句间填充空行 'error', { blankLine: 'always', prev: ['const', 'let', 'var'], next: '*' }, { blankLine: 'any', prev: ['const', 'let', 'var'], next: ['const', 'let', 'var'] }, { blankLine: 'always', prev: '*', next: 'return' } ], quotes: ['error', 'single', { avoidEscape: true }], // 强制使用一致的反勾号、双引号或单引号 semi: ['error', 'always'], // 要求或禁止使用分号代替 ASI 'space-before-blocks': ['error', 'always'], // 强制在块之前使用一致的空格 'space-before-function-paren': [ // 强制在 function 的左括号之前使用一致的空格 'error', { anonymous: 'never', named: 'never', asyncArrow: 'always' } ], 'space-infix-ops': 'error', // 要求操作符周围有空格 'space-unary-ops': ['error', { words: true }], // 要求或禁止在一元操作符之前或之后存在空格 'spaced-comment': 'error', // 强制在注释中 // 或 /* 使用一致的空格 // ECMAScript 6 'arrow-parens': ['error', 'always'], // 要求箭头函数的参数使用圆括号 'arrow-spacing': [ // 强制箭头函数的箭头前后使用一致的空格 'error', { before: true, after: true } ], 'constructor-super': 'error', // 要求在构造函数中有 super() 的调用 'no-this-before-super': 'error', // 禁止在构造函数中,在调用 super() 之前使用 this 或 super 'no-var': 'error', // 要求使用 let 或 const 而不是 var 'prefer-const': 'error', // 要求使用 const 声明那些声明后不再被修改的变量 'object-shorthand': 'error', // 要求或禁止对象字面量中方法和属性使用简写语法 'require-yield': 'error', // 要求 generator 函数内有 yield 'template-curly-spacing': ['error', 'always'] // 要求或禁止模板字符串中的嵌入表达式周围空格的使用 } };duan602728596-gulp-terser-28ec69b/.gitignore000066400000000000000000000002451412401173300202560ustar00rootroot00000000000000# node node_modules npm-debug.log package-lock.json pnpm-lock.yaml # yarn yarn.lock yarn-error.log # webstorm .idea lib test/build/es.js test/build/es.js.map distduan602728596-gulp-terser-28ec69b/.npmignore000066400000000000000000000003651412401173300202700ustar00rootroot00000000000000# node node_modules npm-debug.log package-lock.json pnpm-lock.yaml # yarn yarn.lock yarn-error.log # webstorm .idea bin src test gulpfile.js .eslintignore .eslintrc.yml .eslintrc.js .gitignore .babelrc .editorconfig .flowconfig tsconfig.jsonduan602728596-gulp-terser-28ec69b/LICENSE000066400000000000000000000020521412401173300172710ustar00rootroot00000000000000MIT License Copyright (c) 2017 段昊辰 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. duan602728596-gulp-terser-28ec69b/README.md000066400000000000000000000027361412401173300175540ustar00rootroot00000000000000# gulp-terser Gulp plugin, compressed es6+ code. ## Install ``` $ npm install gulp-terser --save-dev ``` or ``` $ yarn add gulp-terser --dev ``` ## How to use ```javascript const gulp = require('gulp'); const terser = require('gulp-terser'); function es(){ return gulp.src('./src/index.js') .pipe(terser()) .pipe(gulp.dest('./build')); } exports.default = es; ``` ## Options Terser configuration can be viewed [https://github.com/terser-js/terser#minify-options](https://github.com/terser-js/terser#minify-options). ```javascript const gulp = require('gulp'); const terser = require('gulp-terser'); function es(){ return gulp.src('./src/index.js') .pipe(terser({ keep_fnames: true, mangle: false })) .pipe(gulp.dest('./build')); } exports.default = es; ``` ## Use a 3rd-party version of minifier ```javascript const gulp = require('gulp'); const terser = require('terser'); const gulpTerser = require('gulp-terser'); function es(){ return gulp.src('./src/index.js') .pipe(gulpTerser({}, terser.minify)) .pipe(gulp.dest('./build')); } exports.default = es; ``` ## Use sourcemaps You can use sourcemaps like this: ```javascript const gulp = require('gulp'); const sourcemaps = require('gulp-sourcemaps'); const terser = require('gulp-terser'); function es(){ return gulp.src('./src/**/*.js') .pipe(sourcemaps.init()) .pipe(terser()) .pipe(sourcemaps.write('./')) .pipe(gulp.dest('./build')); } exports.default = es; ```duan602728596-gulp-terser-28ec69b/package.json000066400000000000000000000033321412401173300205540ustar00rootroot00000000000000{ "name": "gulp-terser", "version": "2.1.0", "description": "gulp plugin, compressed es6+ code.", "scripts": { "start": "npm run clean && tsc --watch", "build": "npm run clean && tsc", "clean": "rimraf ./dist", "test": "mocha \"./test/build/**/*.js\"", "test:gulp": "gulp --gulpfile ./test/gulpfile.js", "eslint": "eslint ./ --fix", "eslint:no-fix": "eslint ./" }, "main": "dist/cjs.js", "license": "MIT", "keywords": [ "gulp", "gulpplugin", "uglify-es", "terser" ], "author": { "name": "段昊辰", "email": "duanhaochen@126.com", "url": "https://github.com/duan602728596" }, "repository": { "type": "git", "url": "git://github.com/duan602728596/gulp-terser.git" }, "dependencies": { "plugin-error": "^1.0.1", "terser": "^5.9.0", "through2": "^4.0.2", "vinyl-sourcemaps-apply": "^0.2.1" }, "devDependencies": { "@babel/core": "^7.15.5", "@babel/eslint-parser": "^7.15.7", "@types/node": "^14", "@types/through2": "^2", "@typescript-eslint/eslint-plugin": "^4.31.2", "@typescript-eslint/parser": "^4.31.2", "chai": "^4.2.0", "commitizen": "^4.2.4", "cz-conventional-changelog": "^3.3.0", "eslint": "^7.32.0", "gulp": "^4.0.2", "gulp-plumber": "^1.2.1", "gulp-sourcemaps": "^3.0.0", "husky": "^4.3.8", "lint-staged": "^11.1.2", "mocha": "^8.4.0", "rimraf": "^3.0.2", "typescript": "^4.4.3" }, "engines": { "node": ">=10" }, "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "**/*.{js,jsx,ts,tsx}": "eslint" }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } } } duan602728596-gulp-terser-28ec69b/src/000077500000000000000000000000001412401173300170545ustar00rootroot00000000000000duan602728596-gulp-terser-28ec69b/src/cjs.ts000066400000000000000000000001511412401173300202000ustar00rootroot00000000000000import gulpTerser from './gulpTerser'; module.exports = gulpTerser; module.exports.default = gulpTerser;duan602728596-gulp-terser-28ec69b/src/gulpTerser.ts000066400000000000000000000046221412401173300215640ustar00rootroot00000000000000import type { Transform } from 'stream'; import { obj, TransformCallback } from 'through2'; import { minify, MinifyOptions, MinifyOutput } from 'terser'; import * as PluginError from 'plugin-error'; import * as applySourceMap from 'vinyl-sourcemaps-apply'; const PLUGIN_NAME: string = 'terser'; /** * @param { MinifyOptions } gulpTerserOptions: gulp-terser configuration * @param { typeof minify | undefined } customMinifyFunc:custom minify function */ function gulpTerser(gulpTerserOptions: MinifyOptions = {}, customMinifyFunc: typeof minify | undefined): Transform { const minifyFunc: typeof minify = customMinifyFunc ?? minify; return obj(async function(chunk: any, enc: BufferEncoding, callback: TransformCallback): Promise { if (chunk.isStream()) { this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!')); return callback(); } if (chunk.isBuffer()) { try { const terserOptions: MinifyOptions = { ...gulpTerserOptions }; // SourceMap configuration if (chunk.sourceMap) { if (!terserOptions.sourceMap || terserOptions.sourceMap === true) { terserOptions.sourceMap = {}; } terserOptions.sourceMap.filename = chunk.sourceMap.file; } const chunkString: string = chunk.contents.toString('utf8'); let build: string | string[] | { [file: string]: string } = {}; // gulp version compatibility if (('sourceMap' in chunk) && ('file' in chunk.sourceMap)) { build[chunk.sourceMap.file] = chunkString; } else { build = chunkString; } // Compressed code (terser5 is asynchronous, terser4 is synchronous) const minifyOutput: MinifyOutput = await minifyFunc(build, terserOptions); if ('error' in minifyOutput) { throw new Error(minifyOutput['error']['message']); } // Buffer if (minifyOutput.code) { chunk.contents = ('from' in Buffer) ? Buffer.from(minifyOutput.code) : new Buffer(minifyOutput.code); } // Output source-map if (chunk.sourceMap && minifyOutput.map) { applySourceMap(chunk, minifyOutput.map); } this.push(chunk); return callback(); } catch (err) { this.emit('error', new PluginError(PLUGIN_NAME, err)); return callback(); } } }); } export default gulpTerser;duan602728596-gulp-terser-28ec69b/test/000077500000000000000000000000001412401173300172445ustar00rootroot00000000000000duan602728596-gulp-terser-28ec69b/test/build/000077500000000000000000000000001412401173300203435ustar00rootroot00000000000000duan602728596-gulp-terser-28ec69b/test/build/es.test.js000066400000000000000000000026271412401173300222750ustar00rootroot00000000000000const fs = require('fs'); const path = require('path'); const { expect } = require('chai'); const { add, array, pow, promise, asyncFn } = require('./es'); describe('es.js', () => { // add describe('add()', () => { it('add(1, 2) should return 15', function() { expect(add(1, 2)).to.be.equal(15); }); it('add(-18, 3) should return -3', function() { expect(add(-18, 3)).to.be.equal(-3); }); }); // array describe('array()', () => { it('array([1, 3, 5]) should return 9', function() { expect(array([1, 3, 5])).to.be.equal(9); }); it('array([-11, 35, 8]) should return 32', function() { expect(array([-11, 35, 8])).to.be.equal(32); }); }); // pow describe('pow()', () => { it('pow(2, 4) should return 16', function() { expect(pow(2, 4)).to.be.equal(16); }); it('pow(3, 3) should return 27', function() { expect(pow(3, 3)).to.be.equal(27); }); }); // promise describe('promise()', () => { it('promise(12) should return 15', async function() { const r = await promise(12); expect(r).to.be.equal(15); }); }); // async describe('async()', function() { it('async', function() { asyncFn(); }); }); // sourcemaps describe('sourcemaps', function() { it('sourcemaps exist', function() { expect(fs.existsSync(path.join(__dirname, 'es.js.map'))).to.be.true; }); }); });duan602728596-gulp-terser-28ec69b/test/gulpfile.js000066400000000000000000000006071412401173300214140ustar00rootroot00000000000000const gulp = require('gulp'); const sourcemaps = require('gulp-sourcemaps'); const plumber = require('gulp-plumber'); const terser = require('../dist/cjs'); function es() { return gulp.src(['./src/es.js']) .pipe(sourcemaps.init()) .pipe(plumber()) .pipe(terser({ ecma: 8 })) .pipe(sourcemaps.write('./')) .pipe(gulp.dest('./build')); } exports.default = es;duan602728596-gulp-terser-28ec69b/test/src/000077500000000000000000000000001412401173300200335ustar00rootroot00000000000000duan602728596-gulp-terser-28ec69b/test/src/es.js000066400000000000000000000010641412401173300210010ustar00rootroot00000000000000// const function add(x, y) { const mid = 12; return x + y + 12; } // let function array(arr) { let r = 0; for (let i = 0; i < arr.length; i++) { r += arr[i]; } return r; } // arrow function const pow = (a, b) => { return a ** b; }; // Promise const promise = (number) => { return new Promise((resolve, reject) => { setTimeout(() => { resolve(number + 3); }, 1500); }); }; // async async function asyncFn() { const input = await promise(add(15, 27)); } module.exports = { add, array, pow, promise, asyncFn };duan602728596-gulp-terser-28ec69b/tsconfig.json000066400000000000000000000005111412401173300207710ustar00rootroot00000000000000{ "compilerOptions": { "alwaysStrict": true, "moduleResolution": "node", "locale": "zh-CN", "module": "commonjs", "target": "ES2018", "newLine": "LF", "resolveJsonModule": true, "strictNullChecks": true, "rootDir": "./src", "outDir": "./dist" }, "exclude": [ "node_modules" ] }