Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allInOne功能扩展,js文件合并后压缩 #73

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ fis 中对依赖的js 加载,尤其是异步 js,需要一个 js loader。
},
css: function (file) {
return "/static/css/" + file.filename + "_aio.css";
}
}
},
//默认`false`. 打包文件后使用`uglify-js`压缩js文件(注意:该功能与`sourceMap`等属性冲突),配置详细请参见 https://github.com/mishoo/UglifyJS2
urglifyJs: {}
}
})
```
- `css` all in one 打包后, css 文件的路径规则。默认为 `pkg/${filepath}_aio.css`
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ rudePackager.defaultOptions = {
js: '', // 打包后 js 的文件路径。
includeAsyncs: false, // 可以配置成 true 用来包含异步依赖。
ignore: null // 忽略列表,可以配置部分文件不被 all in one.
urglifyJs: false, //js文件合成后压缩, 配置见 https://github.com/mishoo/UglifyJS2
}*/,

// 是否捕获页面内的 <script src="xxx"> 资源
Expand Down
10 changes: 8 additions & 2 deletions lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
var common = require('./common.js');
var SourceMap = require('source-map');
var UglifyJS = require('uglify-js');
var _ = fis.util;
var rSourceMap = /(?:\/\/\#\s*sourceMappingURL[^\r\n\'\"]*|\/\*\#\s*sourceMappingURL[^\r\n\'\"]*\*\/)(?:\r?\n|$)/ig;

Expand Down Expand Up @@ -38,7 +39,7 @@ module.exports = function(file, resource, ret, opts) {
});
}

pack(resource.js, opts.js || 'pkg/${filepath}_aio.js', opts.sourceMap);
pack(resource.js, opts.js || 'pkg/${filepath}_aio.js', opts.sourceMap, 'js');
pack(resource.css, opts.css || 'pkg/${filepath}_aio.css', opts.sourceMap);

function isIgnored(item) {
Expand Down Expand Up @@ -70,7 +71,7 @@ module.exports = function(file, resource, ret, opts) {
return hitted;
}

function pack(list, fileTpl, sourceMap) {
function pack(list, fileTpl, sourceMap, isJsLike) {
var index = 1;
var i = 0;
var unpacked = [];
Expand Down Expand Up @@ -168,8 +169,13 @@ module.exports = function(file, resource, ret, opts) {
mapping.setContent(generater.toString());
ret.pkg[mapping.subpath] = mapping;
content += pkg.isCssLike ? ('/*# sourceMappingURL=' + mapping.getUrl() + '*/') : ('//# sourceMappingURL=' + mapping.getUrl());
}

if(isJsLike && 'object' === opts.urglifyJs){
opts.urglifyJs.fromString = true;
content = UglifyJS.minify(content, opts.urglifyJs);
}

pkg.setContent(content);

list.splice(i, 0, {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"mocha": "^2.2.5"
},
"dependencies": {
"source-map": "^0.5.6"
"source-map": "^0.5.6",
"uglify-js": "^2.4.15"
}
}