Skip to content

Commit

Permalink
Merge pull request gulp-community#8 from A-312/work-v5
Browse files Browse the repository at this point in the history
gulp-terser-js 5.0.0
  • Loading branch information
A-312 committed Aug 13, 2019
2 parents c64dc5f + 437d822 commit 8a510fb
Show file tree
Hide file tree
Showing 18 changed files with 377 additions and 142 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage
node_modules
temp
test/expect
test/fixtures
.output
13 changes: 13 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root: true
extends: standard
rules:
no-param-reassign: error
no-shadow: error
space-before-function-paren:
- error
- anonymous: never
named: never
asyncArrow: always
no-multiple-empty-lines:
- error
- { "max": 2 }
2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
node_modules
node_modules/
*.sublime-workspace
*.sublime-project
package-lock.json
.nyc_output/
.output/
66 changes: 60 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
sudo: false

language: node_js
node_js:
- "8.0.0"
- "10.0.0"
- "11.0.0"
- "11.6.0"
- "8.10"
- "8.15"
- "9.11"
- "lts/*"
matrix:
fast_finish: true
sudo: false
cache:
directories:
- node_modules
before_install:
# Configure npm
- |
# Skip updating shrinkwrap / lock
npm config set shrinkwrap false
# Setup Node.js version-specific dependencies
- |
# eslint for linting
# - remove on Node.js < 6
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \
grep -E '^eslint(-|$)' | \
xargs npm rm --save-dev
fi
- |
# mocha for testing
# - use 3.x for Node.js < 4
# - use 5.x for Node.js < 6
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 4 ]]; then
npm install --save-dev mocha@3.5.3
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
npm install --save-dev mocha@5.2.0
fi
- |
# supertest for http calls
# - use 2.0.0 for Node.js < 4
# - use 3.4.2 for Node.js < 6
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 4 ]]; then
npm install --save-dev supertest@2.0.0
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
npm install --save-dev supertest@3.4.2
fi
# Update Node.js modules
- |
# Prune and rebuild node_modules
if [[ -d node_modules ]]; then
npm prune
npm rebuild
fi
script:
# Run test script
- npm run test-ci
# Run linting
- |
# Run linting, depending on eslint install
if [[ -n "$(npm -ps ls eslint)" ]]; then
npm run-script lint
fi
after_script:
- npm run coverage
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [5.0.0] - 2019-08-14
### Changed

- Update dependencies version (Terser is now on 4.1.4) #8
- Add test, coverage in travis #8


## [4.1.1] - 2019-08-13
### Fix

- Fix printError and improve readme(caused by #3) #5

### Added

- Add API for printError: `require('gulp-terser-js').printError` #5


## [4.1.0] - 2019-08-09
### Added

- Add printError #3

[5.0.0]: https://github.com/A-312/gulp-terser-js/releases/tag/5.0.0
[4.1.1]: https://github.com/A-312/gulp-terser-js/releases/tag/4.1.1
[4.1.0]: https://github.com/A-312/gulp-terser-js/releases/tag/4.1.0
34 changes: 9 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,10 @@ Source:[Why choose terser?](https://github.com/terser-js/terser/blob/master/READ

## Why choose `gulp-terser-js`

This plugins display formatted error:
This plugin displays formatted error:

![error screenshot](https://i.imgur.com/eZUpLmB.png)

## Information

<table>
<tr>
<td>Package</td><td>gulp-terser-js</td>
</tr>
<tr>
<td>Description</td>
<td>Terser-js plugin for gulp</td>
</tr>
<tr>
<td>Node Version</td>
<td>Recommanded >= 8.0.0</td>
</tr>
<tr>
<td>Terser Version</td>
<td>Recommanded v3.14+</td>
</tr>
<tr>
<td>Gulp Version</td>
<td>3.x</td>
</tr>
</table>

## Installation

```
Expand Down Expand Up @@ -124,3 +100,11 @@ function printLESSError(error) {
this.emit('end')
}
```

## Version

| Description | gulp-terser-js |
| -------------- | -------------- |
| Node Version | >= 8.10.0 |
| Terser Version | 4.1.4+ |
| Gulp Version | >= 4.X |
89 changes: 89 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const through2 = require('through2')
const assign = require('object-assign')
const PluginError = require('plugin-error')
const applySourceMap = require('vinyl-sourcemaps-apply')
const os = require('os')

const terser = require('terser')

function gulpTerser(options) {
// Mixes in default options.
const opts = assign({}, {}, options)

return through2.obj(function(file, enc, next) {
if (file.isNull()) {
return next(null, file)
}

if (file.isStream()) {
return next(new PluginError('gulp-terser', 'Streaming not supported'))
}

const str = file.contents.toString()
let build = {}

// Bootstrap source maps
if (file.sourceMap) {
opts.sourceMap = {}
opts.sourceMap.filename = file.sourceMap.file
}

if (file.sourceMap && file.sourceMap.file) {
build[file.sourceMap.file] = str
} else {
build = str
}
const res = terser.minify(build, opts)

if (res.error) {
res.error.filePath = file.path
res.error.fileContent = (typeof build === 'string') ? build : build[res.error.filename]

return next(printError(new PluginError('gulp-terser', res.error)))
}

file.contents = new (Buffer.from || Buffer)(res.code) // eslint-disable-line node/no-deprecated-api

if (file.sourceMap && res.map) {
applySourceMap(file, res.map)
}

return next(null, file)
})
}

function alignLine(num, text, longer, maxsize) {
let maxlength = process.stdout.columns - 1
const spaces = num + Array(longer - (num + '').length).join(' ')
maxlength -= spaces.length + 3
console.log('\x1B[36m' + spaces + ' |\x1B[00m ' + text.slice(0, (maxsize < maxlength) ? maxlength : maxsize))
}

function printError(error) {
if (!error.fileContent || (error.line === undefined && error.col === undefined)) {
return console.error(error.stack || error)
}

const fileContent = error.fileContent
const lines = fileContent.replace(/\t/g, ' ').split(os.EOL)
const more = (error.line + ' ').length
const col = error.col + 1
const pos = (more + 2) + fileContent.split(os.EOL)[error.line - 1].replace(/[^\t]/g, '').length * 3 + parseInt(col)

console.log(`\nError with ${error.plugin} :`)
for (let i = error.line - 5; i < error.line; i++) {
if (lines[i]) {
alignLine(i + 1, lines[i], more, pos)
}
}

console.log(Array(pos).join(' ') + '\x1B[91m^\x1B[00m')
console.log('(' + error.name + ') ' + error.message + ' (line : \x1B[96m' + error.line + '\x1B[00m, col : \x1B[96m' + col + '\x1B[00m).')
console.log(`in : ${error.filePath}\n`)

return error
}

gulpTerser.printError = printError

module.exports = gulpTerser
87 changes: 0 additions & 87 deletions index.js

This file was deleted.

Loading

0 comments on commit 8a510fb

Please sign in to comment.