在实现一些新功能之前,我一直在审查过去项目的代码。目标之一是通过将代码迁移到 ES6 模块来简化代码的管理。这一切进展顺利,让生活变得更加轻松。
但是,在构建最终应用程序的过程中,我们一直在使用 Google 的 Closure Compiler 来缩小代码。这运行良好,但现在......
拿这个示例 HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML file to test compilation of JS modules</title>
</head>
<body>
<script src="JSModule1.js" type="module"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并添加这两个模块
JSModule1.js
import {Thing} from './JSModule2.js'
let item = new Thing('Something');
let otherItem = new Thing();
item.speak();
otherItem.speak();
Run Code Online (Sandbox Code Playgroud)
JSModule2.js
function Thing(word) {
let words = (word || "I've nothing to say.");
this.speak = function() {
console.log(words);
}
}
export {Thing}
Run Code Online (Sandbox Code Playgroud)
如果我尝试编译,JSModule1.js我会收到一个错误:
JSC_JS_MODULE_LOAD_WARNING: Failed to load …
我正在使用 Angular 9,我有一些这样的代码:
(features.ts, autogenerated:)
// AUTO-GENERTATED FILE. DO NOT EDIT!
export const Features = {
// Whether to reveal our Secret New Feature to the world
ENABLE_SECRET_FEATURE: 1
};
(mycode.ts, app code)
import { Features } from 'generated/features.ts';
function doSomething() {
if (Features.ENABLE_SECRET_FEATURE) {
doAIBlockChainARThing();
} else {
doSomeBoringOldCRUDThing();
}
}
Run Code Online (Sandbox Code Playgroud)
我希望发出的代码是
function doSomething() {
doAIBlockChainARThing();
}
Run Code Online (Sandbox Code Playgroud)
或者
function doSomething() {
doSomeBoringOldCRUDThing();
}
Run Code Online (Sandbox Code Playgroud)
但不是两者兼而有之。
是否有调用ng build它会发生这种情况?我知道 uglify 可以做到这一点,而 Closure Compiler 当然可以。我目前的调用是:ng build --aot=true --stats-json --buildOptimizer=true --optimization=true …
我有一个普通的 javascript 项目,它使用 ClosureCompiler 生成缩小的 js 文件。该文件是一个 SDK,人们可以将其包含在自己的网站中。我需要计算 SHA-256 哈希值,但事实证明它比应有的更加棘手。
我想要的是:
const data = "test"
const hashedData = SHA256(data);
Run Code Online (Sandbox Code Playgroud)
要求:
我尝试过的:
Webcrypto.subtle:这返回一个 Promise,而不是一个字符串。它需要我使我的 SHA256 函数异步。这将需要大量的重新架构。
SJCL:不使用闭包编译器进行编译。
我承认我问过一个问题,为什么Closure Compiler不会缩短几天前一见钟情的某些代码,但是这个原因并不适用于这种情况,我不确定为什么它没有缩短这里.
我的代码是:
var a = 0;
function b() {
return a++ >= 3;
}
Run Code Online (Sandbox Code Playgroud)
现在有预增量和后增量.所不同的是返回值- a++收益a和再增加了,++a第一增量a和再返回.
这归结为我的代码可以缩短为(忽略空白删除):
var a = 0;
function b() {
return ++a > 3;
}
Run Code Online (Sandbox Code Playgroud)
但是,Closure Compiler似乎没有改变(或认识)这一点.
因此,我的问题是:++a >使用时会产生哪些副作用而不是a++ >=?
javascript pre-increment post-increment google-closure-compiler
我是关闭库的新手,我开始了.我刚刚在我的windows7机器上安装了Python,想要连接和缩小脚本.我按照这里记录的那些命令执行了一些但没有收获 这里有一些参数
Python安装在 c:\python27\python.exe
关闭库 c:\closure\
Closure编译器 c:\closure\bin\build\compiler.jar
我的Javascript文件 D:\projects\closureapp\js\index.js
index.js的内容如下
/// <reference path="../closure/base.js" />
/// <reference path="../closure/dom/dom.js" />
/*Hello world into Closure Library Example*/
//Load the dom module
goog.require("goog.dom");
//refer the document body
var pageBody = document.body;
//after the body is loaded execute and add a header
pageBody.onload = function () {
//create a header for the page
var pageHeader = goog.dom.createDom('h1', { 'style': 'background-color:#EEE' }, 'Hello world!');
//append the header to the document body
goog.dom.appendChild(pageBody, pageHeader); …Run Code Online (Sandbox Code Playgroud) google-closure google-closure-library google-closure-compiler
我正在使用html5boilerplate构建脚本并在缩小脚本时(使用Google Closure Compiler)
我收到了这个错误
-js.all.minify:
[echo] Minifying scripts
[copy] Copying 3 files to /Users/Username/Desktop/Web/intermediate/js
[apply] /Users/Juan/Desktop/Web/js/plugins.js:117: ERROR - Parse error. Internet Explorer has a non-standard intepretation of trailing commas. Arrays will have the wrong length and objects will not parse at all.
[apply] }, { duration: 727 })
[apply]
^
Run Code Online (Sandbox Code Playgroud)
但是,如果运行未编译,代码将在IE 8中运行.
这是代码
anim1.animate({
'left': '+=32px',
'filter': 'alpha(opacity=100)',
'-moz-opacity': '1',
'-khtml-opacity': '1',
'opacity': '1',
}, { duration: 727 })
Run Code Online (Sandbox Code Playgroud)
如何让这段代码通过Compulsure Compiler?
谢谢
jquery build-script google-closure google-closure-compiler html5boilerplate
我有JS项目:源代码+测试.目前测试是在原始资源上运行的,一切都很好.
但随后消息来源缩小了,我想在他们的缩小版本上运行我的所有测试.请注意,由于缩小,所有函数名称都被重命名.它是可解决的任务吗?理想情况下,测试/来源不会有太大变化.
我现在的配置是:TeamCity,karma.js + mocha,闭包编译器(高级优化).
javascript unit-testing google-closure-compiler karma-runner
我使用Google Closure Compiler使用PHP自动编译javascript(需要这样做 - 在PHP中,在Windows机器上没有安全限制).我编写了简单的PHP脚本来调用进程,将.js内容传递给stdin并通过stdout接收重新编译的.js.它工作正常,问题是,当我编译例如40 .js文件时,它需要强大的机器将近2分钟.但是,市长延迟是因为java为每个脚本启动.jar应用程序的新实例.有没有办法如何修改下面的脚本来创建进程只有一个并在进程结束前多次发送/接收.js内容?
function compileJScript($s) {
$process = proc_open('java.exe -jar compiler.jar', array(
0 => array("pipe", "r"), 1 => array("pipe", "w")), $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $s);
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) == 0) // If fails, keep $s intact
$s = $output;
}
return $s;
}
Run Code Online (Sandbox Code Playgroud)
我可以看到几个选项,但不知道是否可能以及如何做到这一点:
如何使用Google Closure继承机制测试JavaScript对象是否是接口的实现?
我找不到任何暗示my.Animal在通过创建对象new my.Dog()并object instanceof my.Animal没有奏效.忘记在子类中实现方法时,有关接口的唯一信息是编译器错误.
/**
* @interface
*/
my.Animal = function() {};
/**
* Does something.
* @return {string}
*/
my.Animal.prototype.doSomething;
/**
* @constructor
* @implements {my.Animal}
*/
my.Dog = function() {};
/** @inheritDoc */
my.Dog.prototype.doSomething() = function {
return "something";
}
var dog = new my.Dog();
console.log(dog instanceof my.Animal); // returns false
Run Code Online (Sandbox Code Playgroud)
我找到的一种方法是大致测试接口的属性,尽管这在很多方面都很糟糕:
console.log(!!dog.doSomething); // returns true
Run Code Online (Sandbox Code Playgroud) javascript google-closure google-closure-library google-closure-compiler
我正在努力调试Closure Compiler编译的js文件及其相应的源映射文件(问题:关联文件的源代码未显示在Chrome / firefox中)。
Closure Compiler是否有办法将源映射“内联”放在已编译的js文件中(而不是生成单独的源映射文件)?
如果不是,是否可以修改已编译的js文件,以用源地图内容替换源地图链接?
javascript ×6
angular ×1
build-script ×1
java ×1
jquery ×1
karma-runner ×1
module ×1
php ×1
sha ×1
source-maps ×1
terser ×1
typescript ×1
unit-testing ×1
webpack ×1