使用 Gulp 缩小 PHP 文件中的内联 Javascript
Minify Inline Javascript in PHP Files with Gulp
我想做的是使用 Gulp 缩小 php 文件中的内联 javascript <script>
。原因是因为我试图压缩整体文件大小(这对我来说很重要)。 javascript 包含 php 个变量。
我从 gulp-minify-inline-scripts
插件开始,但对其进行了更改,使其能够识别 php 文件。不幸的是,除非使用 html 文件,否则我无法成功输出 javascript。
我的想法是保留 php 变量并将内容保存回 php 文件。没有编译实际的 php.
插件代码:
var path = require('path');
var gutil = require('gulp-util');
var uglify = require('uglify-js');
var through = require('through2');
var PLUGIN_NAME = 'gulp-minify-inline-scripts';
module.exports = function (options) {
return through.obj(function (file, enc, cb) {
var self = this;
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
var fileName = file.path.split(path.sep).pop();
//html file only
if (!/^\.php?$/.test(path.extname(file.path))) {
gutil.log(gutil.colors.red('[WARN] file ' + fileName + ' is not a html file'));
this.push(file);
return cb();
}
gutil.log("uglify inline scripts in html file: " + file.path);
var html = file.contents.toString('utf8');
var reg = /(<script(?![^>]*?\b(type=['"]text\/template['"]|src=["']))[^>]*?>)([\s\S]*?)<\/script>/g;
html = html.replace(reg, function (str, tagStart, attr, script) {
try {
var result = uglify.minify(script, { fromString: true });
return tagStart + result.code + '</script>';
} catch (e) {
self.emit('error', new gutil.PluginError(PLUGIN_NAME, 'uglify inline scripts error: ' + (e && e.stack)));
}
});
file.contents = new Buffer(html);
this.push(file);
cb();
});
};
PHP 内联 Javascript 压缩:
<?php
/* Start: This is a php file
?>
<script>
<?php $var = 'test'; ?>
A.config = {
test: '<?php echo $var; ?>'
};
a.visible = function (image, distance) {
var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
var bounds;
var gap;
if (!image.getBoundingClientRect) {
return false;
}
};
</script>
<?php
/* End: This is a php file
?>
对于遇到此问题的任何人,我已经根据@Cbroe 在评论中的暗示编写了一个解决方案。这真的很简单:包装 php 代码,这样它就不会破坏 jsminifier,然后在代码缩小后,删除包装的 php 字符串以释放 php 变量。您可以通过选择更好的包装字符串来进一步简化它。
我只是要小心你用它做什么,因为除了我的特定用例之外,我没有测试任何东西。
更新:我已经用了好几个月了,从来没有坏过。只需确保使用您在正则表达式中放置的相同脚本标记模式,否则它可能无法在页面中找到该标记。
新Gulp插件代码:
var path = require('path');
var gutil = require('gulp-util');
var uglify = require('uglify-js');
var through = require('through2');
var PLUGIN_NAME = 'gulp-minify-inline-scripts';
module.exports = function(options) {
return through.obj(function(file, enc, cb) {
var self = this;
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
var fileName = file.path.split(path.sep).pop();
//html file only
if (!/^\.php?$/.test(path.extname(file.path))) {
gutil.log(gutil.colors.red('[WARN] file ' + fileName + ' is not a html file'));
this.push(file);
return cb();
}
gutil.log("uglify inline scripts in html file: " + file.path);
var html = file.contents.toString('utf8');
var reg = /(<script(?![^>]*?\b(type=['"]text\/template['"]|src=["']))[^>]*?>)([\s\S]*?)<\/script>/g;
html = html.replace(reg, function(str, tagStart, attr, script) {
try {
var result = uglify.minify(script, {
fromString: true
});
var code = result.code;
code = code.split("STRIP__>\"").join('');
code = code.split("\"<__STRIP").join('');
return tagStart + code + '</script>';
} catch (e) {
self.emit('error', new gutil.PluginError(PLUGIN_NAME, 'uglify inline scripts error: ' + (e && e.stack)));
}
});
file.contents = new Buffer(html);
this.push(file);
cb();
});
};
PHP 内联 Javascript 压缩:
<?php
/* Start: This is a php file
?>
<script>
<?php $var = 'test'; ?>
A.config = {
test: "<__STRIP'<?php echo $var; ?>'STRIP__>"
};
a.visible = function (image, distance) {
var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
var bounds;
var gap;
if (!image.getBoundingClientRect) {
return false;
}
};
</script>
<?php
/* End: This is a php file
?>
我想做的是使用 Gulp 缩小 php 文件中的内联 javascript <script>
。原因是因为我试图压缩整体文件大小(这对我来说很重要)。 javascript 包含 php 个变量。
我从 gulp-minify-inline-scripts
插件开始,但对其进行了更改,使其能够识别 php 文件。不幸的是,除非使用 html 文件,否则我无法成功输出 javascript。
我的想法是保留 php 变量并将内容保存回 php 文件。没有编译实际的 php.
插件代码:
var path = require('path');
var gutil = require('gulp-util');
var uglify = require('uglify-js');
var through = require('through2');
var PLUGIN_NAME = 'gulp-minify-inline-scripts';
module.exports = function (options) {
return through.obj(function (file, enc, cb) {
var self = this;
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
var fileName = file.path.split(path.sep).pop();
//html file only
if (!/^\.php?$/.test(path.extname(file.path))) {
gutil.log(gutil.colors.red('[WARN] file ' + fileName + ' is not a html file'));
this.push(file);
return cb();
}
gutil.log("uglify inline scripts in html file: " + file.path);
var html = file.contents.toString('utf8');
var reg = /(<script(?![^>]*?\b(type=['"]text\/template['"]|src=["']))[^>]*?>)([\s\S]*?)<\/script>/g;
html = html.replace(reg, function (str, tagStart, attr, script) {
try {
var result = uglify.minify(script, { fromString: true });
return tagStart + result.code + '</script>';
} catch (e) {
self.emit('error', new gutil.PluginError(PLUGIN_NAME, 'uglify inline scripts error: ' + (e && e.stack)));
}
});
file.contents = new Buffer(html);
this.push(file);
cb();
});
};
PHP 内联 Javascript 压缩:
<?php
/* Start: This is a php file
?>
<script>
<?php $var = 'test'; ?>
A.config = {
test: '<?php echo $var; ?>'
};
a.visible = function (image, distance) {
var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
var bounds;
var gap;
if (!image.getBoundingClientRect) {
return false;
}
};
</script>
<?php
/* End: This is a php file
?>
对于遇到此问题的任何人,我已经根据@Cbroe 在评论中的暗示编写了一个解决方案。这真的很简单:包装 php 代码,这样它就不会破坏 jsminifier,然后在代码缩小后,删除包装的 php 字符串以释放 php 变量。您可以通过选择更好的包装字符串来进一步简化它。
我只是要小心你用它做什么,因为除了我的特定用例之外,我没有测试任何东西。
更新:我已经用了好几个月了,从来没有坏过。只需确保使用您在正则表达式中放置的相同脚本标记模式,否则它可能无法在页面中找到该标记。
新Gulp插件代码:
var path = require('path');
var gutil = require('gulp-util');
var uglify = require('uglify-js');
var through = require('through2');
var PLUGIN_NAME = 'gulp-minify-inline-scripts';
module.exports = function(options) {
return through.obj(function(file, enc, cb) {
var self = this;
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
var fileName = file.path.split(path.sep).pop();
//html file only
if (!/^\.php?$/.test(path.extname(file.path))) {
gutil.log(gutil.colors.red('[WARN] file ' + fileName + ' is not a html file'));
this.push(file);
return cb();
}
gutil.log("uglify inline scripts in html file: " + file.path);
var html = file.contents.toString('utf8');
var reg = /(<script(?![^>]*?\b(type=['"]text\/template['"]|src=["']))[^>]*?>)([\s\S]*?)<\/script>/g;
html = html.replace(reg, function(str, tagStart, attr, script) {
try {
var result = uglify.minify(script, {
fromString: true
});
var code = result.code;
code = code.split("STRIP__>\"").join('');
code = code.split("\"<__STRIP").join('');
return tagStart + code + '</script>';
} catch (e) {
self.emit('error', new gutil.PluginError(PLUGIN_NAME, 'uglify inline scripts error: ' + (e && e.stack)));
}
});
file.contents = new Buffer(html);
this.push(file);
cb();
});
};
PHP 内联 Javascript 压缩:
<?php
/* Start: This is a php file
?>
<script>
<?php $var = 'test'; ?>
A.config = {
test: "<__STRIP'<?php echo $var; ?>'STRIP__>"
};
a.visible = function (image, distance) {
var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
var bounds;
var gap;
if (!image.getBoundingClientRect) {
return false;
}
};
</script>
<?php
/* End: This is a php file
?>