[英]How to minify/uglify document and window properties and methods
我正在尋找一種方法來縮小這樣的代碼:
setTimeout(function() {
document.getElementById('test').innerText = 'Hello World!';
}, 1000);
像這樣(減去空格和新行):
(function(a,b){
a(function(){
b('test').innerText='Hello World!';
}, 1000);
})(setTimeout, document.getElementById)
使用像 UglifyJS 或類似的自動工具。 從文檔來看,它似乎不是這樣做的選擇。
編輯:看到這樣的代碼很常見:
(function (window, document, undefined) {
// code here
})(window, document);
這樣做是為了提高性能並使代碼對壓縮器更友好,所以我想知道為什么沒有在更深層次上這樣做。
使用uglify-js (用 3.14.5 版測試過,但它也適用於版本 2),您可以使用--enclose
選項:
npx uglify-js --mangle --enclose setTimeout,document:setTimeout,document test.js --output test2.js
給出以下輸出:
(function(e,t){e(function(){t.getElementById("test").innerText="Hello World!"},1e3)})(setTimeout,document);
不幸的是,它不能替換像document.getElementById
這樣的表達式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.