Skip to content

Commit

Permalink
feat(plugin-uglifyjs): added uglifyjs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Jan 30, 2019
1 parent 9c5a2e7 commit 3511120
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/plugin-uglifyjs/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
27 changes: 27 additions & 0 deletions packages/plugin-uglifyjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# wepy uglifyjs 插件

## 安装

```bash
npm install @wepy/plugin-uglifyjs uglify-js --save-dev
```

## 配置`wepy.config.js`

```javascript
const PluginUglifyjs = require('@wepy/plugin-uglifyjs');

module.exports = {
plugins: [
PluginUglifyjs({
// options
})
]
};
```

## 参数说明

你提供的配置选项 ```options``` 将传递给 ```UglifyJS```处理,有关 ```options``` 的更多详细信息,请参阅[UglifyJS文档](https://github.com/mishoo/UglifyJS2#minify-options)


45 changes: 45 additions & 0 deletions packages/plugin-uglifyjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Tencent is pleased to support the open source community by making WePY available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

const UglifyJS = require('uglify-js');

exports = module.exports = function (options = {}) {
return function () {
this.register('output-file', function ({ filename, code, encoding }) {

let ext = path.extname(filename);
if (ext !== '.js') {
return { filename, code, encoding };
}

let result = UglifyJS.minify({ [filename]: code }, options);

if (result.error) {
let pos = {
line: result.error.line,
column: result.error.col
};
throw {
handler: 'script',
error: {
filename: result.error.filename,
code: code,
type: 'error',
message: result.error.message,
title: 'uglifyjs'
},
pos: {
start: pos,
end: pos
}
};
}
});
}
};
20 changes: 20 additions & 0 deletions packages/plugin-uglifyjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@wepy/plugin-uglifyjs",
"version": "0.0.1",
"description": "wepy uglifyjs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"wepy",
"uglifyjs"
],
"author": "Gcaufy",
"license": "MIT",
"peerDependencies": {
"uglify-js": "^3.4.9"
},
"dependencies": {
}
}
1 change: 1 addition & 0 deletions packages/plugin-uglifyjs/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Todo

0 comments on commit 3511120

Please sign in to comment.