❶ 如何使用nodejs 的 uglifyjs 压缩js文件
[js压缩]uglifyjs source.js -mc -o target.min.js
以压缩angular.js为例,可以压缩局部变量名,去掉无用的局部变量,空白符等,压缩比例接近80%,效率不错
[user_00@localhost ~]$ uglifyjs angular.js -mc -o angular.min.js
WARN: Dropping unused function isBoolean [angular.js:416,9]
WARN: Dropping unused function makeMap [angular.js:446,9]
WARN: Dropping unused function size [angular.js:488,9]
WARN: Dropping unused function isLeafNode [angular.js:523,9]
WARN: Dropping unused function argument boundTranscludeFn [angular.js:4682,27]
WARN: Dropping unused function argument rootElement [angular.js:4681,16]
WARN: Dropping unused function argument nodeList [angular.js:4680,17]
WARN: Dropping unused function argument scope [angular.js:4679,22]
WARN: Dropping unused function nodesetLinkingFn [angular.js:4678,9]
WARN: Dropping unused function argument boundTranscludeFn [angular.js:4690,27]
WARN: Dropping unused function argument rootElement [angular.js:4689,16]
WARN: Dropping unused function argument node [angular.js:4688,13]
WARN: Dropping unused function argument scope [angular.js:4687,22]
WARN: Dropping unused function argument nodesetLinkingFn [angular.js:4686,25]
WARN: Dropping unused function directiveLinkingFn [angular.js:4685,9]
WARN: Dropping unused function stripHash [angular.js:5009,9]
WARN: Dropping unused variable NG_SWITCH [angular.js:13857,4]
WARN: Dropping unused variable fns [angular.js:1536,8]
WARN: Dropping unused variable events [angular.js:1536,13]
WARN: Dropping unused function wrongMode [angular.js:3879,13]
WARN: Dropping unused function argument cause [angular.js:4799,31]
WARN: Dropping unused function argument exception [angular.js:4799,20]
WARN: Dropping unused function argument args [angular.js:8165,28]
WARN: Dropping unused function argument args [angular.js:8231,33]
WARN: Dropping unused function argument names [angular.js:9018,32]
WARN: Dropping unused function argument name [angular.js:9030,40]
WARN: Dropping unused variable value [angular.js:9230,14]
WARN: Dropping unused function argument value [angular.js:12475,36]
[user_00@localhost ~]$ ls -lh angular.*
-rw-rw-r-- 1 user_00 user_00 479K Apr 5 01:53 angular.js
-rw-rw-r-- 1 user_00 user_00 79K May 4 16:09 angular.min.js
❷ nodejs zlib 怎么把几个压缩过的文件解压拼接
我请求管用所结束httpvar http = require("http"),
zlib = require("zlib");
function getGzipped(url, callback) {
// buffer to store the streamed decompression
var buffer = [];
http.get(url, function(res) {
// pipe the response into the gunzip to decompress
var gunzip = zlib.createGunzip();
res.pipe(gunzip);
gunzip.on('data', function(data) {
// decompression chunk ready, add it to the buffer
buffer.push(data.toString())
}).on("end", function() {
// response and decompression complete, join the buffer and return
callback(null, buffer.join(""));
}).on("error", function(e) {
callback(e);
})
}).on('error', function(e) {
callback(e)
});
}
getGzipped(url, function(err, data) {
console.log(data);
});
2. 尝试添加encoding: null给传递给选项request避免载体转换字符串并保持二进制缓冲区
3. 工作示例(使用节点请求模块)gunzips响应function gunzipJSON(response){
var gunzip = zlib.createGunzip();
var json = "";
gunzip.on('data', function(data){
json += data.toString();
});
gunzip.on('end', function(){
parseJSON(json);
});
response.pipe(gunzip);
}
全码:
4. 像@Iftah说设置encoding: null 完整例(少错误处理):request = require('request');
zlib = require('zlib');
request(url, {encoding: null}, function(err, response, body){
if(response.headers['content-encoding'] == 'gzip'){
zlib.gunzip(body, function(err, dezipped) {
callback(dezipped.toString());
}
} else {
callback(body);
}
});
❸ 如何用nodejs压缩文件,生成tar包
你好,
我不知道如何直接使用Node.js代码逻辑生成tar包,但我可以给你提供一个使用Node.js调用Linux命令生成tar包的方法。
首先,在npm中有一个叫做shelljs的包,在项目中安装它:
npmi--saveshelljs
然后,在执行打包任务的js文件中使用它:
varshell=require('shelljs');
if(shell.exec('tar-zvcf文件名.tar.gz被打包的文件(夹)').code!==0){
shell.echo('Error:...');
shell.exit(1);
}
这样就可以在当前目录生成一个“文件名.tar.gz”的压缩包了,而“文件名”和“被打包的文件(夹)”都可以通过参数的形式进行传递。
好了,更多信息可以github上搜索shelljs,希望能解决你的问题。
❹ 如何使用webpack打包ES6的Nodejs后台程序
使用webpack跟后台是否用nodejs无关,
因为webpack在执行打包压缩的时候是依赖nodejs
❺ 如何用 webpack 打包 node
使用webpack跟后台是否用nodejs无关,因为webpack在执行打包压缩的时候是依赖nodejs的,没有nodejs就不能使用webpack,就好比要使用电灯,首先必须得有电流,而电流是需要发动机来发电的。
❻ 请教关于webpack压缩nodejs代码的问题
webpack 自带了一个压缩插件 UglifyJsPlugin,只需要在配置文件中引入即可。 { plugins: [ new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }) ] } 加入了这个插件之后,编译的速度会明显变慢,所以一般只在生产环境启
❼ 如何用nodejs实现http压缩传输
推荐一个中间件,叫compression
使用见https://www.npmjs.com/package/compression
下面这个use中间件的语句写在所有中间件的前面就行了,抓包可看到有个gzip传输
app.use(compression());
❽ nodejs版本的脚本压缩和compo工具怎么使用
一、下载并安装NodeJS for Windows 0.6.5 或进入NodeJS官网下载最新版本。
二、查看版本:
运行cmd
node -v
npm -v
如果上诉两个命令出错,请打开系统环境变量Path,直接点确定。再试。
三、安装UglifyJS 这里我们安装最新的版本,具体版本可在npm官网查看,uglify-js版本页面。
npm install uglify-js -g
然后等待命令完成,这个过程时间可能有点长。
如果出错请删除
C:\Users\Administrator\AppData\Roaming\npm 内相关内容
C:\Users\Administrator\AppData\Roaming\npm-cache 内相关内容
C:\Users\Administrator 下的错误信息文件
然后再次运行
四、修改配置
上述安装后自动生成的配置文件是不可直接使用的,我们需要手工去修改。
打开C:\Users\Administrator\AppData\Roaming\npm\uglifyjs.cmd
内容为:
:: Created by npm, please don't edit manually.
"%~dp0\.\node_moles\uglify-js\bin\uglifyjs" %*
修改为:
:: Created by npm, please don't edit manually.
@IF EXIST "%~dp0"\"node.exe" (
"%~dp0"\"node.exe" "%~dp0\.\node_moles\uglify-js\bin\uglifyjs" %*
) ELSE (
node "%~dp0\.\node_moles\uglify-js\bin\uglifyjs" %*
)
五、使用uglifyjs对javascript进行压缩美化
压缩:
uglifyjs f.js > f.min.js
美化:
uglifyjs -b f.min.js > f.b.js
❾ 如何利用node进行js css合并压缩
gulp是基于Node.js的前端构建工具。所以首先需要安装nodejs,安装nodejs。
完成nodejs安装之后,需要使用npm安装gulp。
先安装全局gulp
npm install -g gulp然后在项目根目录下安装本地gulp。
此时项目根目录下会多出下面这个文件夹 node_moles
好的,现在gulp已经安装完成了,但是gulp本身不提供js压缩合并等功能,需要使用gulp的相关插件。目前只需要完成js压缩合并和css文件压缩的功能,先安装相应的插件:
1.css压缩 gulp-minify-css
2.js压缩 gulp-uglify
3.js合并 gulp-concat
由于压缩之前需要对js代码进行代码检测,压缩完成之后需要加上min的后缀,我们还需要安装另外两个插件: