如何使用 Java 在运行时缩小不同的 javascript 文件

发布于 2024-11-04 08:57:51 字数 542 浏览 3 评论 0原文

我正在尝试构建(或找到一个我可以使用的现有过滤器)一个可以在运行时压缩 JavaScript 文件的 Web 过滤器。我尝试过基于 YUICompressor 构建一个,但是当我尝试将基于字符串的源而不是实际文件传递给它时,我遇到了奇怪的错误。

现在,我预计会收到诸如“实时压缩/缩小是一个坏主意”之类的回复的轰炸,但我不想在构建时这样做是有原因的。

我有一个 JavaScript Web 应用程序,可以延迟加载它的 JavaScript。它只会加载它实际需要的内容。 JavaScript 文件可以指定依赖项,并且我已经有一个过滤器,它将连接请求的文件和尚未加载到单个响应中的任何依赖项。这意味着 JavaScript 中有大量不同的组合将发送给用户,这使得尝试在构建时构建所有包变得不切实际。

所以重申一下。理想情况下,我正在寻找一个现有的实时 JavaScript 过滤器,我可以将其插入到我的应用程序中。

如果不存在,我正在寻找有关可以用作构建块的提示。 YUICompressor 还没有让我完全明白,而 GoogleClosure 似乎只是一个 Web API。

干杯, 彼得

I'm trying to build (or find an existing one I can use) a web filter that will compress a JavaScript file at runtime. I've tried building one based on YUICompressor, but I'm getting weird errors out of it when I try and pass a String based source into it instead of an actual file.

Now I'm expecting to get bombarded with responses like 'Real time compression/minification is a bad idea' but there is a reason I'm not wanting to do it at build time.

I've got a JavaScript web application that lazy loads it's JavaScript. It will only load what it actually needs. The JavaScript files can specify dependencies and I already have a filter that will concatenate the requested files and any dependencies not already loaded into a single response. This means that there is a large number of different combinations in the JavaScript that will be sent to the user which makes trying to build all the bundles at build time impractical.

So to restate. Ideally I'm looking for an existing real time javascript filter I can just plug into my app.

If one doesn't exist I'm looking for tips on what I can use as building blocks. YUICompressor hasn't quite got me there and GoogleClosure seems to only be a web API.

Cheers,
Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亢潮 2024-11-11 08:57:52

查看 Douglas Crockford 的JavaScript Minifier。源代码在这里:JSMin.java。它不是过滤器,仅包含要缩小的代码。我们将其制作成一个过滤器,我们还可以在其中动态组合和缩小 JavaScript。它运行良好,特别是当您有浏览器和 CDN 缓存结果时。

更新:我遗漏了我们也在服务器上缓存它们。仅当组合和缩小输出所需的任何资产发生变化时,它们才会重新生成。基本上,我们不是在构建时“编译”它们,而是在运行时处理每个组合一次。

Take a look at The JavaScript Minifier from Douglas Crockford. The source is here: JSMin.java. It's not a filter and only contains the code to minify. We've made it into a filter where we combine and minify JavaScript on the fly as well. It works well, especially if you have browsers and a CDN cache results.

Update: I left out that we cache them on the server too. They're only regenerated if any of the assets required to make the combined and minified output have changed. Basically instead of "compiling" them at build time, we handle each combination once at runtime.

つ低調成傷 2024-11-11 08:57:52

我已经有一个过滤器了
连接请求的文件并
任何尚未加载的依赖项
转化为单一响应

Sooo.. 为什么不在加载/连接它们之前缩小它们呢?

(是的,动态压缩的成本非常高,并且绝对不值得为每个提供的 .js 文件进行压缩)

I already have a filter that will
concatenate the requested files and
any dependencies not already loaded
into a single response

Sooo.. Why not just minify those prior to loading/concatenating them?

(And yes, compression on the fly is horribly expensive and definitely not worth doing for every .js file served up)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文