From d144151c6874b8020c951735251e293047238aca Mon Sep 17 00:00:00 2001 From: imconfig Date: Sat, 17 Sep 2016 23:44:34 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=90=88=E6=88=90=E5=90=8E=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + index.js | 3 +++ lib/pack.js | 10 ++++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d10518..405fba4 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ fis 中对依赖的js 加载,尤其是异步 js,需要一个 js loader。 - `ignore` 默认为空。如果不希望部分文件被 all in one 打包,请设置 ignore 清单。 - `sourceMap` 默认为 `false`。是否生成 sourcemap. - `useTrack` 默认为 `true`. 是否在打包文件中添加track信息 + - `urglifyJs` 默认为 `false`. 是否在打包文件后使用`uglify-js`压缩js文件(注意:该属性功能与`sourceMap`等属性冲突),配置详细请参见 https://github.com/mishoo/UglifyJS2 * `processor` 默认为 `{'.html': 'html'}`, 即支持后缀是 .html 的文件,如果要支持其他后缀,请在此扩展。 diff --git a/index.js b/index.js index b38b5b9..b534520 100644 --- a/index.js +++ b/index.js @@ -160,6 +160,9 @@ rudePackager.defaultOptions = { // 生成的 resourcemap 是内联呢?还是生成 js 文件外链? useInlineMap: false, + //js文件合成后压缩 + urglifyJs: false, + loaderScripts: ['require.js', 'esl.js', 'mod.js', 'sea.js', 'system.js'] }; diff --git a/lib/pack.js b/lib/pack.js index b988201..0f24811 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -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; @@ -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) { @@ -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 = []; @@ -169,7 +170,12 @@ module.exports = function(file, resource, ret, opts) { ret.pkg[mapping.subpath] = mapping; content += pkg.isCssLike ? ('/*# sourceMappingURL=' + mapping.getUrl() + '*/') : ('//# sourceMappingURL=' + mapping.getUrl()); + //只在非debug模式下 } + if(isJsLike && opts.urglifyJs){ + content = UglifyJS.minify(content, opts.urglifyJs); + } + pkg.setContent(content); list.splice(i, 0, { From 6e494a7409107ca86084e6ec771dc2084d193188 Mon Sep 17 00:00:00 2001 From: imconfig Date: Sun, 18 Sep 2016 00:15:21 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=90=88=E6=88=90=E5=90=8E=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 9458e81..4fb1cb3 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "mocha": "^2.2.5" }, "dependencies": { - "source-map": "^0.5.6" + "source-map": "^0.5.6", + "uglify-js": "^2.4.15" } } From 15be3cecc2683d0f584d72c0bef86f6c0765abb3 Mon Sep 17 00:00:00 2001 From: imconfig Date: Sun, 18 Sep 2016 00:19:57 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=90=88=E6=88=90=E5=90=8E=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++--- lib/pack.js | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 405fba4..48c087b 100644 --- a/README.md +++ b/README.md @@ -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` @@ -146,7 +148,6 @@ fis 中对依赖的js 加载,尤其是异步 js,需要一个 js loader。 - `ignore` 默认为空。如果不希望部分文件被 all in one 打包,请设置 ignore 清单。 - `sourceMap` 默认为 `false`。是否生成 sourcemap. - `useTrack` 默认为 `true`. 是否在打包文件中添加track信息 - - `urglifyJs` 默认为 `false`. 是否在打包文件后使用`uglify-js`压缩js文件(注意:该属性功能与`sourceMap`等属性冲突),配置详细请参见 https://github.com/mishoo/UglifyJS2 * `processor` 默认为 `{'.html': 'html'}`, 即支持后缀是 .html 的文件,如果要支持其他后缀,请在此扩展。 diff --git a/lib/pack.js b/lib/pack.js index 0f24811..bf6bd07 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -173,6 +173,7 @@ module.exports = function(file, resource, ret, opts) { //只在非debug模式下 } if(isJsLike && opts.urglifyJs){ + opts.urglifyJs.fromString = true; content = UglifyJS.minify(content, opts.urglifyJs); } From ffab3ca40b664bb4bd693891210b29d7e4c929be Mon Sep 17 00:00:00 2001 From: imconfig Date: Sun, 18 Sep 2016 00:22:52 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=90=88=E6=88=90=E5=90=8E=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- lib/pack.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 48c087b..a356f5d 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ 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 + //默认`false`. 打包文件后使用`uglify-js`压缩js文件(注意:该功能与`sourceMap`等属性冲突),配置详细请参见 https://github.com/mishoo/UglifyJS2 urglifyJs: {} } }) diff --git a/lib/pack.js b/lib/pack.js index bf6bd07..83a1115 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -172,7 +172,7 @@ module.exports = function(file, resource, ret, opts) { //只在非debug模式下 } - if(isJsLike && opts.urglifyJs){ + if(isJsLike && 'object' === opts.urglifyJs){ opts.urglifyJs.fromString = true; content = UglifyJS.minify(content, opts.urglifyJs); } From bacb81264d98400691968ace53cd63033885f690 Mon Sep 17 00:00:00 2001 From: imconfig Date: Sun, 18 Sep 2016 00:25:10 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=90=88=E6=88=90=E5=90=8E=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index b534520..1df2c01 100644 --- a/index.js +++ b/index.js @@ -147,6 +147,7 @@ rudePackager.defaultOptions = { js: '', // 打包后 js 的文件路径。 includeAsyncs: false, // 可以配置成 true 用来包含异步依赖。 ignore: null // 忽略列表,可以配置部分文件不被 all in one. + urglifyJs: false, //js文件合成后压缩, 配置见 https://github.com/mishoo/UglifyJS2 }*/, // 是否捕获页面内的